Creating a file importer
 
 
 

To import a file into a scene (i.e., to load a scene with the contents of a file), you must first create an importer object. importexport() calls LoadScene() to do all the steps in loading the scene:

bool r = LoadScene(lSdkManager, lScene, ImportFileName);
 ...
 
bool LoadScene(
               KFbxSdkManager* pSdkManager, // Use this memory manager...
               KFbxScene* pScene,           // to import into this scene
               const char* pFilename        // the data from this file.
               )
{    ...
 
    // Create an importer.
    KFbxImporter* lImporter = KFbxImporter::Create(pSdkManager,"");

FBX can import many different kinds of files: FBX files in a binary (or native) format, FBX files in an ASCII format, FBX files using earlier versions of the FBX file format, and some non-FBX file formats, such as Collada.

The importer needs to know the file format of file it will be importing. This is because the same extension can be used for different file formats. For example, an .fbx extension can refer to a binary FBX file or an ASCII FBX file.

    // Detect the file format of the file to be imported
    if (!KFbxIOPluginRegistryAccessor::Get()->DetectFileFormat(
        pFilename,
        lFileFormat
        ))
    { // ... Error handling code ... }
    lImporter->SetFileFormat(lFileFormat);

The importer also needs to know the path and name of the file to be imported.

    const bool lImportStatus = lImporter->Initialize(pFilename);

For FBX files, ImportExport checks whether the version number of the file format used by the FBX file is supported by the version number of the FBX SDK library that it is linked to:

    // Get the version number of the FBX files generated by the
    // version of the FBX SDK that you are using.
    KFbxIO::GetCurrentVersion)lSDKMajor, lSDKMinor, lSDKRevision);
 ...
    
    // Get the version number of the FBX file format.
    lImporter->GetFileVersion(lFileMajor, lFileMinor, lFileRevision);

Here is an example of FBX error handling:

    if( !lImportStatus ) // Problem with the file to be imported
    {
        UI_Printf("Call to KFbxImporter::Initialize() failed.");
        UI_Printf("Error returned: %s", lImporter->GetLastErrorString());
 
        if (lImporter->GetLastErrorID() ==
            KFbxIO::eFILE_VERSION_NOT_SUPPORTED_YET ||
            lImporter->GetLastErrorID() ==
            KFbxIO::eFILE_VERSION_NOT_SUPPORTED_ANYMORE)
            {
                UI_Printf("FBX version number for this FBX SDK is %d.%d.%d",
                    lSDKMajor, lSDKMinor, lSDKRevision);
                UI_Printf("FBX version number for file %s is %d.%d.%d",
                    pFilename, lFileMajor, lFileMinor, lFileRevision);
            }
 
         return false;
    }
 
    UI_Printf("FBX version number for this FBX SDK is %d.%d.%d",
        lSDKMajor, lSDKMinor, lSDKRevision);

Now we’re ready to load the scene.