Tasks/BatchExportCharacterAnimationTool.py

Tasks/BatchExportCharacterAnimationTool.py
1 # Copyright 2009 Autodesk, Inc. All rights reserved.
2 # Use of this software is subject to the terms of the Autodesk license agreement
3 # provided at the time of installation or download, or which otherwise accompanies
4 # this software in either electronic or hard copy form.
5 #
6 # Script description:
7 # Specify a directory where all fbx files will be loaded and all Character rig and Animation will be exported.
8 #
9 # Topic:FBFolderPopup, FBSystem, FBApplication, FBApplication.FileOpen, FBMessageBox, FBFbxOptions
10 #
11 #
12 
13 import os
14 import re
15 from pyfbsdk import FBFolderPopup, FBSystem, FBApplication, FBMessageBox, FBFbxOptions
16 
17 # Creating all the Constructors
18 lFp = FBFolderPopup()
19 lSystem = FBSystem()
20 lApp = FBApplication()
21 lScene = lSystem.Scene
22 fbxList = []
23 
24 # Select directory you would like to load your FBX files in from
25 # Create the popup and set necessary initial values.
26 lFp.Caption = "Source Files: Select the folder containing the files you would like to export"
27 
28 # Set the default path. Good for a PC only... will have to be different for Mac.
29 lFp.Path = r"C:\Autodesk"
30 
31 # Get the GUI to show.
32 lRes = lFp.Execute()
33 
34 # If you select a folder, show its name, otherwise indicate that the selection was canceled.
35 if not lRes:
36  FBMessageBox( "Warning:", "Selection canceled, cannot continue!", "OK" )
37 
38 else:
39  FBMessageBox( "Selected Folder Path:", "Selected folder:\n Path: '%s'" % lFp.Path, "OK" )
40 
41  # Getting the names of the files in your previously selected folder
42  # Using os to get the file names from the specified folder (above) and storing names of files in a list
43  fileList = os.listdir(lFp.Path)
44  print "fileList", fileList
45  # Setting the regular expression to only look for .fbx extenstion
46  fbxRE = re.compile('^\w+.fbx$', re.I)
47 
48  # Removing any files that do not have an .fbx extenstion
49  for fname in fileList:
50  mo = fbxRE.search(fname)
51  if mo:
52  fbxList.append(fname)
53  # Exporting items in the file one at a time
54  for fname in fbxList:
55 
56  # Opening the file in MotionBuilder, this replaces the current scene
57  lApp.FileOpen(lFp.Path + "\\" + fname, False)
58 
59  # Saves out the character and rig animation
60  # if there are multiple characters per file you will need to change to accommodate.
61  # SaveCharacterRigAndAnimation (str pFileName, FBCharacter pCharacter, bool pSaveCharacter, bool pSaveRig, bool pSaveExtensions)
62  lOptions = FBFbxOptions(False)## save options
63  lOptions.SaveCharacter = False
64  lOptions.SaveControlSet = True
65  lOptions.SaveCharacterExtension = False
66  if len( lScene.Characters ) > 0:
67  lApp.SaveCharacterRigAndAnimation(lFp.Path + "\\Animation_" + fname, lScene.Characters[0], lOptions)
68 
69  # Closing the current file, not really necessarily needed sine the FBApplication::FileOpne replaces the current scene
70  lApp.FileNew()
71 #Cleanup
72 del(lFp, FBFolderPopup, lSystem, FBSystem, lRes, lApp, FBApplication, lScene, fbxList, re, os, FBMessageBox, FBFbxOptions)
73 
74 
75 
76