Importing a Scene
 
 
 

A scene can be imported by the FBX SDK from various file formats. The FBX file format can either be in binary (also known as the native format), or in ASCII. Note that only the binary FBX file format supports embedded media (see I/O Settings). The Collada file format and other non-FBX file formats can also be used to import scenes. For more information, see Supported File Formats.

Initializing the Importer

The scene importing functionality of the FBX SDK is abstracted by the KFbxImporter class. Instances of KFbxImporter are created with a reference to the program's KFbxSdkManager singleton object. A KFbxImporter must have its KFbxImporter::Initialize() method called with three parameters:

  1. The path and filename of the file containing the scene to import.
  2. The numeric file format identifier. Typically, this parameter is set to -1 to let the importer automatically detect the file format according to the provided filename's extension.
  3. The KFbxIOSettings object containing the import configuration options. See I/O Settings for more information.

#include <fbxsdk.h>
#include <fbxfilesdk/kfbxio/kfbxiosettings.h>

// ...

// Create the FBX SDK manager
KFbxSdkManager* lSdkManager = KFbxSdkManager::Create();

// Create an IOSettings object.
KFbxIOSettings * ios = KFbxIOSettings::Create(lSdkManager, IOSROOT );
lSdkManager->SetIOSettings(ios);

// ... Configure the KFbxIOSettings object ...

// Create an importer.
KFbxImporter* lImporter = KFbxImporter::Create(lSdkManager, "");

// Declare the path and filename of the file containing the scene.
// In this case, we are assuming the file is in the same directory as the executable.
const char* lFilename = "file.fbx";

// Initialize the importer.
bool lImportStatus = lImporter->Initialize(lFilename, -1, lSdkManager->GetIOSettings());

If any errors occur in the call to KFbxImporter::Initialize(), the method will return false. To retrieve the error , call KFbxIO::GetLastErrorString() or KFbxIO::GetLastErrorID() (observe that KFbxImporter is a subclass of KFbxIO). For more information on error handling, see Error Handling.

if(!lImportStatus) {
    printf("Call to KFbxImporter::Initialize() failed.\n");
    printf("Error returned: %s\n\n", lImporter->GetLastErrorString());
    exit(-1);
}

Importing a Scene

Once the importer has been initialized, a scene container must be created to load the scene from the file. Scenes in the FBX SDK are abstracted by the KFbxScene class. For more information on scenes, see Nodes and the Scene Graph.

// Create a new scene so it can be populated by the imported file.
KFbxScene* lScene = KFbxScene::Create(lSdkManager,"myScene");

// Import the contents of the file into the scene.
lImporter->Import(lScene);

After the importer has populated the scene, it is safe to destroy it to reduce memory usage.

// The file has been imported; we can get rid of the importer.
lImporter->Destroy();

File Version Number

The FBX file format version is incremented to reflect newly supported features (see Supported File Formats). The FBX version of the currently imported file can be obtained by calling KFbxImporter::GetFileVersion().

// File format version numbers to be populated.
int lFileMajor, lFileMinor, lFileRevision;

// Populate the FBX file format version numbers with the import file.
lImporter->GetFileVersion(lFileMajor, lFileMinor, lFileRevision);

As of FBX SDK 2012.0, the header file fbxfilesdk_version.h defines the preprocessor identifer FBXSDK_VERSION_STRING, which represents the version information as a string.

Imported Custom Objects

When the FBX SDK encounters a new object that it does not recognize during the importing process, it creates a new class for it at runtime. To access this object, add a filter to the KFbxObject::GetSrcObjectCount() and KFbxObject::GetSrcObject() functions. The concept of sources, destinations, and connections in the context of the FBX SDK is explained in FBX SDK Object Model.

kFbxClassId lShaderClassID = lSdkManager->FindFbxFileClass("Shader", "FlatShader");
for(int i = 0; i < lNode->GetSrcObjectCount(lShaderClassID); i++) {
  KFbxObject* lObject = lNode->GetSrcObject(lShaderClassID, i);
}
See Also