Tasks/SaveOneTakePerFile.py
 
 
 
Tasks/SaveOneTakePerFile.py
# Copyright 2009 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.
#
# Topic: FBTake, FBApplication, FBFileDlg, 
#

from pyfbsdk import FBSystem, FBApplication, FBFilePopup, FBFilePopupStyle, FBFbxOptions

#Utility function to get the name of a file without extension
def getFileName(f):
    import os
    d, filename = os.path.split(f)
    if filename:
        return os.path.splitext(filename)[0]
    else:
        return "unknown"

# First get some needed objects.
lSystem      = FBSystem()
lApplication = FBApplication()
lFileName    = lApplication.FBXFileName
if lFileName == "":## If no FBX file has been loaded.
    FileDlg = FBFilePopup()
    FileDlg.Style = FBFilePopupStyle.kFBFilePopupSave
    FileDlg.Caption = "The current filename is not valid."
    FileDlg.FileName = "Untitled.fbx"
    FileDlg.Filter = "*"
    if FileDlg.Execute():
        lFileName = FileDlg.FileName 

# We want to make sure that we have a scene with file name
# already, otherwise we might want to open a file popup.
# For now we assume that there is already a name to use as a
# base name.
if lFileName.upper().endswith( '.FBX' ):
    lOptions = FBFbxOptions(False) 
    lOriginalTake = lSystem.CurrentTake

    # Iterate the list of takes.
    for lTake in lSystem.Scene.Takes:
    
        # Switch the current take to the one we want to save.
        lSystem.CurrentTake = lTake
        
        # Build the file name to use. Here we use the same pattern
        # MotionBuilder would use.
        lTakeFileName = "%s-%s.fbx" % ( getFileName(lFileName), lTake.Name )
        
        # Some feedback for the user...
        print "Saving Take '%s' to file '%s'" % ( lTake.Name, lTakeFileName )

        # Let's save to ASCII format.        
        lOptions.UseASCIIFormat = True
        

        # Go through the list of takes to export to tag only
        # the correct take. All the other are disregarded.
        for index in range(lOptions.GetTakeCount()): ## take index
            if lOptions.GetTakeName(index) == lTake.Name:
                lOptions.SetTakeSelect(index, True)
            else:
                lOptions.SetTakeSelect(index, False)
        lApplication.FileSave( lTakeFileName, lOptions )
        

    # Return the current take to the original.
    lSystem.CurrentTake = lOriginalTake
    
    del( lOriginalTake, lOptions )

else:

    print 'File name does not end with ".fbx". Unable to proceed!'


# Cleanup of local variables.
del( lSystem, lApplication, lFileName )

# Cleanup of imported symbols.
del( FBSystem, FBApplication )