Tasks/SaveOneTakePerFile.py

Tasks/SaveOneTakePerFile.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 # Topic: FBTake, FBApplication, FBFileDlg,
7 #
8 
9 from pyfbsdk import FBSystem, FBApplication, FBFilePopup, FBFilePopupStyle, FBFbxOptions
10 
11 #Utility function to get the name of a file without extension
12 def getFileName(f):
13  import os
14  d, filename = os.path.split(f)
15  if filename:
16  return os.path.splitext(filename)[0]
17  else:
18  return "unknown"
19 
20 # First get some needed objects.
21 lSystem = FBSystem()
22 lApplication = FBApplication()
23 lFileName = lApplication.FBXFileName
24 if lFileName == "":## If no FBX file has been loaded.
25  FileDlg = FBFilePopup()
26  FileDlg.Style = FBFilePopupStyle.kFBFilePopupSave
27  FileDlg.Caption = "The current filename is not valid."
28  FileDlg.FileName = "Untitled.fbx"
29  FileDlg.Filter = "*"
30  if FileDlg.Execute():
31  lFileName = FileDlg.FileName
32 
33 # We want to make sure that we have a scene with file name
34 # already, otherwise we might want to open a file popup.
35 # For now we assume that there is already a name to use as a
36 # base name.
37 if lFileName.upper().endswith( '.FBX' ):
38  lOptions = FBFbxOptions(False)
39  lOriginalTake = lSystem.CurrentTake
40 
41  # Iterate the list of takes.
42  for lTake in lSystem.Scene.Takes:
43 
44  # Switch the current take to the one we want to save.
45  lSystem.CurrentTake = lTake
46 
47  # Build the file name to use. Here we use the same pattern
48  # MotionBuilder would use.
49  lTakeFileName = "%s-%s.fbx" % ( getFileName(lFileName), lTake.Name )
50 
51  # Some feedback for the user...
52  print "Saving Take '%s' to file '%s'" % ( lTake.Name, lTakeFileName )
53 
54  # Let's save to ASCII format.
55  lOptions.UseASCIIFormat = True
56 
57 
58  # Go through the list of takes to export to tag only
59  # the correct take. All the other are disregarded.
60  for index in range(lOptions.GetTakeCount()): ## take index
61  if lOptions.GetTakeName(index) == lTake.Name:
62  lOptions.SetTakeSelect(index, True)
63  else:
64  lOptions.SetTakeSelect(index, False)
65  lApplication.FileSave( lTakeFileName, lOptions )
66 
67 
68  # Return the current take to the original.
69  lSystem.CurrentTake = lOriginalTake
70 
71  del( lOriginalTake, lOptions )
72 
73 else:
74 
75  print 'File name does not end with ".fbx". Unable to proceed!'
76 
77 
78 # Cleanup of local variables.
79 del( lSystem, lApplication, lFileName )
80 
81 # Cleanup of imported symbols.
82 del( FBSystem, FBApplication )