Navigation Tool
Posted On April 3, 2017
In this post I will show how I created a Navigation tool with Python in MotionBuilder.
The Tool will use Python to do the following:
- Start and Pause the play back of the current take.
- Jump to the end of the current take’s time line.
- Move to the start of the current take’s time line.
- Set the current frame as the time line’s start frame .
- Set the current frame as the time line’s end frame.
- Go to the previous take.
- Go to the next take.
You can just copy and paste the code into your MotionBuilder Python Consol:
'''================================= | Written by Victor DeBaie | | http://www.vicdebaie.com/blog/ | | vic_debaie@hotmail.com | =================================''' from pyfbsdk import * from pyfbsdk_additions import * gotostartframe = FBButton() setplay = FBButton() gotoendframe = FBButton() setstartframe = FBButton() setendframe = FBButton() previoustake = FBButton() nexttake = FBButton() playback = FBPlayerControl() #Goto Start Frame ------------------------------------------------------------ def Btngotostartframe(control, event): FBPlayerControl().GotoStart() print "Goto Start Frame" #Play ------------------------------------------------------------ def BtnCallsetplay(control, event): playback.Stop() if playback.IsPlaying else playback.Play() print "Play/Paue" #Goto End Frame ------------------------------------------------------------ def Btngotoendframe(control, event): FBPlayerControl().GotoEnd() print "Goto End Frame" #Functions Starts Here ------------------------------------------------------------ def getCurrentTimeSpan(): curtimespan = FBSystem().CurrentTake.LocalTimeSpan return curtimespan #Set Start Frame------------------------------------------------------------ def BtnCallsetstartframe(control, event): ts = getCurrentTimeSpan() newTs = FBTimeSpan() curTime = FBSystem().LocalTime newTs.Set(curTime, ts.GetStop()) FBSystem().CurrentTake.LocalTimeSpan = newTs print "Set Start Frame" #Set End Frame------------------------------------------------------------ def BtnCallsetsetendframe(control, event): ts = getCurrentTimeSpan() newTs = FBTimeSpan() curTime = FBSystem().LocalTime newTs.Set(ts.GetStart(),curTime) FBSystem().CurrentTake.LocalTimeSpan = newTs print "Set End Frame" #Take Navigation Starts Here ------------------------------------------------------------ def GetCurrentTakeIndex(): """ Returns the index of the Current Take """ # Get the current Take oCurTake = FBSystem().CurrentTake # Iterate through the list of Takes to find the index i = 0 for oTake in FBSystem().Scene.Takes: # if this is the Take, return the index if oTake == oCurTake: return i # increment the index counter i += 1 #Previous Take------------------------------------------------------------ def BtnCallprevioustake(control, event): """ Switches to the Next Take if any. If there isn't, it loops back to the first Take. """ # Get the Current Take index iCurTakeIndex = GetCurrentTakeIndex() # Next index iPreviousIndex = iCurTakeIndex - 1 # Make sure the next index exists before switching if iPreviousIndex > -1: # switch using indexing FBSystem().CurrentTake = FBSystem().Scene.Takes[ iPreviousIndex ] else: # if it doesn't exist, go to the first Take FBSystem().CurrentTake = FBSystem().Scene.Takes[ 0 ] print "Previous Take" #Next Take------------------------------------------------------------ def BtnCallnexttake(control, event): """ Switches to the Next Take if any. If there isn't, it loops back to the first Take. """ # Get the Current Take index iCurTakeIndex = GetCurrentTakeIndex() # Next index iNextIndex = iCurTakeIndex + 1 # Make sure the next index exists before switching if iNextIndex < len( FBSystem().Scene.Takes ): # switch using indexing FBSystem().CurrentTake = FBSystem().Scene.Takes[ iNextIndex ] # This will allow the "Next Take" to loop from the last take to the first take just # # else: # # if it doesn't exist, go to the first Take # FBSystem().CurrentTake = FBSystem().Scene.Takes[ 0 ] print "Next Take" #Layout Starts Here ------------------------------------------------------------ def PopulateTool(t): #populate regions here x = FBAddRegionParam(12,FBAttachType.kFBAttachNone,"") y = FBAddRegionParam(5,FBAttachType.kFBAttachNone,"") w = FBAddRegionParam(55,FBAttachType.kFBAttachNone,"") h = FBAddRegionParam(55,FBAttachType.kFBAttachNone,"") t.AddRegion("gotostartframe","gotostartframe", x, y, w, h) t.SetControl("gotostartframe", gotostartframe) gotostartframe.Visible = True gotostartframe.ReadOnly = False gotostartframe.Enabled = True gotostartframe.Hint = "Goto Start Frame" gotostartframe.Caption = "<<" gotostartframe.State = 0 gotostartframe.Style = FBButtonStyle.kFBPushButton gotostartframe.Justify = FBTextJustify.kFBTextJustifyCenter gotostartframe.Look = FBButtonLook.kFBLookNormal gotostartframe.OnClick.Add(Btngotostartframe) x = FBAddRegionParam(72,FBAttachType.kFBAttachNone,"") y = FBAddRegionParam(5,FBAttachType.kFBAttachNone,"") w = FBAddRegionParam(120,FBAttachType.kFBAttachNone,"") h = FBAddRegionParam(55,FBAttachType.kFBAttachNone,"") t.AddRegion("setplay","setplay", x, y, w, h) t.SetControl("setplay", setplay) setplay.Visible = True setplay.ReadOnly = False setplay.Enabled = True setplay.Hint = "Play" setplay.Caption = "Play" setplay.State = 0 setplay.Style = FBButtonStyle.kFBPushButton setplay.Justify = FBTextJustify.kFBTextJustifyCenter setplay.Look = FBButtonLook.kFBLookNormal setplay.OnClick.Add(BtnCallsetplay) x = FBAddRegionParam(197,FBAttachType.kFBAttachNone,"") y = FBAddRegionParam(5,FBAttachType.kFBAttachNone,"") w = FBAddRegionParam(55,FBAttachType.kFBAttachNone,"") h = FBAddRegionParam(55,FBAttachType.kFBAttachNone,"") t.AddRegion("gotoendframe","gotoendframe", x, y, w, h) t.SetControl("gotoendframe", gotoendframe) gotoendframe.Visible = True gotoendframe.ReadOnly = False gotoendframe.Enabled = True gotoendframe.Hint = "Goto End Frame" gotoendframe.Caption = ">>" gotoendframe.State = 0 gotoendframe.Style = FBButtonStyle.kFBPushButton gotoendframe.Justify = FBTextJustify.kFBTextJustifyCenter gotoendframe.Look = FBButtonLook.kFBLookNormal gotoendframe.OnClick.Add(Btngotoendframe) x = FBAddRegionParam(10,FBAttachType.kFBAttachNone,"") y = FBAddRegionParam(70,FBAttachType.kFBAttachNone,"") w = FBAddRegionParam(120,FBAttachType.kFBAttachNone,"") h = FBAddRegionParam(55,FBAttachType.kFBAttachNone,"") t.AddRegion("setstartframe","setstartframe", x, y, w, h) t.SetControl("setstartframe", setstartframe) setstartframe.Visible = True setstartframe.ReadOnly = False setstartframe.Enabled = True setstartframe.Hint = "Set Currnet Fram to Start Frame of Current Take" setstartframe.Caption = "Set Start Frame" setstartframe.State = 0 setstartframe.Style = FBButtonStyle.kFBPushButton setstartframe.Justify = FBTextJustify.kFBTextJustifyCenter setstartframe.Look = FBButtonLook.kFBLookNormal setstartframe.OnClick.Add(BtnCallsetstartframe) x = FBAddRegionParam(135,FBAttachType.kFBAttachNone,"") y = FBAddRegionParam(70,FBAttachType.kFBAttachNone,"") w = FBAddRegionParam(120,FBAttachType.kFBAttachNone,"") h = FBAddRegionParam(55,FBAttachType.kFBAttachNone,"") t.AddRegion("setendframe","setendframe", x, y, w, h) t.SetControl("setendframe", setendframe) setendframe.Visible = True setendframe.ReadOnly = False setendframe.Enabled = True setendframe.Hint = "Set Currnet Fram to End Frame of Current Take" setendframe.Caption = "Set End Frame" setendframe.State = 0 setendframe.Style = FBButtonStyle.kFBPushButton setendframe.Justify = FBTextJustify.kFBTextJustifyCenter setendframe.Look = FBButtonLook.kFBLookNormal setendframe.OnClick.Add(BtnCallsetsetendframe) x = FBAddRegionParam(10,FBAttachType.kFBAttachNone,"") y = FBAddRegionParam(130,FBAttachType.kFBAttachNone,"") w = FBAddRegionParam(120,FBAttachType.kFBAttachNone,"") h = FBAddRegionParam(55,FBAttachType.kFBAttachNone,"") t.AddRegion("previoustake","previoustake", x, y, w, h) t.SetControl("previoustake", previoustake) previoustake.Visible = True previoustake.ReadOnly = False previoustake.Enabled = True previoustake.Hint = "Goto Previous Take" previoustake.Caption = "Previous Take" previoustake.State = 0 previoustake.Style = FBButtonStyle.kFBPushButton previoustake.Justify = FBTextJustify.kFBTextJustifyCenter previoustake.Look = FBButtonLook.kFBLookNormal previoustake.OnClick.Add(BtnCallprevioustake) x = FBAddRegionParam(135,FBAttachType.kFBAttachNone,"") y = FBAddRegionParam(130,FBAttachType.kFBAttachNone,"") w = FBAddRegionParam(120,FBAttachType.kFBAttachNone,"") h = FBAddRegionParam(55,FBAttachType.kFBAttachNone,"") t.AddRegion("nexttake","nexttake", x, y, w, h) t.SetControl("nexttake", nexttake) nexttake.Visible = True nexttake.ReadOnly = False nexttake.Enabled = True nexttake.Hint = "Goto Next Take" nexttake.Caption = "Next Take" nexttake.State = 0 nexttake.Style = FBButtonStyle.kFBPushButton nexttake.Justify = FBTextJustify.kFBTextJustifyCenter nexttake.Look = FBButtonLook.kFBLookNormal nexttake.OnClick.Add(BtnCallnexttake) def CreateTool(): t = FBCreateUniqueTool("Navigation Tool - Victor DeBaie") t.StartSizeX = 280 t.StartSizeY = 225 PopulateTool(t) ShowTool(t) CreateTool()
Since writing this tool back in late January (or was it early February??), I use this daily and I love it! Being able to jump through takes and easily setting my start and end frame per take (which is great for exporting animations to game engines) is an amazing thing.
I hope this helps.
4 Comments
Nice work, but in line 128 : SyntaxError: invalid syntax
I will check when I get back to my desk. Line 128 is within the layout which “should” be fine.
Have you tried restarting MotionBuilder then running the script?
I will try to upload a NavigationTool.py for people to dl in the next little bit.
There was a problem that occurred during the pasting of the script into the blog, it seems that multiple lines were pasted as on string. The navigation tool should work now, sorry about that.
Great work man! Your blog help me, to learn python.