FBX SDK Programmer's Guide > Importing and Exporting a Scene >

# 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 FbxImporter class. Instances of FbxImporter are created with a reference to the program's FbxManager singleton object. A FbxImporter must have its FbxImporter::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 FbxIOSettings object containing the import configuration options. See I/O Settings for more information.

    #include <fbxsdk.h>
    #include <fbxfilesdk/fbxio/fbxiosettings.h>

    // ...

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

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

    // ... Configure the FbxIOSettings object ...

    // Create an importer.
    FbxImporter* lImporter = FbxImporter::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 FbxImporter::Initialize(), the method will return false. To retrieve the error , call FbxIO::GetLastErrorString() or FbxIO::GetLastErrorID() (observe that FbxImporter is a subclass of FbxIO). For more information on error handling, see Error Handling.

    if(!lImportStatus) {
        printf("Call to FbxImporter::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 FbxScene 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.
    FbxScene* lScene = FbxScene::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 FbxImporter::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 FbxObject::GetSrcObjectCount() and FbxObject::GetSrcObject() functions. The concept of sources, destinations, and connections in the context of the FBX SDK is explained in FBX SDK Object Model.

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

#### See Also

- Customizing File Formats with FBX SDK I/O Plug-ins