ComplexTools/AudioTrackSetupTool.py

ComplexTools/AudioTrackSetupTool.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 # MotionBuilder 2009 and Pre-Visualization Demo 2
8 #
9 # Topic: FBAudioClip, FBStoryClip, FBStoryTrack, FBStoryTrackType, FBFilePopup
10 #
11 
12 from pyfbsdk import *
13 from pyfbsdk_additions import *
14 from sys import *
15 import os.path
16 import traceback
17 
18 global lFp
19 lFp = FBFilePopup()
20 
21 #There would need to be more error handling added if an unperfect audio track definition files were added.
22 #There would be to be some error handling if you are trying to put an audio file that is bigger then the assigned frames for it
23 def BrowseCallback(control, event):
24  #OPENING AND FILE READING
25  #Create the popup and set necessary initial values.
26  lFp.Caption = "Select the audio track definition file:"
27  lFp.Style = FBFilePopupStyle.kFBFilePopupOpen
28  lFp.Filter = "*.txt" # BUG: If we do not set the filter, we will have an exception.
29  if os.path.isdir(os.path.splitext(traceback.extract_stack()[-1][0])[0]):
30  lFp.Path = os.path.normpath(os.path.splitext(traceback.extract_stack()[-1][0])[0])
31  else:
32  lFp.Path = os.path.normpath(os.path.dirname(traceback.extract_stack()[-1][0]))
33  lRes = lFp.Execute() # Get the GUI to show.
34 
35  if lRes: #Check to see if a file was set, if so set the edit box to the file path and name
36  eButt.Text = os.path.join(lFp.Path,lFp.FileName) #set up the edit box to contain the path
37 
38 def SetupCallback(control, event):
39  #Check to see if a valid file was selected.
40  try:
41  f=open(os.path.join(lFp.Path,lFp.FileName), 'r') #Open the Audio Track Definition File
42  shotInfoFile = f.readlines() #Read all the content of Audio Track Definition File
43  f.close() #Close the file that you were reading
44  lTrackAudio = FBStoryTrack(FBStoryTrackType.kFBStoryTrackAudio) #add a new Audio track to the Story tool
45  shotCnt = len(shotInfoFile) #Find out how many lines were in the file
46  counter = 1
47  lastClipEnd = 0
48  for shotInfo in shotInfoFile:
49  #PARSING THE STRINGS
50  shots = shotInfo.split() # Spitting the lines in the file
51  shotName = shots[0]
52  shotStart = shots[1]
53  shotEnd = shots[2]
54  iShotStart = int(shotStart)
55  iShotEnd = int(shotEnd)
56  if counter < 10:
57  audioPath = os.path.join(lFp.Path,"Audio_Clip_0" + str(counter) + ".wav")
58  else:
59  audioPath = os.path.join(lFp.Path,"Audio_Clip_" + str(counter) + ".wav")
60 
61  lAudio = FBAudioClip(audioPath)
62  lClip = FBStoryClip (lAudio, lTrackAudio, FBTime(0,0,0,iShotStart))
63  if lastClipEnd<iShotEnd: lastClipEnd=iShotEnd
64  counter = counter + 1
65  FBPlayerControl().LoopStop=FBTime(0,0,0,lastClipEnd,0)## Set the end of the player control at the end of the last clip
66 
67  except IOError:
68  FBMessageBox( "Cannot Continue", "Unable to read the audio track definition file you selected or you have not selected one.", "OK", None, None )
69  exit
70 
71 def CloseCallback(control, event):
72  FBDestroyTool(t)
73 
74 def PopulateLayout(mainLyt):
75 
76  #1. Creating the instruction title
77  l = FBLabel()
78  l.Caption = "Select the audio track definition file to use:"
79  l.Style = FBTextStyle.kFBTextStyleBold
80  l.WordWrap = True
81 
82  x = FBAddRegionParam(0,FBAttachType.kFBAttachLeft,"") # Create a label that is left justify
83  y = FBAddRegionParam(5,FBAttachType.kFBAttachTop,"") # ... and at the top of the layout.
84  w = FBAddRegionParam(300,FBAttachType.kFBAttachNone,"") # its width is fixed at 300 pixels
85  h = FBAddRegionParam(20,FBAttachType.kFBAttachNone,"") # and its height is fixed at 15 pixels
86 
87  mainLyt.AddRegion("lab","lab", x, y, w, h)
88  mainLyt.SetControl("lab",l)
89 
90  #2. Creating a temp button for a place holder
91  e = FBEdit()
92  e.ReadOnly = True
93  e.Text = ""
94 
95  x2 = FBAddRegionParam(5,FBAttachType.kFBAttachLeft,"")
96  y2 = FBAddRegionParam(5,FBAttachType.kFBAttachBottom,"lab")
97  w2 = FBAddRegionParam(220,FBAttachType.kFBAttachNone,"")
98  h2 = FBAddRegionParam(20,FBAttachType.kFBAttachNone,"")
99 
100  mainLyt.AddRegion("path","path", x2, y2, w2, h2)
101  mainLyt.SetControl("path",e)
102 
103  #3. Creating a the browse button
104  b2 = FBButton()
105  b2.Caption = "Browse..."
106  b2.Justify = FBTextJustify.kFBTextJustifyCenter
107 
108  x3 = FBAddRegionParam(225,FBAttachType.kFBAttachLeft,"path")
109  y3 = FBAddRegionParam(5,FBAttachType.kFBAttachBottom,"lab")
110  w3 = FBAddRegionParam(57,FBAttachType.kFBAttachNone,"")
111  h3 = FBAddRegionParam(20,FBAttachType.kFBAttachNone,"")
112 
113  mainLyt.AddRegion("but","but", x3, y3, w3, h3)
114  mainLyt.SetControl("but",b2)
115 
116  global eButt #setting the FBEdit button global so it can be set from the function BrowseCallback
117  eButt = e
118  b2.OnClick.Add(BrowseCallback)
119 
120  #4. Creating the button the automatically does everything
121  b3 = FBButton()
122  b3.Caption = "Setup Audio"
123  b3.Justify = FBTextJustify.kFBTextJustifyCenter
124 
125  x4 = FBAddRegionParam(40,FBAttachType.kFBAttachLeft,"")
126  y4 = FBAddRegionParam(12,FBAttachType.kFBAttachBottom,"path")
127  w4 = FBAddRegionParam(100,FBAttachType.kFBAttachNone,"")
128  h4 = FBAddRegionParam(25,FBAttachType.kFBAttachNone,"")
129 
130  mainLyt.AddRegion("audio","audio", x4, y4, w4, h4)
131  mainLyt.SetControl("audio",b3)
132  b3.OnClick.Add(SetupCallback)
133 
134  #5. Creating a close
135  b4 = FBButton()
136  b4.Caption = "Close"
137  b4.Justify = FBTextJustify.kFBTextJustifyCenter
138 
139  x5 = FBAddRegionParam(20,FBAttachType.kFBAttachRight,"audio")
140  y5 = FBAddRegionParam(12,FBAttachType.kFBAttachBottom,"path")
141  w5 = FBAddRegionParam(100,FBAttachType.kFBAttachNone,"")
142  h5 = FBAddRegionParam(25,FBAttachType.kFBAttachNone,"")
143 
144  mainLyt.AddRegion("close","close", x5, y5, w5, h5)
145  mainLyt.SetControl("close",b4)
146  b4.OnClick.Add(CloseCallback)
147 
148 def CreateTool():
149  # Tool creation will serve as the hub for all other controls
150  global t
151  t = FBCreateUniqueTool("Audio Track Setup Tool")
152  t.StartSizeX = 300
153  t.StartSizeY = 123
154  PopulateLayout(t)
155  ShowTool(t)
156 
157 CreateTool()