Interactive/ApfToFbx.py

# (C) Copyright 2010 Autodesk, Inc.  All rights reserved.
#
# Use of this software is subject to the terms of the Autodesk license
# agreement provided at the time of installation or download, or which
# otherwise accompanies this software in either electronic or hard copy
# form.

## Find all APF files in the directory where this script is.  For each one
## export a matching FBX.  Log the results to logfile.txt in the same
## directory.

from Modes              import SelectionMode
from ScriptRunner       import ScriptRunner

import os.path, glob, time

class ConvertApfToFbx( ScriptRunner ):
    def __init__( self, scriptName, args ):
        ScriptRunner.__init__( self, scriptName, args )
        self.myDir = os.path.dirname(scriptName)
        self.myLog = None
        self.myFileNum = 0
        
    def Main( self ):
        self.myLog = open( os.path.join(self.myDir,"logfile.txt"), "w" )
        for apfName in glob.glob( os.path.join(self.myDir, "*.apf" ) ):
            fbxName = self.fbxToConvert( apfName )
            if fbxName:
                self.fbxCreate( apfName, fbxName )
                
        self.myLog.write("Found %s .APF file(s) in %s." % (self.myFileNum, self.myDir))
        print "Found %s .APF file(s) in %s." % (self.myFileNum, self.myDir)
        
        self.myLog.close()
        
    def write( self, msg ):
        if self.myLog is not None:
            self.myLog.write( "%s\n" % (msg) )
            self.myLog.flush()
        
        print msg
        
    def fbxToConvert( self, apfName ):
        (fbxName, extension) = os.path.splitext(apfName)
        fbxName = fbxName + ".fbx"
        self.myFileNum+=1

        # TODO: If the file exists and the timestamp is
        # newer, do not bother.
        return fbxName
        

    def fbxCreate( self, apfName, fbxName ):
        timeStart = time.time()
        self.write( "Processing %s" % (apfName) )
        self.write( "  FBX %s" % (fbxName) )
        self.sendMessageSync( 'APPLICATION_NEW_SCENE', () )

        self.write( "  Importing %s" % (apfName) )
        self.sendMessage_( 'MODEL_IMPORT', ( '', apfName, True ) )
        self.write( "  Import completed for %s" % (apfName) )

        self.sendMessage_( 'SELECT', ( (), SelectionMode.kReplace ) )
        self.sendMessage_( 'REFRESH', () )
        self.sendMessage_( 'FIT_TO_VIEW', ( False, ) )

        self.write( "  Exporting %s" % (fbxName) )

        # Not all options apply to FBX export!
        exportHow = 0

        #exportHow += 1 # convert to Y-up
        #exportHow += 2 # export hidden
        #exportHow += 4 # export all LOD
        #exportHow += 8 # export as ascii
        #exportHow += 16 # export patches
        #exportHow += 32 # export empty groups
        #exportHow += 64 # export spline data
        #exportHow += 128 # export alternatives
        #exportHow += 256 # export for Maya compatibility
        exportHow += 512 # merge combined into a single node
        #exportHow += 1024 # include all material textures
        exportHow += 2048 # use node labels instead of IDs
        #exportHow += 4096 # export selected only
        #exportHow += 8192 # preserve all patch data
        #exportHow += 16384 # export empty meshes


        self.sendMessageSync( 'APPLICATION_EXPORT_SCENE', ( fbxName, (),
                                                            exportHow, ) )
        self.write( "  Closing %s" % (fbxName) )

        self.sendMessageSync( 'APPLICATION_CLOSE_SCENE', () )
        timeEnd = time.time()
        self.write( "Done in %.1f seconds" % (timeEnd - timeStart) )
        
# ===============================================================
def instantiate( scriptName, args ):
    return ConvertApfToFbx( scriptName, args )
# ===============================================================