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

Loading an FBX SDK I/O Plug-in

The FBX SDK uses a plug-in architecture to implement its file reading and writing capabilities. FBX SDK I/O Plug-ins consist of dynamically loaded library (DLL) files which can be loaded using the FbxManager::LoadPluginsDirectory() method. The following code can be found in the Common/Common.cxx sample.

void InitializeSdkObjects(FbxManager*& pSdkManager, FbxScene*& pScene)
{
    // The first thing to do is to create the FBX SDK manager which is the 
    // object allocator for almost all the classes in the SDK.
    pSdkManager = FbxManager::Create();

    if (!pSdkManager)
    {
        printf("Unable to create the FBX SDK manager\n");
        exit(0);
    }

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

        // Load plugins from the executable directory
        FbxString lPath = FbxGetApplicationDirectory();
#if defined(KARCH_ENV_WIN)
        FbxString lExtension = "dll";
#elif defined(KARCH_ENV_MACOSX)
        FbxString lExtension = "dylib";
#elif defined(KARCH_ENV_LINUX)
        FbxString lExtension = "so";
#endif
        pSdkManager->LoadPluginsDirectory(lPath.Buffer(), lExtension.Buffer());

    // Create the entity that will hold the scene.
    pScene = FbxScene::Create(pSdkManager,"");
}

An FBX SDK I/O Plug-in inherits from the FbxPlugin class. Consult the FbxPlugin class documentation for a template FBX SDK I/O Plug-in.

Custom File Writer and Reader Plug-in

A reference to the FbxManager singleton object can be accessed from within your FBX SDK I/O Plug-in with FbxPlugin::GetData().

The FbxManager's I/O plug-in registry can be accessed with FbxManager::GetIOPluginRegistry(). The FbxIOPluginRegistry indexes the FbxReader and FbxWriter objects used to read/write to specific file formats. You can register your own custom subclasses of FbxReader and FbxWriter by calling FbxIOPluginRegistry::RegisterReader() and FbxIOPluginRegistry::RegisterWriter().

The following sample (present in the program below) illustrates how your own FbxReader and FbxWriter can be registered into the FbxManager's FbxIOPluginRegistry:

class MyOwnWriterReaderPlugin : public FbxPlugin
{
    FBXPLUGIN_DECLARE(MyOwnWriterReaderPlugin);

protected:
    explicit MyOwnWriterReaderPlugin(const FbxPluginDefinition& pDefinition, kLibHandle pLibHandle) : FbxPlugin(pDefinition, pLibHandle)
    {
    }

    // Implement Fbxmodules::FbxPlugin
    virtual bool SpecificInitialize()
    {
        int FirstPluginID, RegistredCount;
        GetData().mSDKManager->GetIOPluginRegistry()->RegisterReader(CreateMyOwnReader, GetMyOwnReaderInfo, FirstPluginID, RegistredCount, FillOwnReaderIOSettings);
        GetData().mSDKManager->GetIOPluginRegistry()->RegisterWriter(CreateMyOwnWriter, GetMyOwnWriterInfo, FirstPluginID, RegistredCount, FillOwnWriterIOSettings);
        return true;
    }

    virtual bool SpecificTerminate()
    {
        return true;
    }
};

The Exporting With Embedded Media subsection shows how the FbxIOPluginRegistry can be used to specify the desired file format of a FbxExporter.

NOTE:The MyOwnWriterReader sample provides insight on how to customize your own FbxReader and FbxWriter classes.

Sample FBX SDK I/O Plug-in

The code below shows how to implement a sample FBX SDK I/O Plug-in. It uses the MyOwnWriterReader example to implement custom versions of FbxReader and FbxWriter.

/**************************************************************************************

 Copyright (C) 2001 - 2010 Autodesk, Inc. and/or its licensors.
 All Rights Reserved.

 The coded instructions, statements, computer programs, and/or related material 
 (collectively the "Data") in these files contain unpublished information 
 proprietary to Autodesk, Inc. and/or its licensors, which is protected by 
 Canada and United States of America federal copyright law and by international 
 treaties. 
 
 The Data may not be disclosed or distributed to third parties, in whole or in
 part, without the prior written consent of Autodesk, Inc. ("Autodesk").

 THE DATA IS PROVIDED "AS IS" AND WITHOUT WARRANTY.
 ALL WARRANTIES ARE EXPRESSLY EXCLUDED AND DISCLAIMED. AUTODESK MAKES NO
 WARRANTY OF ANY KIND WITH RESPECT TO THE DATA, EXPRESS, IMPLIED OR ARISING
 BY CUSTOM OR TRADE USAGE, AND DISCLAIMS ANY IMPLIED WARRANTIES OF TITLE, 
 NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR USE. 
 WITHOUT LIMITING THE FOREGOING, AUTODESK DOES NOT WARRANT THAT THE OPERATION
 OF THE DATA WILL BE UNINTERRUPTED OR ERROR FREE. 
 
 IN NO EVENT SHALL AUTODESK, ITS AFFILIATES, PARENT COMPANIES, LICENSORS
 OR SUPPLIERS ("AUTODESK GROUP") BE LIABLE FOR ANY LOSSES, DAMAGES OR EXPENSES
 OF ANY KIND (INCLUDING WITHOUT LIMITATION PUNITIVE OR MULTIPLE DAMAGES OR OTHER
 SPECIAL, DIRECT, INDIRECT, EXEMPLARY, INCIDENTAL, LOSS OF PROFITS, REVENUE
 OR DATA, COST OF COVER OR CONSEQUENTIAL LOSSES OR DAMAGES OF ANY KIND),
 HOWEVER CAUSED, AND REGARDLESS OF THE THEORY OF LIABILITY, WHETHER DERIVED
 FROM CONTRACT, TORT (INCLUDING, BUT NOT LIMITED TO, NEGLIGENCE), OR OTHERWISE,
 ARISING OUT OF OR RELATING TO THE DATA OR ITS USE OR ANY OTHER PERFORMANCE,
 WHETHER OR NOT AUTODESK HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS
 OR DAMAGE. 

**************************************************************************************/
#include "MyOwnReader.h"
#include "MyOwnWriter.h"

#define PLUGIN_NAME		"My_Own_Writer_Reader"
#define PLUGIN_VERSION		"1.0"
#define PLUGIN_EXTENSION	"ABC"

// Create your own writer.
// And your writer will get a pPluginID and pSubID. 
FbxWriter* CreateMyOwnWriter(FbxManager& pManager, FbxExporter& pExporter, int pSubID, int pPluginID)
{
	FbxWriter* lWriter = FbxSdkNew< MyOwnWriter >(pManager, pPluginID);
	lWriter->SetIOSettings(pExporter.GetIOSettings());
	return lWriter;
}

// Get extension, description or version info about MyOwnWriter
void* GetMyOwnWriterInfo(FbxWriter::KInfoRequest pRequest, int pId)
{
    static char const* sExt[] = {PLUGIN_EXTENSION, 0};
    static char const* sDesc[] = {PLUGIN_NAME" Writer", 0};

    switch( pRequest )
    {
		case FbxWriter::eInfoExtension:	return sExt;
		case FbxWriter::eInfoDescriptions:	return sDesc;
		case FbxWriter::eInfoVersions:		return 0;
		default:							return 0;
    }
}

void FillOwnWriterIOSettings(FbxIOSettings& pIOS)
{
    // Here you can write your own FbxIOSettings and parse them.
    FbxProperty FBXExtentionsSDKGroup = pIOS.GetProperty(EXP_FBX_EXT_SDK_GRP);
    if( !FBXExtentionsSDKGroup.IsValid() ) return;

    FbxProperty IOPluginGroup = pIOS.AddPropertyGroup(FBXExtentionsSDKGroup, PLUGIN_NAME, DTString, PLUGIN_NAME);
    if( IOPluginGroup.IsValid() )
    {
        //Add your plugin export options here...
        //Example:
        bool Default_True = true;
        pIOS.AddProperty(IOPluginGroup, "Test", DTBool, "Test", &Default_True, eBOOL1);
    }
}


// Creates a MyOwnReader in the Sdk Manager
FbxReader* CreateMyOwnReader(FbxManager& pManager, FbxImporter& pImporter, int pSubID, int pPluginID)
{
	FbxReader* lReader = FbxSdkNew< MyOwnReader >(pManager, pPluginID);
	lReader->SetIOSettings(pImporter.GetIOSettings());
	return lReader;
}

// Get extension, description or version info about MyOwnReader
void *GetMyOwnReaderInfo(FbxReader::KInfoRequest pRequest, int pId)
{
    static char const* sExt[] = {PLUGIN_EXTENSION, 0};
    static char const* sDesc[] = {PLUGIN_NAME" Reader", 0};

    switch( pRequest )
    {
		case FbxReader::eInfoExtension:	return sExt;
		case FbxReader::eInfoDescriptions:	return sDesc;
		default:							return 0;
    }
}

void FillOwnReaderIOSettings(FbxIOSettings& pIOS)
{    
    // Here you can write your own FbxIOSettings and parse them.
    FbxProperty FBXExtentionsSDKGroup = pIOS.GetProperty(IMP_FBX_EXT_SDK_GRP);
    if( !FBXExtentionsSDKGroup.IsValid() ) return;

    FbxProperty IOPluginGroup = pIOS.AddPropertyGroup(FBXExtentionsSDKGroup, PLUGIN_NAME, DTString, PLUGIN_NAME);
    if( IOPluginGroup.IsValid() )
    {
        //Add your plugin import options here...
        //Example:
        bool Default_True = true;
        pIOS.AddProperty(IOPluginGroup, "Test", DTBool, "Test", &Default_True, eBOOL1);
    }
}


class MyOwnWriterReaderPlugin : public FbxPlugin
{
    FBXPLUGIN_DECLARE(MyOwnWriterReaderPlugin);

protected:
    explicit MyOwnWriterReaderPlugin(const FbxPluginDefinition& pDefinition, kLibHandle pLibHandle) : FbxPlugin(pDefinition, pLibHandle)
    {
    }

    // Implement Fbxmodules::FbxPlugin
    virtual bool SpecificInitialize()
    {
        int FirstPluginID, RegistredCount;
        GetData().mSDKManager->GetIOPluginRegistry()->RegisterReader(CreateMyOwnReader, GetMyOwnReaderInfo, FirstPluginID, RegistredCount, FillOwnReaderIOSettings);
        GetData().mSDKManager->GetIOPluginRegistry()->RegisterWriter(CreateMyOwnWriter, GetMyOwnWriterInfo, FirstPluginID, RegistredCount, FillOwnWriterIOSettings);
        return true;
    }

    virtual bool SpecificTerminate()
    {
        return true;
    }
};

FBXPLUGIN_IMPLEMENT(MyOwnWriterReaderPlugin);

// FBX Interface
extern "C"
{
    //The DLL is owner of the plug-in
    static MyOwnWriterReaderPlugin* sPlugin = NULL;

    //This function will be called when an application will request the plug-in
#ifdef KARCH_ENV_WIN
	__declspec(dllexport) void FBXPluginRegistration(FbxPluginContainer& pContainer, kLibHandle pLibHandle)
#else
	void FBXPluginRegistration(FbxPluginContainer& pContainer, kLibHandle pLibHandle)
#endif
    {
        if( sPlugin == NULL )
        {
            //Create the plug-in definition which contains the information about the plug-in
            FbxPluginDefinition sPluginDef;
            sPluginDef.mName = PLUGIN_NAME" Plugin";
            sPluginDef.mVersion = PLUGIN_VERSION;

            //Create an instance of the plug-in.  The DLL has the ownership of the plug-in
            sPlugin = MyOwnWriterReaderPlugin::Create(sPluginDef, pLibHandle);

            //Register the plug-in
            pContainer.Register(*sPlugin);
        }
    }
}