<commit_before>#include "AlembicLicensing.h"
#include "AlembicWriteJob.h"
#include "AlembicObject.h"
#include "AlembicXform.h"
#include "AlembicCamera.h"
#include "AlembicPolyMesh.h"
#include "AlembicSubD.h"
#include "AlembicPoints.h"
#include "AlembicCurves.h"
#include "AlembicHair.h"
//#include "AlembicNurbs.h"

#include <maya/MAnimControl.h>

namespace AbcA = ::Alembic::AbcCoreAbstract::ALEMBIC_VERSION_NS;
using namespace AbcA;

AlembicWriteJob::AlembicWriteJob
(
   const MString & in_FileName,
   const MObjectArray & in_Selection,
   const MDoubleArray & in_Frames
)
{
   // ensure to clear the isRefAnimated cache
   clearIsRefAnimatedCache();

   // todo... do we need to resolve this path?
   // mFileName = CUtils::ResolveTokenString(in_FileName,XSI::CTime(),false);
   mFileName = in_FileName;
   for(unsigned int i=0;i<in_Selection.length();i++)
      mSelection.append(in_Selection[i]);

   for(unsigned int i=0;i<in_Frames.length();i++)
      mFrames.push_back(in_Frames[i]);
}

AlembicWriteJob::~AlembicWriteJob()
{
}

void AlembicWriteJob::SetOption(const MString & in_Name, const MString & in_Value)
{
   std::map<std::string,std::string>::iterator it = mOptions.find(in_Name.asChar());
   if(it == mOptions.end())
      mOptions.insert(std::pair<std::string,std::string>(in_Name.asChar(),in_Value.asChar()));
   else
      it->second = in_Value.asChar();
}

bool AlembicWriteJob::HasOption(const MString & in_Name)
{
   std::map<std::string,std::string>::iterator it = mOptions.find(in_Name.asChar());
   return it != mOptions.end();
}

MString AlembicWriteJob::GetOption(const MString & in_Name)
{
   std::map<std::string,std::string>::iterator it = mOptions.find(in_Name.asChar());
   if(it != mOptions.end())
      return it->second.c_str();
   return MString();
}

AlembicObjectPtr AlembicWriteJob::GetObject(const MObject & in_Ref)
{
   MString refString = getFullNameFromRef(in_Ref);
   for(size_t i=0;i<mObjects.size();i++)
   {
      if(getFullNameFromRef(mObjects[i]->GetRef()) == refString)
         return mObjects[i];
   }
   return AlembicObjectPtr();
}

bool AlembicWriteJob::AddObject(AlembicObjectPtr in_Obj)
{
   if(!in_Obj)
      return false;
   if(in_Obj->GetRef().isNull())
      return false;
   AlembicObjectPtr existing = GetObject(in_Obj->GetRef());
   if(existing != NULL)
      return false;
   mObjects.push_back(in_Obj);
   return true;
}


MStatus AlembicWriteJob::PreProcess()
{
   // check filenames
   if(mFileName.length() == 0)
   {
      MGlobal::displayError("[ExocortexAlembic] No filename specified.");
      return MStatus::kInvalidParameter;
   }

   // check objects
   if(mSelection.length() == 0)
   {
      MGlobal::displayError("[ExocortexAlembic] No objects specified.");
      return MStatus::kInvalidParameter;
   }

   // check frames
   if(mFrames.size() == 0)
   {
      MGlobal::displayError("[ExocortexAlembic] No frames specified.");
      return MStatus::kInvalidParameter;
   }

   // check if the file is currently in use
   if(getRefArchive(mFileName) > 0)
   {
      MGlobal::displayError("[ExocortexAlembic] Error writing to file '"+mFileName+"'. File currently in use.");
      return MStatus::kInvalidParameter;
   }

   // init archive (use a locally scoped archive)
   // TODO: determine how to access the current maya scene path
   //MString sceneFileName = "Exported from: "+Application().GetActiveProject().GetActiveScene().GetParameterValue("FileName").GetAsText();
   MString sceneFileName = "Exported from Maya.";
   try
   {
      mArchive = CreateArchiveWithInfo(
            Alembic::AbcCoreHDF5::WriteArchive(),
            mFileName.asChar(),
            "Softimage Alembic Plugin",
            sceneFileName.asChar(),
            Alembic::Abc::ErrorHandler::kThrowPolicy);
   }
   catch(Alembic::Util::Exception& e)
   {
      MString exc(e.what());
      MGlobal::displayError("[ExocortexAlembic] Error writing to file '"+mFileName+"' ("+exc+"). Do you still have it opened?");
      return MStatus::kInvalidParameter;
   }

   // get the frame rate
   mFrameRate = MAnimControl::currentTime().value() / MAnimControl::currentTime().as(MTime::kSeconds);
   std::vector<Alembic::AbcCoreAbstract::chrono_t> frames;
   for(LONG i=0;i<mFrames.size();i++)
      frames.push_back(mFrames[i] / mFrameRate);

   // create the sampling
   double timePerSample = 1.0 / mFrameRate;
   if(frames.size() > 1)
   {
      if( ! HasAlembicWriterLicense() )
      {
         if(frames.size() > 75)
         {           
            frames.resize(75);
            EC_LOG_WARNING("[ExocortexAlembic] Demo Mode: Maximum exportable samplecount is 75!");
         }
      }

      double timePerCycle = frames[frames.size()-1] - frames[0];
      AbcA::TimeSamplingType samplingType((boost::uint32_t)frames.size(),timePerCycle);
      AbcA::TimeSampling sampling(samplingType,frames);
      mTs = mArchive.addTimeSampling(sampling);
   }
   else
   {
      AbcA::TimeSampling sampling(1.0,frames[0]);
      mTs = mArchive.addTimeSampling(sampling);
   }

   Alembic::Abc::OBox3dProperty boxProp = Alembic::AbcGeom::CreateOArchiveBounds(mArchive,mTs);

   // create object for each
   for(unsigned int i=0;i<mSelection.length();i++)
   {
      MObject mObj = mSelection[i];
      if(GetObject(mObj) != NULL)
         continue;

      // take care of all other types
      MString mType = mObj.apiTypeStr();
      if(mType == "kCamera")
      {
         AlembicObjectPtr ptr;
         ptr.reset(new AlembicCamera(mObj,this));<commit_msg>fixed #127, adopt Alembic 1.1<commit_after>#include "AlembicLicensing.h"
#include "AlembicWriteJob.h"
#include "AlembicObject.h"
#include "AlembicXform.h"
#include "AlembicCamera.h"
#include "AlembicPolyMesh.h"
#include "AlembicSubD.h"
#include "AlembicPoints.h"
#include "AlembicCurves.h"
#include "AlembicHair.h"
//#include "AlembicNurbs.h"

#include <maya/MAnimControl.h>

namespace AbcA = ::Alembic::AbcCoreAbstract::ALEMBIC_VERSION_NS;
using namespace AbcA;

AlembicWriteJob::AlembicWriteJob
(
   const MString & in_FileName,
   const MObjectArray & in_Selection,
   const MDoubleArray & in_Frames
)
{
   // ensure to clear the isRefAnimated cache
   clearIsRefAnimatedCache();

   // todo... do we need to resolve this path?
   // mFileName = CUtils::ResolveTokenString(in_FileName,XSI::CTime(),false);
   mFileName = in_FileName;
   for(unsigned int i=0;i<in_Selection.length();i++)
      mSelection.append(in_Selection[i]);

   for(unsigned int i=0;i<in_Frames.length();i++)
      mFrames.push_back(in_Frames[i]);
}

AlembicWriteJob::~AlembicWriteJob()
{
}

void AlembicWriteJob::SetOption(const MString & in_Name, const MString & in_Value)
{
   std::map<std::string,std::string>::iterator it = mOptions.find(in_Name.asChar());
   if(it == mOptions.end())
      mOptions.insert(std::pair<std::string,std::string>(in_Name.asChar(),in_Value.asChar()));
   else
      it->second = in_Value.asChar();
}

bool AlembicWriteJob::HasOption(const MString & in_Name)
{
   std::map<std::string,std::string>::iterator it = mOptions.find(in_Name.asChar());
   return it != mOptions.end();
}

MString AlembicWriteJob::GetOption(const MString & in_Name)
{
   std::map<std::string,std::string>::iterator it = mOptions.find(in_Name.asChar());
   if(it != mOptions.end())
      return it->second.c_str();
   return MString();
}

AlembicObjectPtr AlembicWriteJob::GetObject(const MObject & in_Ref)
{
   MString refString = getFullNameFromRef(in_Ref);
   for(size_t i=0;i<mObjects.size();i++)
   {
      if(getFullNameFromRef(mObjects[i]->GetRef()) == refString)
         return mObjects[i];
   }
   return AlembicObjectPtr();
}

bool AlembicWriteJob::AddObject(AlembicObjectPtr in_Obj)
{
   if(!in_Obj)
      return false;
   if(in_Obj->GetRef().isNull())
      return false;
   AlembicObjectPtr existing = GetObject(in_Obj->GetRef());
   if(existing != NULL)
      return false;
   mObjects.push_back(in_Obj);
   return true;
}


MStatus AlembicWriteJob::PreProcess()
{
   // check filenames
   if(mFileName.length() == 0)
   {
      MGlobal::displayError("[ExocortexAlembic] No filename specified.");
      return MStatus::kInvalidParameter;
   }

   // check objects
   if(mSelection.length() == 0)
   {
      MGlobal::displayError("[ExocortexAlembic] No objects specified.");
      return MStatus::kInvalidParameter;
   }

   // check frames
   if(mFrames.size() == 0)
   {
      MGlobal::displayError("[ExocortexAlembic] No frames specified.");
      return MStatus::kInvalidParameter;
   }

   // check if the file is currently in use
   if(getRefArchive(mFileName) > 0)
   {
      MGlobal::displayError("[ExocortexAlembic] Error writing to file '"+mFileName+"'. File currently in use.");
      return MStatus::kInvalidParameter;
   }

   // init archive (use a locally scoped archive)
   // TODO: determine how to access the current maya scene path
   //MString sceneFileName = "Exported from: "+Application().GetActiveProject().GetActiveScene().GetParameterValue("FileName").GetAsText();
   MString sceneFileName = "Exported from Maya.";
   try
   {
      mArchive = CreateArchiveWithInfo(
            Alembic::AbcCoreHDF5::WriteArchive(),
            mFileName.asChar(),
            "Softimage Alembic Plugin",
            sceneFileName.asChar(),
            Alembic::Abc::ErrorHandler::kThrowPolicy);
   }
   catch(Alembic::Util::Exception& e)
   {
      MString exc(e.what());
      MGlobal::displayError("[ExocortexAlembic] Error writing to file '"+mFileName+"' ("+exc+"). Do you still have it opened?");
      return MStatus::kInvalidParameter;
   }

   // get the frame rate
   mFrameRate = MAnimControl::currentTime().value() / MAnimControl::currentTime().as(MTime::kSeconds);
   std::vector<Alembic::AbcCoreAbstract::chrono_t> frames;
   for(LONG i=0;i<mFrames.size();i++)
      frames.push_back(mFrames[i] / mFrameRate);

   // create the sampling
   double timePerSample = 1.0 / mFrameRate;
   if(frames.size() > 1)
   {
      if( ! HasAlembicWriterLicense() )
      {
         if(frames.size() > 75)
         {           
            frames.resize(75);
            EC_LOG_WARNING("[ExocortexAlembic] Demo Mode: Maximum exportable samplecount is 75!");
         }
      }

      double timePerCycle = frames[frames.size()-1] - frames[0];
	  AbcA::TimeSamplingType samplingType((Alembic::Abc::ALEMBIC_VERSION_NS::uint32_t)frames.size(),timePerCycle);
      AbcA::TimeSampling sampling(samplingType,frames);
      mTs = mArchive.addTimeSampling(sampling);
   }
   else
   {
      AbcA::TimeSampling sampling(1.0,frames[0]);
      mTs = mArchive.addTimeSampling(sampling);
   }

   Alembic::Abc::OBox3dProperty boxProp = Alembic::AbcGeom::CreateOArchiveBounds(mArchive,mTs);

   // create object for each
   for(unsigned int i=0;i<mSelection.length();i++)
   {
      MObject mObj = mSelection[i];
      if(GetObject(mObj) != NULL)
         continue;

      // take care of all other types
      MString mType = mObj.apiTypeStr();
      if(mType == "kCamera")
      {
         AlembicObjectPtr ptr;
         ptr.reset(new AlembicCamera(mObj,this));
{'fraction_non_alphanumeric': 0.14057132525763877, 'fraction_numerical': 0.005333574398842886, 'mean_word_length': 3.770590771884433, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4183006', 'n_tokens_mistral': 3867, 'n_tokens_neox': 3702, 'n_words': 667}