Exporting a Scene
 
 
 

The FBX SDK can export a scene into various file formats (see Supported File Formats), including both binary and ASCII FBX file formats. Note that only the binary FBX file format supports embedded media (see I/O Settings).

Initializing the Exporter

The scene exporting functionality of the FBX SDK is abstracted by the KFbxExporter class. The steps required to initialize the exporter object and to export the scene are analogous to the steps detailed in Importing a Scene. A KFbxExporter object is created using a reference to the program's KFbxSdkManager singleton. The KFbxExporter::Initialize() method determines the filename and configuration settings of the exporter. This method requires three parameters:

  1. The path and filename of the file to which the scene will be exported.
  2. The numeric file format identifier. Typically, this parameter is set to -1 to let the exporter automatically detect the file format according to the provided filename's extension.
  3. The KFbxIOSettings object containing the export 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 here ...

// Create an exporter.
KFbxExporter* lExporter = KFbxExporter::Create(lSdkManager, "");

// Declare the path and filename of the file to which the scene will be exported.
// In this case, the file will be in the same directory as the executable.
const char* lFilename = "file.fbx";

// Initialize the exporter.
bool lExportStatus lExporter->Initialize(lFilename, -1, lSdkManager->GetIOSettings());

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

if(!lExportStatus) {
    printf("Call to KFbxExporter::Initialize() failed.\n");
    printf("Error returned: %s\n\n", lExporter->GetLastErrorString());
    return false;
}

Exporting a Scene

The KFbxExporter::Export() method exports the specified scene (KFbxScene) to the file . For more information on building and manipulating scenes with the FBX SDK, see Nodes and the Scene Graph.

NoteDuring an export operation, the root node of a scene is not exported to the file. Only the children of the root node are exported to the file. As such, it is not recommended to associate anything to the root node that should be saved to a file.

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

// ... Import a scene, or build a new one ...

// Export the scene to the file.
lExporter->Export(lScene);

Once the scene has been exported, the exporter can safely be destroyed to minimize memory allocation.

// Destroy the exporter.
lExporter->Destroy();

File Version Number

Use the KFbxSdkManager::GetFileFormatVersion() method to obtain the exported FBX file version number. For the latest FBX file version number, see Supported File Formats.

// File format version numbers to be populated.
int lMajor, lMinor, lRevision;

// Populate the exported file format version numbers.
KFbxSdkManager::GetFileFormatVersion(lMajor, lMinor, lRevision);

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.

Exporting With Embedded Media

To export a scene and have its media embedded within the exported FBX file, the binary (or native) FBX file format identifier must be specified in the call to KFbxExporter::Initialize(). The following code snippet illustrates how to perform this task. Recall that the KFbxIOSettings object must also be configured to embed the scene's media into the file (see I/O Settings).

// ... Initialize the required objects and variables ...

// Set the KFbxIOSettings EXP_FBX_EMBEDDED property to true.
(*(lSdkManager->GetIOSettings())).SetBoolProp(EXP_FBX_EMBEDDED, true);

// Get the appropriate file format.
int lFileFormat = lSdkManager->GetIOPluginRegistry()->GetNativeWriterFormat();

// Initialize the exporter to embed the scene's media into the exported file.
bool lExportStatus lExporter->Initialize(lFilename, lFileFormat, lSdkManager->GetIOSettings());

// ... Export the scene ...
See Also