importexport/impexppropconversion/orimpexppropconversion_tool.cxx

importexport/impexppropconversion/orimpexppropconversion_tool.cxx
/***************************************************************************************
Autodesk(R) Open Reality(R) Samples
(C) 2009 Autodesk, Inc. and/or its licensors
All rights reserved.
AUTODESK SOFTWARE LICENSE AGREEMENT
Autodesk, Inc. licenses this Software to you only upon the condition that
you accept all of the terms contained in the Software License Agreement ("Agreement")
that is embedded in or that is delivered with this Software. By selecting
the "I ACCEPT" button at the end of the Agreement or by copying, installing,
uploading, accessing or using all or any portion of the Software you agree
to enter into the Agreement. A contract is then formed between Autodesk and
either you personally, if you acquire the Software for yourself, or the company
or other legal entity for which you are acquiring the software.
AUTODESK, INC., MAKES NO WARRANTY, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE REGARDING THESE MATERIALS, AND MAKES SUCH MATERIALS AVAILABLE SOLELY ON AN
"AS-IS" BASIS.
IN NO EVENT SHALL AUTODESK, INC., BE LIABLE TO ANYONE FOR SPECIAL, COLLATERAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES IN CONNECTION WITH OR ARISING OUT OF PURCHASE
OR USE OF THESE MATERIALS. THE SOLE AND EXCLUSIVE LIABILITY TO AUTODESK, INC.,
REGARDLESS OF THE FORM OF ACTION, SHALL NOT EXCEED THE PURCHASE PRICE OF THE
MATERIALS DESCRIBED HEREIN.
Autodesk, Inc., reserves the right to revise and improve its products as it sees fit.
Autodesk and Open Reality are registered trademarks or trademarks of Autodesk, Inc.,
in the U.S.A. and/or other countries. All other brand names, product names, or
trademarks belong to their respective holders.
GOVERNMENT USE
Use, duplication, or disclosure by the U.S. Government is subject to restrictions as
set forth in FAR 12.212 (Commercial Computer Software-Restricted Rights) and
DFAR 227.7202 (Rights in Technical Data and Computer Software), as applicable.
Manufacturer is Autodesk, Inc., 10 Duke Street, Montreal, Quebec, Canada, H3C 2L7.
***************************************************************************************/
#define _CRT_SECURE_NO_WARNINGS 1
//--- Class declaration
#include "orimpexppropconversion_tool.h"
//--- Registration defines
#define ORTOOLPROPERTYCONVERSION__CLASS ORTOOLPROPERTYCONVERSION__CLASSNAME
#define ORTOOLPROPERTYCONVERSION__LABEL "Property Conversion"
#define ORTOOLPROPERTYCONVERSION__DESC "OR - Property Conversion"
//--- FiLMBOX implementation and registration
FBToolImplementation( ORTOOLPROPERTYCONVERSION__CLASS );
FBRegisterTool ( ORTOOLPROPERTYCONVERSION__CLASS,
ORTOOLPROPERTYCONVERSION__LABEL,
ORTOOLPROPERTYCONVERSION__DESC,
FB_DEFAULT_SDK_ICON ); // Icon filename (default=Open Reality icon)
/************************************************
* FiLMBOX Creation Function.
************************************************/
bool ORToolPropertyConversion::FBCreate()
{
// Tool variables
mCurrentPhase = eCreateData;
// UI Create & Configure
UICreate ();
UIConfigure ();
return true;
}
/************************************************
* FiLMBOX Destruction function.
************************************************/
void ORToolPropertyConversion::FBDestroy()
{
}
/************************************************
* Create the UI.
************************************************/
void ORToolPropertyConversion::UICreate()
{
int lW = 200;
int lS = 5;
int lH = 25;
// Add regions
AddRegion ("EditExportFile", "EditExportFile",
lS, kFBAttachLeft, "", 1.0,
lS, kFBAttachTop, "", 1.0,
lW, kFBAttachNone, NULL, 1.0,
lH, kFBAttachNone, NULL, 1.0 );
AddRegion ("EditNumberRate", "EditNumberRate",
0, kFBAttachLeft, "EditExportFile", 1.0,
lS, kFBAttachBottom, "EditExportFile", 1.0,
0, kFBAttachWidth, "EditExportFile", 1.0,
0, kFBAttachHeight, "EditExportFile", 1.0 );
AddRegion ("ButtonExport", "ButtonExport",
0, kFBAttachLeft, "EditNumberRate", 1.0,
lS, kFBAttachBottom, "EditNumberRate", 1.0,
0, kFBAttachWidth, "EditNumberRate", 1.0,
0, kFBAttachHeight, "EditNumberRate", 1.0 );
// Assign regions
SetControl( "EditExportFile", mEditExportFile );
SetControl( "ButtonExport", mButtonExport );
}
/************************************************
* Configure the UI.
************************************************/
void ORToolPropertyConversion::UIConfigure()
{
mButtonExport.Caption = "Create Data";
#ifdef KARCH_ENV_WIN
mEditExportFile.Text = "C:\\orimpexppropconversion.fbx";
#else
mEditExportFile.Text = "/usr/tmp/orimpexppropconversion.fbx";
#endif
// Add callbacks
mButtonExport.OnClick.Add ( this,(FBCallback)&ORToolPropertyConversion::EventButtonClick );
}
/************************************************
* Browse button callback.
************************************************/
void ORToolPropertyConversion::EventButtonClick(HIRegister pSender, HKEvent pEvent)
{
// If currently exporting, stop and let the idle stop the export.
switch(mCurrentPhase)
{
case eCreateData:
if(CreateData())
{
mButtonExport.Caption = "Export file";
mCurrentPhase = eExportData;
}
break;
case eExportData:
if(ExportFile())
{
mButtonExport.Caption = "Import file";
mCurrentPhase = eImportData;
}
break;
case eImportData:
if(ImportFile())
{
mButtonExport.Caption = "Clean Scene";
mCurrentPhase = eCleanScene;
}
break;
case eCleanScene:
if(CleanScene())
{
mButtonExport.Caption = "Create Data";
mCurrentPhase = eCreateData;
}
break;
default:
mCurrentPhase = eCreateData;
};
}
/************************************************
* Create data.
************************************************/
FBAnimationNode* ORToolPropertyConversion::FindAnimationNode( FBAnimationNode* pNode, const char* pName )
{
const char* lName;
// For the number of child nodes on pNode.
for(int i=0;i < pNode->Nodes.GetCount();i++)
{
lName = pNode->Nodes[i]->Name;
// If the same, return the node.
if(strcmp(lName,pName)==0)
{
return pNode->Nodes[i];
}
}
return NULL;
}
/************************************************
* Create data.
************************************************/
bool ORToolPropertyConversion::CreateData()
{
// Create a new marker
FBFCurve* lCurveX;
FBFCurve* lCurveY;
FBFCurve* lCurveZ;
FBAnimationNode* lNodeTranslation;
FBAnimationNode* lNodeTrans_X;
FBAnimationNode* lNodeTrans_Y;
FBAnimationNode* lNodeTrans_Z;
mHdlModelExport = new FBModelPlane("Tool Property Conversion Model");
mHdlModelExport->Show = true;
mHdlModelExport->Translation.SetAnimated(true);
lNodeTranslation = FindAnimationNode(mHdlModelExport->AnimationNode, ANIMATIONNODE_TYPE_LOCAL_TRANSLATION);
lNodeTrans_X = FindAnimationNode(lNodeTranslation, "X");
lNodeTrans_Y = FindAnimationNode(lNodeTranslation, "Y");
lNodeTrans_Z = FindAnimationNode(lNodeTranslation, "Z");
// Set base layer as current layer
FBSystem().CurrentTake->SetCurrentLayer(0);
// Get FCurves
lCurveX = (FBFCurve*) lNodeTrans_X->FCurve;
lCurveY = (FBFCurve*) lNodeTrans_Y->FCurve;
lCurveZ = (FBFCurve*) lNodeTrans_Z->FCurve;
// Create if necessary
if(!lCurveX) lCurveX = new FBFCurve;
else lCurveX->Keys.RemoveAll();
if(!lCurveY) lCurveY = new FBFCurve;
else lCurveY->Keys.RemoveAll();
if(!lCurveZ) lCurveZ = new FBFCurve;
else lCurveZ->Keys.RemoveAll();
// Setup the take timing
FBTime lStart,lStop;
// Set start & stop times
lStart.SetMilliSeconds(0);
lStop.SetMilliSeconds(1000);
// Add keys
lCurveX->KeyAdd(lStart,0);
lCurveX->KeyAdd(lStop,100);
lCurveY->KeyAdd(lStart,0);
lCurveY->KeyAdd(lStop,100);
lCurveZ->KeyAdd(lStart,0);
lCurveZ->KeyAdd(lStop,100);
//change layer and add keys on that layer
FBSystem().CurrentTake->SetCurrentLayer(1);
lCurveX = (FBFCurve*) lNodeTrans_X->FCurve;
lCurveY = (FBFCurve*) lNodeTrans_Y->FCurve;
lCurveZ = (FBFCurve*) lNodeTrans_Z->FCurve;
// Create if necessary
if(!lCurveX) lCurveX = new FBFCurve;
else lCurveX->Keys.RemoveAll();
if(!lCurveY) lCurveY = new FBFCurve;
else lCurveY->Keys.RemoveAll();
if(!lCurveZ) lCurveZ = new FBFCurve;
else lCurveZ->Keys.RemoveAll();
lCurveX->KeyAdd(lStart,50);
lCurveX->KeyAdd(lStop,150);
lCurveY->KeyAdd(lStart,50);
lCurveY->KeyAdd(lStop,150);
lCurveZ->KeyAdd(lStart,50);
lCurveZ->KeyAdd(lStop,150);
return true;
}
/************************************************
* File exporting.
************************************************/
bool ORToolPropertyConversion::ExportFile()
{
if(strlen(mEditExportFile.Text))
{
// Create a SdkManager
FbxManager* lSdkManager = FbxManager::Create();
// Create an IOSettings object
FbxIOSettings* ios = FbxIOSettings::Create(lSdkManager, IOSROOT);
// Create an empty scene
FbxScene* lScene = FbxScene::Create(lSdkManager, "");
// Create an exporter.
FbxExporter* lExporter = FbxExporter::Create(lSdkManager, "");
// Initialize the exporter by providing a filename and the IOSettings to use
if(lExporter->Initialize(mEditExportFile.Text, 1, ios))
{
FbxAnimStack* lAnimStack = FbxAnimStack::Create(lScene, FBSystem().CurrentTake->GetFullName());
// Create a dummy object that will hold the property data
FbxNode* lDummyNode = FbxNode::Create(lSdkManager, "dummy");
FbxNode* lRootNode = lScene->GetRootNode();
lRootNode->AddChild(lDummyNode);
// Convert the translation to a FBX property
FbxProperty lNewFBXProperty = FBtoFBXProperty((FBProperty*)(&mHdlModelExport->Translation), FBSystem().CurrentTake, lDummyNode, lScene, lAnimStack, true);
// export the scene.
lExporter->Export(lScene);
// destroy the exporter
lExporter->Destroy();
return true;
}
}
return false;
}
/************************************************
* Utility function to verify if a file exists
************************************************/
bool FileExist( const char* pFileName )
{
bool lReturn = false;
FILE* lFile = fopen( pFileName, "r" );
if( lFile != NULL )
{
lReturn = true;
fclose(lFile);
}
return lReturn;
}
/************************************************
* File importing.
************************************************/
bool ORToolPropertyConversion::ImportFile()
{
if( FileExist( mEditExportFile.Text ) )
{
// Import that scene and apply the animation on a new place
mHdlModelImport = new FBModelPlane("Model for file import");
mHdlModelImport->Show = true;
// Create a SdkManager
FbxManager* lSdkImportManager = FbxManager::Create();
// Create an IOSettings object
FbxIOSettings* ios = FbxIOSettings::Create(lSdkImportManager, IOSROOT);
ios->SetBoolProp(IMP_FBX_ANIMATION, true);
// Create an importer.
FbxImporter* lImporter = FbxImporter::Create(lSdkImportManager, "");
// Create an empty scene
FbxScene* lSceneImport = FbxScene::Create(lSdkImportManager,"MyScene");
// Initialize the importer by providing a filename and the IOSettings to use
if(lImporter->Initialize(mEditExportFile.Text, -1, ios))
{
// Import the scene.
lImporter->Import(lSceneImport);
// Import the object, we will find the "dummy" object that we have saved before
FbxNode* lRootNode = lSceneImport->GetRootNode();
FbxNode* lImportedDummyObject = NULL;
for(int lModelCount = 0; lModelCount < lRootNode->GetChildCount(); lModelCount++)
{
if(strcmp(lRootNode->GetChild(lModelCount)->GetName(), "dummy") == 0)
{
lImportedDummyObject = lRootNode->GetChild(lModelCount);
break;
}
}
if(lImportedDummyObject)
{
// Import the translation property that we have saved before
FbxAnimStack* lAnimStack = FbxCast<FbxAnimStack>(lSceneImport->GetSrcObject<FbxAnimStack>(0));
FbxProperty lSrcProp = lImportedDummyObject->FindProperty("Lcl Translation");
if(lSrcProp.IsValid())
{
/*FBProperty* lNewFBProperty = */FBXtoFBProperty(&lSrcProp, lAnimStack, mHdlModelImport, FBSystem().CurrentTake, true);
}
}
// Destroy the importer.
lImporter->Destroy();
}
}
return true;
}
/************************************************
* File importing.
************************************************/
bool ORToolPropertyConversion::CleanScene()
{
if( mHdlModelExport.Ok() )
{
mHdlModelExport->FBDelete();
}
if( mHdlModelImport.Ok() )
{
mHdlModelImport->FBDelete();
}
return true;
}