Animating and setting keys with MotionBuilder’s Python Editor
Posted On February 16, 2017

Here is the code for a Python script that I wrote dealing animating and setting keys on a selected object within MotionBuilder. Writing this animation script really helped me get a better understanding on how to use MotionBuilder’s FBTime. The exercise forced me to sit down for about two hours and research 3 of the major things that we as animators manipulate – Time, Keys, and Transforms.
I hope this helps others and I really look forward to spending more time with fun scripts like this.
A big thanks goes to Alex Forsythe’s video “Python Scripting in MotionBuilder – 05 – FBTime and Playback Control”.
from pyfbsdk import *
# Get the instance of FBApplication, and clear the scene with FileNew().
app = FBApplication()
app.FileNew()
# import time to allow us to access time line controls and play back controls
import time
# ensure new scene is set to 30 fps and that the time line is shown as frames
FBPlayerControl().SetTransportFps(FBTimeMode.kFBTimeMode30Frames)
FBPlayerControl().TransportTimeFormat = FBTransportTimeFormat.kFBTimeFormatFrame
# Set our Timeline to be 200 frames in length
FBSystem().CurrentTake.LocalTimeSpan = FBTimeSpan(
# set start frame (0, 0, 0, 0, 0) = (hours, minutes, seconds, frames, fields)
FBTime(0, 0, 0, 0, 0),
# end frame
FBTime(0, 0, 0, 200, 0)
# if you wanted to set the time line to be 3 minutes long you could do some of the following to achieve that:
#in frames = (0, 0, 0, 5400, 0)
#in seconds = (0, 0, 180, 0, 0)
#in minutes = (0, 3, 0, 0, 0)
)
# Creat an object that we will animate
lmarker = FBModelMarker('VicMarker')
lmarker.Show = True
lmarker.Size = 1000
lmarker.PropertyList.Find('LookUI').Data=1
lmarker.Color = FBColor(0,1,1)
lmarker.Selected = True
# Define the Transaltion to be represented as lmarker.Translation
xTrans, yTrans, zTrans = lmarker.Translation
# Declare that lmarker.Translation can be animated
lmarker.Translation.SetAnimated(True)
# Position lmarker.Translation to 0 on the x axis, 250 on the y axis and 0 on the z axis [0, 250, 0]
# Set a key at frame 0 (FBTime(0,0,0,0) - the time is the last 0 in the sequence
lmarker.Translation.GetAnimationNode().KeyAdd(FBTime(0,0,0,0), [0, 250, 0])
# Position lmarker.Translation to -200 on the x axis, -150 on the y axis and 200 on the z axis [-200, -150, 200]
# Set a key at frame 25 (FBTime(0,0,0,25)
lmarker.Translation.GetAnimationNode().KeyAdd(FBTime(0,0,0,25), [-200, 150, 200])
# Set a key at frame 50 (FBTime(0,0,0,50) with x,y and z translation values
lmarker.Translation.GetAnimationNode().KeyAdd(FBTime(0,0,0,50), [0, 0, 400])
# Set a key at frame 75......
lmarker.Translation.GetAnimationNode().KeyAdd(FBTime(0,0,0,75), [200, -150, 250])
# etc. for the remaining 5 translation keys that will be set on frames 100, 125, 150, 175 and finally our last frame 200
lmarker.Translation.GetAnimationNode().KeyAdd(FBTime(0,0,0,100), [0, -250, 0])
lmarker.Translation.GetAnimationNode().KeyAdd(FBTime(0,0,0,125), [-200, -150, -250])
lmarker.Translation.GetAnimationNode().KeyAdd(FBTime(0,0,0,150), [0, 0, -400])
lmarker.Translation.GetAnimationNode().KeyAdd(FBTime(0,0,0,175), [200, 150, -200])
lmarker.Translation.GetAnimationNode().KeyAdd(FBTime(0,0,0,200), [0, 250, 0])
print "BAM! YOU JUST ANIMATED WITH PYTHON! :)"