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 KFbxSdkManager::LoadPluginsDirectory() method. The following code can be found in the Common/Common.cxx sample.

void InitializeSdkObjects(KFbxSdkManager*& pSdkManager, KFbxScene*& 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 = KFbxSdkManager::Create();

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

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

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

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

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

Custom File Writer and Reader Plug-in

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

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

The following sample (present in the program below) illustrates how your own KFbxReader and KFbxWriter can be registered into the KFbxSdkManager's KFbxIOPluginRegistry:

class MyOwnWriterReaderPlugin : public KFbxPlugin
{
    KFBXPLUGIN_DECLARE(MyOwnWriterReaderPlugin);

protected:
    explicit MyOwnWriterReaderPlugin(const KFbxPluginDefinition& pDefinition, kLibHandle pLibHandle) : KFbxPlugin(pDefinition, pLibHandle)
    {
    }

    // Implement kfbxmodules::KFbxPlugin
    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 KFbxIOPluginRegistry can be used to specify the desired file format of a KFbxExporter.

NoteThe MyOwnWriterReader sample provides insight on how to customize your own KFbxReader and KFbxWriter 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 KFbxReader and KFbxWriter.

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

 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. 
KFbxWriter* CreateMyOwnWriter(KFbxSdkManager& pManager, KFbxExporter& pExporter, int pSubID, int pPluginID)
{
	KFbxWriter* lWriter = FbxSdkNew< MyOwnWriter >(pManager, pPluginID);
	lWriter->SetIOSettings(pExporter.GetIOSettings());
	return lWriter;
}

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

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

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

    KFbxProperty 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
KFbxReader* CreateMyOwnReader(KFbxSdkManager& pManager, KFbxImporter& pImporter, int pSubID, int pPluginID)
{
	KFbxReader* lReader = FbxSdkNew< MyOwnReader >(pManager, pPluginID);
	lReader->SetIOSettings(pImporter.GetIOSettings());
	return lReader;
}

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

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

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

    KFbxProperty 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 KFbxPlugin
{
    KFBXPLUGIN_DECLARE(MyOwnWriterReaderPlugin);

protected:
    explicit MyOwnWriterReaderPlugin(const KFbxPluginDefinition& pDefinition, kLibHandle pLibHandle) : KFbxPlugin(pDefinition, pLibHandle)
    {
    }

    // Implement kfbxmodules::KFbxPlugin
    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;
    }
};

KFBXPLUGIN_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(KFbxPluginContainer& pContainer, kLibHandle pLibHandle)
#else
	void FBXPluginRegistration(KFbxPluginContainer& pContainer, kLibHandle pLibHandle)
#endif
    {
        if( sPlugin == NULL )
        {
            //Create the plug-in definition which contains the information about the plug-in
            KFbxPluginDefinition 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);
        }
    }
}