Interactive/FbxBatchConversion.py

##
## (C) Copyright 2012 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 FBX files in the directory where this script is.  Look for
## Newport .A3S files with the same base name.  If not found, create a
## new scene, import the FBX file and save it out as A3S scene.

from ScriptRunner       import ScriptRunner
from Modes              import SelectionMode
from MessageRegistry    import theMessageRegistry

import os.path, glob, time

class ConvertFbxToScene( 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 fbxName in glob.glob( os.path.join(self.myDir, "*.fbx" ) ):
            a3sName = self.a3sToConvert( fbxName )
            if a3sName:
                self.a3sCreate( fbxName, a3sName )

        self.myLog.write("Found %s .FBX file(s) in %s." % (self.myFileNum, self.myDir))
        print "Found %s .FBX 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 a3sToConvert( self, fbxName ):
        (a3sName, extension) = os.path.splitext(fbxName)
        a3sName = a3sName + ".a3s"
        self.myFileNum+=1

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


    def a3sCreate( self, fbxName, a3sName ):
        timeStart = time.time()
        self.write( "Processing %s" % (fbxName) )
        self.write( "  Scene %s" % (a3sName) )
        self.sendMessageSync( 'APPLICATION_NEW_SCENE', () )
        self.switchToEnvironment_( "PhysicalSunAndSky\\SunSky.a3e", True,)
        self.write( "  Importing %s" % (fbxName) )
        self.sendMessage_( 'MODEL_IMPORT', ( '', fbxName, True ) )
        self.write( "  Import completed for %s" % (fbxName) )
        self.sendMessage_( 'SELECT', ( (), SelectionMode.kReplace ) )
        self.sendMessage_( 'REFRESH', () )
        self.sendMessage_( 'FIT_TO_VIEW', ( False, ) )
        if theMessageRegistry.isRegistered( "AUTO_GROUP" ):
            self.write( "  Group and combine %s" % (a3sName) )
            self.sendMessage_( 'AUTO_GROUP', ( True, ) )
        else:
            self.write( "  SKIPPING Group and combine" )
        self.write( "  Saving %s" % (a3sName) )
        self.sendMessageSync( 'APPLICATION_SAVE_SCENE', ( a3sName, ) )
        self.write( "  Closing %s" % (a3sName) )
        self.sendMessageSync( 'APPLICATION_CLOSE_SCENE', () )
        timeEnd = time.time()
        self.write( "Done in %.1f seconds" % (timeEnd - timeStart) )


# ===============================================================
def instantiate( scriptName, args ):
    return ConvertFbxToScene( scriptName, args )
# ===============================================================