ConvertScene/main.cxx

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

 Copyright (C) 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. 

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

//
// This program converts any file in a format supported by the FBX SDK 
// into DAE, FBX, 3DS, OBJ and DXF files. 
//
// Steps:
// 1. Initialize SDK objects.
// 2. Load a file(fbx, obj,...) to a FBX scene.
// 3. Create a exporter.
// 4. Retrieve the writer ID according to the description of file format.
// 5. Initialize exporter with specified file format
// 6. Export.
// 7. Destroy the exporter
// 8. Destroy the FBX SDK manager
//

#include "../Common/Common.h"

#include <fbxfilesdk/kfbxio/kfbxio.h>
#include <fbxfilesdk/kfbxio/kfbxexporter.h>
#include <fbxfilesdk/kfbxio/kfbximporter.h>

#include <fbxfilesdk/kfbxplugins/kfbxsdkmanager.h>
#include <fbxfilesdk/kfbxplugins/kfbxscene.h>
#include <fbxfilesdk/kfbxplugins/kfbxutilities.h>

#include <fbxfilesdk/components/kbaselib/klib/kerror.h>
#include <fbxfilesdk/components/kbaselib/klib/kstring.h>

#include <fbxfilesdk/fbxfilesdk_nsuse.h>

char* lFileTypes[] =
{
    ".dae",            "Collada DAE (*.dae)",
    "_fbx7binary.fbx", "FBX binary (*.fbx)",
    "_fbx7ascii.fbx",  "FBX ascii (*.fbx)",
    "_fbx6binary.fbx", "FBX 6.0 binary (*.fbx)",
    "_fbx6ascii.fbx",  "FBX 6.0 ascii (*.fbx)",
    ".obj",            "Alias OBJ (*.obj)",
    ".dxf",            "AutoCAD DXF (*.dxf)"
};

int main(int argc, char** argv)
{
    char* lFileName = "box.fbx";
    if(argc == 2)
        lFileName = argv[1];

    KFbxSdkManager* lSdkManager = NULL;
    KFbxScene* lScene = NULL;

    // Prepare the FBX SDK.
    InitializeSdkObjects(lSdkManager, lScene);

    bool lResult = LoadScene(lSdkManager, lScene, lFileName);

    if(lResult)
    {
        const size_t lFileNameLength = strlen(lFileName);
        char* lNewFileName = new char[lFileNameLength+64];
        strcpy(lNewFileName,lFileName);

        const size_t lFileTypeCount = sizeof(lFileTypes)/sizeof(lFileTypes[0])/2;

        for(size_t i=0; i<lFileTypeCount; ++i)
        {
            // Retrieve the writer ID according to the description of file format.
            int lFormat = lSdkManager->GetIOPluginRegistry()->FindWriterIDByDescription(lFileTypes[i*2+1]);

            // Construct the output file name.
            strcpy(lNewFileName+lFileNameLength-4, lFileTypes[i*2]);

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

            // Initialize the exporter.
            if (!lExporter->Initialize(lNewFileName, lFormat, lSdkManager->GetIOSettings()))
            {
                printf("%s:\tCall to KFbxExporter::Initialize() failed.\n", lFileTypes[i*2+1]);
                printf("Error returned: %s\n\n", lExporter->GetLastErrorString());
            }
            else
            {
                // Export the scene.
                if (!lExporter->Export(lScene))
                {
                    printf("Call to KFbxExporter::Export() failed.\n");
                }
            }

            // Destroy the exporter.
            lExporter->Destroy();
        }
        delete[] lNewFileName;
    }
    else
    {
        printf("Call to LoadScene() failed.\n");
    }

    // Delete the FBX SDK manager. All the objects that have been allocated 
    // using the FBX SDK manager and that haven't been explicitly destroyed 
    // are automatically destroyed at the same time.
    lSdkManager->Destroy();
    return 0;
}