ComplexTools/ShotTrackSetupTool.py

ComplexTools/ShotTrackSetupTool.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 1
8 #
9 # Topic: FBPlayerControl, FBLayout, FBConfigFile
10 #
11 
12 from pyfbsdk import *
13 from pyfbsdk_additions import *
14 from sys import *
15 import os
16 import traceback
17 
18 global lFp
19 lFp = FBFilePopup()
20 
21 #There would need to be more error handling added if an unperfect shot track definition files were added.
22 
23 def BrowseCallback(control, event):
24  #OPENING AND FILE READING
25  #Create the popup and set necessary initial values.
26 
27  lFp.Caption = "Select the shot track definition file:"
28  lFp.Style = FBFilePopupStyle.kFBFilePopupOpen
29  lFp.Filter = "*.txt" # BUG: If we do not set the filter, we will have an exception.
30  if os.path.isdir(os.path.splitext(traceback.extract_stack()[-1][0])[0]):
31  lFp.Path = os.path.normpath(os.path.splitext(traceback.extract_stack()[-1][0])[0])
32  else:
33  lFp.Path = os.path.normpath(os.path.dirname(traceback.extract_stack()[-1][0]))
34  ## Set the default path to the script folder
35  lRes = lFp.Execute() # Get the GUI to show.
36 
37  if lRes: # if a file was selected set the FBEdit button to the values
38  eButt.Text = os.path.join(lFp.Path,lFp.FileName) #set up the edit box to contain the path
39 
40 def SetupCallback(control, event):
41  try:
42  f=open(os.path.join(lFp.Path,lFp.FileName), 'r') #Open Shot Track Definition File
43  shotInfoFile = f.readlines() #Read all the content of Shot Track Definition File
44  f.close() #Close the file that you were reading
45 
46  shotCnt = len(shotInfoFile) #Find out how many lines were in the file
47  counter = 1
48  for shotInfo in shotInfoFile:
49  #PARSING THE STRINGS
50  shots = shotInfo.split() # Spitting the lines in the file
51  (shotName,shotStart,shotEnd) = shots
52  iShotStart = int(shotStart)
53  iShotEnd = int(shotEnd)
54  if shotInfoFile[0] == shotInfo:
55  completeShotStart = iShotStart # Capture the final start frame of all the clips for setting the Transport Controls later
56 
57  if shotInfoFile[shotCnt - 1] == shotInfo:
58  completeShotEnd = iShotEnd # Capture the final end frame of all the clips for setting the Transport Controls later
59 
60  #CAMERA AND INTERST, Create a camera, named the same as the label in the definition
61  lCam = FBCamera (shotName)
62  lCam.Visible = True
63  lCam.Show = True
64  lNull = FBModelNull (shotName + "_INT") #Create a Camera Interest, named the same as the label in the definition with _INT appended
65  lCam.Interest = lNull #Link the null to be the camera interest.
66 
67  #STORY
68  #In Story you need to create the clips
69  lTrackContainer = FBStory().RootEditFolder.Tracks
70  lastTrack=0
71  lTrack=None
72  for Track in lTrackContainer:
73  if Track.Label =="Shot Track":
74  lTrack=Track
75  clipStart = FBTime(0,0,0,iShotStart)
76  if lTrack:
77  lClip = FBStoryClip (lCam, lTrack, clipStart)
78  lClip.Start = FBTime(0,0,0,iShotStart) #Set the correct start and end frame from the shot track definition file.
79  lClip.Stop = FBTime(0,0,0,iShotEnd)
80  if lastTrack<iShotEnd: lastTrack=iShotEnd
81 
82  #Setting up the backplates if the user chooses to
83  if backPlate.State == 1:
84  if counter < 10:
85  vidPath = os.path.join(lFp.Path, "shot0"+str(counter)+".avi")
86  else:
87  vidPath = os.path.normpath(lFp.Path+"shot" + str(counter) +".avi")
88  lVid = FBVideoClip(vidPath)
89  if lVid.IsValid():
90  lTex = FBTexture("bg")
91  lTex.Video = lVid
92  lCam.BackGroundTexture = lTex #linking the backplate to the camera
93  counter = counter + 1
94 
95  FBPlayerControl().LoopEnd=FBTime(0,0,0,lastTrack,0)
96  #TRANSPORT CONTROLS
97  #You need to set up the Transport Controls so it goes from the start of the Shot Track to the End.
98  #When you save your scene, this is not remembered, bug 313059 :(
99  lPlayer = FBPlayerControl()
100  lPlayer.LoopStart = FBTime(0,0,0,completeShotStart)
101  lPlayer.ZoomWindowStart = FBTime(0,0,0,completeShotStart)
102  lPlayer.LoopStop = FBTime(0,0,0,completeShotEnd)
103  lPlayer.ZoomWindowStop = FBTime(0,0,0,completeShotEnd)
104 
105  #####################################
106  #This is purely code for demoing
107 
108  lModel = FBFindModelByLabelName('SHOT_001')
109  lModel.Translation = FBVector3d(-10000, 145, 300)
110 
111  lModel = FBFindModelByLabelName('SHOT_001_INT')
112  lModel.Translation = FBVector3d(-10000, 0, 0)
113 
114  lModel = FBFindModelByLabelName('SHOT_002')
115  lModel.Translation = FBVector3d(-5000, 145, 300)
116 
117  lModel = FBFindModelByLabelName('SHOT_002_INT')
118  lModel.Translation = FBVector3d(-5000, 0, 0)
119 
120  lModel = FBFindModelByLabelName('SHOT_003')
121  lModel.Translation = FBVector3d(0, 145, 300)
122 
123  lModel = FBFindModelByLabelName('SHOT_003_INT')
124  lModel.Translation = FBVector3d(0, 0, 0)
125 
126  lModel = FBFindModelByLabelName('SHOT_004')
127  lModel.Translation = FBVector3d(5000, 145, 300)
128 
129  lModel = FBFindModelByLabelName('SHOT_004_INT')
130  lModel.Translation = FBVector3d(5000, 0, 0)
131 
132  lModel = FBFindModelByLabelName('SHOT_005')
133  lModel.Translation = FBVector3d(10000, 145, 300)
134 
135  lModel = FBFindModelByLabelName('SHOT_005_INT')
136  lModel.Translation = FBVector3d(10000, 0, 0)
137 
138  lCameras = FBSystem().Scene.Cameras
139 
140  for lCam in lCameras:
141  if lCam.Name == "Producer Perspective":
142  lCam.FarPlaneDistance = 40000
143  break
144  FBApplication().SwitchViewerCamera(FBCameraSwitcher().CurrentCamera)
145 
146  except IOError:
147  FBMessageBox( "Cannot Continue", "Unable to read the shot track definition file you selected or you have not selected one.", "OK", None, None )
148  exit
149 
150 def PopulateLayout(mainLyt):
151  #1. Creating the instruction title
152  l = FBLabel()
153  l.Caption = "Select the shot track definition file to use:"
154  l.Style = FBTextStyle.kFBTextStyleBold
155  l.WordWrap = True
156 
157  x = FBAddRegionParam(5,FBAttachType.kFBAttachLeft,"") # Create a label that is left justify
158  y = FBAddRegionParam(5,FBAttachType.kFBAttachTop,"") # ... and at the top of the layout.
159  w = FBAddRegionParam(300,FBAttachType.kFBAttachNone,"") # its width is fixed at 300 pixels
160  h = FBAddRegionParam(20,FBAttachType.kFBAttachNone,"") # and its height is fixed at 15 pixels
161 
162  mainLyt.AddRegion("lab","lab", x, y, w, h)
163  mainLyt.SetControl("lab",l)
164 
165  #2. Creating a temp button for a place holder
166  e = FBEdit()
167  e.ReadOnly = True
168  e.Text = ""
169 
170  x2 = FBAddRegionParam(5,FBAttachType.kFBAttachLeft,"")
171  y2 = FBAddRegionParam(5,FBAttachType.kFBAttachBottom,"lab")
172  w2 = FBAddRegionParam(220,FBAttachType.kFBAttachNone,"")
173  h2 = FBAddRegionParam(20,FBAttachType.kFBAttachNone,"")
174 
175  mainLyt.AddRegion("path","path", x2, y2, w2, h2)
176  mainLyt.SetControl("path",e)
177 
178  #3. Creating a the browse button
179  b2 = FBButton()
180  b2.Caption = "Browse..."
181  b2.Justify = FBTextJustify.kFBTextJustifyCenter
182 
183  x3 = FBAddRegionParam(225,FBAttachType.kFBAttachLeft,"path")
184  y3 = FBAddRegionParam(5,FBAttachType.kFBAttachBottom,"lab")
185  w3 = FBAddRegionParam(57,FBAttachType.kFBAttachNone,"")
186  h3 = FBAddRegionParam(20,FBAttachType.kFBAttachNone,"")
187 
188  mainLyt.AddRegion("but","but", x3, y3, w3, h3)
189  mainLyt.SetControl("but",b2)
190 
191  global eButt #creating The FBEdit button as global so it can be set in the function BrowseCallback
192  eButt = e
193  b2.OnClick.Add(BrowseCallback)
194 
195  #4. Creating the 'create backplate' checkbox
196  cb = FBButton()
197  cb.Style = FBButtonStyle.kFBCheckbox
198  cb.Caption = "Add backplates to each shot"
199 
200  x4 = FBAddRegionParam(60, FBAttachType.kFBAttachLeft, "")
201  y4 = FBAddRegionParam(5, FBAttachType.kFBAttachBottom, "path")
202  w4 = FBAddRegionParam(300,FBAttachType.kFBAttachNone,"")
203  h4 = FBAddRegionParam(25,FBAttachType.kFBAttachNone,"")
204 
205  mainLyt.AddRegion("backp", "backp",x4,y4,w4,h4)
206  mainLyt.SetControl("backp",cb)
207 
208  global backPlate #Create this global so the setupCallback can find the result of the checkbox
209  backPlate = cb
210 
211  #5. Creating the button the automatically does everything
212  b3 = FBButton()
213  b3.Caption = "Setup Shots"
214  b3.Justify = FBTextJustify.kFBTextJustifyCenter
215 
216  x5 = FBAddRegionParam(40,FBAttachType.kFBAttachLeft,"")
217  y5 = FBAddRegionParam(5,FBAttachType.kFBAttachBottom,"backp")
218  w5 = FBAddRegionParam(100,FBAttachType.kFBAttachNone,"")
219  h5 = FBAddRegionParam(25,FBAttachType.kFBAttachNone,"")
220 
221  mainLyt.AddRegion("shots","shots", x5, y5, w5, h5)
222  mainLyt.SetControl("shots",b3)
223  b3.OnClick.Add(SetupCallback)
224 
225  x6 = FBAddRegionParam(20,FBAttachType.kFBAttachRight,"shots")
226  y6 = FBAddRegionParam(5,FBAttachType.kFBAttachBottom,"backp")
227  w6 = FBAddRegionParam(100,FBAttachType.kFBAttachNone,"")
228  h6 = FBAddRegionParam(25,FBAttachType.kFBAttachNone,"")
229 
230 def CreateTool():
231  # Tool creation will serve as the hub for all other controls
232  global t
233  t = FBCreateUniqueTool("Shot Track Setup Tool")
234  t.StartSizeX = 300
235  t.StartSizeY = 143
236 
237  PopulateLayout(t)
238  ShowTool(t)
239 
240 CreateTool()