Community
// The first function I call after getting the scene.
void FileReader::convertScene(KFbxScene * _pFbxScene)
{
// Convert in Max axis system and in metric system.
KFbxAxisSystem oldAxisSys = _pFbxScene->GetGlobalSettings().GetAxisSystem()
KFbxAxisSystem::Max.ConvertScene(_pFbxScene);
KFbxSystemUnit::m.ConvertScene(_pFbxScene);
// All the nodes linked to the root with the name "Fbx_Root" have a special treatment.
// It allows our animators to pack the assets together in the FBX hierarchical tree view.
KFbxNode * root = _pFbxScene->GetRootNode();
int numChildren = root->GetChildCount();
for(int i = 0; i < numChildren; ++i)
{
KFbxNode * child = root->GetChild(i);
// Get node name without prefix.
char const * nodeName = getNameWithoutNamespace(child->GetName());
// Convert all the node named "Fbx_Root" (eventually with a prefix).
if(strcmp(nodeName, "Fbx_Root") == 0)
KFbxAxisSystem::Max.ConvertChildren(child, oldAxisSys);
}
}
// One of the functions I call during the import.
// It allows to parse the curves on a node.
// FBX_E_CT_Translation{X,Y,Z} are simple integer defines.
void parseObjectCurves(KFbxNode * _fbxNode, KFbxAnimLayer * _layer, KFbxAnimCurveMap & _curves)
{
KFbxAnimCurve * curve = NULL;
// Translation curves.
float const scaleFactor = static_cast<float>(1.0f / _fbxNode->GetScene()->GetGlobalSettings().GetSystemUnit().GetScaleFactor());
curve = _fbxNode->LclTranslation.GetCurve<KFbxAnimCurve>(_layer, KFCURVENODE_T_X);
if(curve && curve->KeyGetCount() > 0)
{
curve->KeyScaleValueAndTangent(scaleFactor);
_curves.insert(KFbxAnimCurveMap::value_type(FBX_E_CT_TranslationX, curve));
}
curve = _fbxNode->LclTranslation.GetCurve<KFbxAnimCurve>(_layer, KFCURVENODE_T_Y);
if(curve && curve->KeyGetCount() > 0)
{
curve->KeyScaleValueAndTangent(scaleFactor);
_curves.insert(KFbxAnimCurveMap::value_type(FBX_E_CT_TranslationY, curve));
}
curve = _fbxNode->LclTranslation.GetCurve<KFbxAnimCurve>(_layer, KFCURVENODE_T_Z);
if(curve && curve->KeyGetCount() > 0)
{
curve->KeyScaleValueAndTangent(scaleFactor);
_curves.insert(KFbxAnimCurveMap::value_type(FBX_E_CT_TranslationZ, curve));
}
// Rotation curves.
// Same without the KeyScaleValueAndTangent() call ...
// Scale curves.
// Same without the KeyScaleValueAndTangent() call ...
}
KFbxSdkManager* lSdkManager = NULL;
KFbxScene* lScene = NULL;
bool lResult;
// Prepare the FBX SDK.
InitializeSdkObjects(lSdkManager, lScene);
// Load the scene.
lResult = LoadScene(lSdkManager, lScene, "CM.fbx");
if(lResult == false)
{
printf("\n\nAn error occurred while loading the scene...");
}
else
{
KFbxSystemUnit::m.ConvertScene(lScene);
SaveScene(lSdkManager, lScene, "CM_M.fbx");
}
void FileReader::convertAxisSystem(KFbxScene * _fbxScene, KFbxAxisSystem const & _axisSys)
{
// Get root node.
KFbxNode * root = _fbxScene->GetRootNode();
// Convert to Max axis.
KFbxAxisSystem oldAxisSys = _fbxScene->GetGlobalSettings().GetAxisSystem();
_axisSys.ConvertScene(_fbxScene);
// All the nodes linked to the root with the name "Fbx_Root" have a special treatment.
// It allows the animators to pack the assets together in the FBX hierarchical tree view.
int numChildren = root->GetChildCount();
for(int i = 0; i < numChildren; ++i)
{
KFbxNode * child = root->GetChild(i);
// Get node name without prefix.
char const * nodeName = getNameWithoutNamespace(child->GetName());
// Convert all the node named "Fbx_Root" (eventually with a prefix).
if(strcmp(nodeName, "Fbx_Root") == 0)
_axisSys.ConvertChildren(child, oldAxisSys);
}
}
void FileReader::convertUnitSystem(KFbxScene * _fbxScene, KFbxSystemUnit const & _unitSys)
{
// Get scale factor.
double const scaleFactor = _fbxScene->GetGlobalSettings().GetSystemUnit().GetConversionFactorTo(_unitSys);
if(scaleFactor == 1.0f)
return;
// Get root node.
KFbxNode * root = _fbxScene->GetRootNode();
// Parse all the nodes to convert the translations and meshes vertices.
std::deque<KFbxNode *> nodes;
nodes.push_back(root);
while(!nodes.empty())
{
KFbxNode * node = nodes.front();
nodes.pop_front();
#ifdef DEBUG
char const * name = node->GetName();
#endif // DEBUG
// Convert node's translation.
fbxDouble3 lcltra = node->LclTranslation.Get();
lcltra *= scaleFactor;
lcltra *= scaleFactor;
lcltra *= scaleFactor;
node->LclTranslation.Set(lcltra);
KFbxMesh * mesh = node->GetMesh();
if(mesh)
{
// Convert vertices.
unsigned int const numVertices = mesh->GetControlPointsCount();
if(numVertices)
{
KFbxVector4 * vertexBuffer = mesh->GetControlPoints();
for(unsigned int i = 0; i < numVertices; ++i)
vertexBuffer *= scaleFactor;
}
}
int const numChildren = node->GetChildCount();
for(int i = 0; i < numChildren; ++i)
nodes.push_back(node->GetChild(i));
}
// Parse all the stacks' layers to convert the translation curves.
int numAnimStacks = _fbxScene->GetSrcObjectCount(FBX_TYPE(KFbxAnimStack));
for(int i = 0; i < numAnimStacks; ++i)
{
KFbxAnimStack * stack = KFbxCast<KFbxAnimStack>(_fbxScene->GetSrcObject(FBX_TYPE(KFbxAnimStack), i));
int nbAnimLayers = stack->GetMemberCount(FBX_TYPE(KFbxAnimLayer));
for(int j = 0; j < nbAnimLayers; ++j)
{
KFbxAnimLayer * layer = stack->GetMember(FBX_TYPE(KFbxAnimLayer), j);
nodes.push_back(root);
while(!nodes.empty())
{
KFbxNode * node = nodes.front();
nodes.pop_front();
#ifdef DEBUG
char const * name = node->GetName();
#endif // DEBUG
// Convert translations curves.
KFbxAnimCurve * curveX = node->LclTranslation.GetCurve<KFbxAnimCurve>(layer, KFCURVENODE_T_X);
KFbxAnimCurve * curveY = node->LclTranslation.GetCurve<KFbxAnimCurve>(layer, KFCURVENODE_T_Y);
KFbxAnimCurve * curveZ = node->LclTranslation.GetCurve<KFbxAnimCurve>(layer, KFCURVENODE_T_Z);
if(curveX)
curveX->KeyScaleValueAndTangent(static_cast<float>(scaleFactor));
if(curveY)
curveY->KeyScaleValueAndTangent(static_cast<float>(scaleFactor));
if(curveZ)
curveZ->KeyScaleValueAndTangent(static_cast<float>(scaleFactor));
int const numChildren = node->GetChildCount();
for(int i = 0; i < numChildren; ++i)
nodes.push_back(node->GetChild(i));
}
}
}
}
Can't find what you're looking for? Ask the community or share your knowledge.