Skip to content

Instantly share code, notes, and snippets.

@leestuartx
Last active August 29, 2015 14:23
Show Gist options
  • Save leestuartx/5de2c6c2b4f516f54b1a to your computer and use it in GitHub Desktop.
Save leestuartx/5de2c6c2b4f516f54b1a to your computer and use it in GitHub Desktop.
Auto orient and character animations in motionsbuilder - Python
#a script for removing all the keyframes on the root node
from pyfbsdk import *
import os.path, os, inspect, sys
# Find the animation node recurvesive by name.
def findAnimationNode( pName, pNode ):
lResult = None
lName = pName.split( '/' )
for lNode in pNode.Nodes:
if lNode.Name == lName[0]:
if len( lName ) > 1:
lResult = findAnimationNode( pName.replace( '%s/' % lName[0], '' ), lNode )
else:
lResult = lNode
return lResult
def copyAnimation(pSrc, pDst ):
for pName in [ 'Lcl Translation/X','Lcl Translation/Z','Lcl Rotation/X','Lcl Rotation/Y','Lcl Rotation/Z']:
lSrcNode = findAnimationNode( pName, pSrc.AnimationNode )
lDstNode = findAnimationNode( pName, pDst.AnimationNode )
if lSrcNode and lSrcNode.FCurve and lDstNode:
lDstNode.FCurve.KeyReplaceBy(lSrcNode.FCurve)
def ClearAnim( pNode ):
if pNode.FCurve:
pNode.FCurve.EditClear()
else:
for lNode in pNode.Nodes:
ClearAnim( lNode )
# Cleanup
del( lNode )
#List to populate character template
lScene = FBSystem().Scene
lSystem = FBSystem()
lcharactersList = FBSystem().Scene.Characters
lchar = lcharactersList[0]
#select the first character
lchar.Selected = True
#find a joint
jointsList = FBComponentList()
FBFindObjectsByName('Character1_Reference', jointsList, False, True)
rootJoint = jointsList[0];
jointsList2 = FBComponentList()
FBFindObjectsByName('Character1_Hips', jointsList2, False, True)
hipsJoint = jointsList2[0];
'''
Loop through all takes
for newTake in lSystem.Scene.Takes:
#Open next take
lSystem.CurrentTake = newTake
'''
#Set Time to 0
lPlayer = FBPlayerControl()
lPlayer.Goto(FBTime(0, 0, 0, 0))
rootJoint.Translation.SetAnimated(True)
rootJoint.Rotation.SetAnimated(True)
#clear the root joint
ClearAnim( rootJoint.AnimationNode )
'''
#bake to the rig before making any transformations
'''
lOptions = FBPlotOptions()
lOptions.ConstantKeyReducerKeepOneKey = True
lOptions.PlotAllTakes = False
lOptions.PlotOnFrame = True
lOptions.PlotPeriod = FBTime( 0, 0, 0, 1 )
lOptions.PlotTranslationOnRootOnly = True
lOptions.PreciseTimeDiscontinuities = True
lOptions.RotationFilterToApply = FBRotationFilter.kFBRotationFilterGimbleKiller
lOptions.UseConstantKeyReducer = False
lchar.PlotAnimation (FBCharacterPlotWhere.kFBCharacterPlotOnControlRig,lOptions )
rootJoint.Translation = FBVector3d(0.0, 0.0, 0.0)
rootJoint.Rotation = FBVector3d(0.0, 0.0, 0.0)
'''
Bake back to skeleton
'''
## Plot the character animation to skeleton
lOptions = FBPlotOptions()
lOptions.ConstantKeyReducerKeepOneKey = True
lOptions.PlotAllTakes = False
lOptions.PlotOnFrame = True
lOptions.PlotPeriod = FBTime( 0, 0, 0, 1 )
lOptions.PlotTranslationOnRootOnly = True
lOptions.PreciseTimeDiscontinuities = True
lOptions.RotationFilterToApply = FBRotationFilter.kFBRotationFilterGimbleKiller
lOptions.UseConstantKeyReducer = False
lchar.PlotAnimation (FBCharacterPlotWhere.kFBCharacterPlotOnSkeleton,lOptions )
'''
#bake to the rig before making any transformations
'''
lOptions = FBPlotOptions()
lOptions.ConstantKeyReducerKeepOneKey = True
lOptions.PlotAllTakes = False
lOptions.PlotOnFrame = True
lOptions.PlotPeriod = FBTime( 0, 0, 0, 1 )
lOptions.PlotTranslationOnRootOnly = True
lOptions.PreciseTimeDiscontinuities = True
lOptions.RotationFilterToApply = FBRotationFilter.kFBRotationFilterGimbleKiller
lOptions.UseConstantKeyReducer = False
lchar.PlotAnimation (FBCharacterPlotWhere.kFBCharacterPlotOnControlRig,lOptions )
'''
#Find the Tranlation and Rotation nodes of the hips joint for frame 0
'''
translateX = findAnimationNode( 'Lcl Translation/X', hipsJoint.AnimationNode )
translateY = findAnimationNode( 'Lcl Translation/Y', hipsJoint.AnimationNode )
translateZ = findAnimationNode( 'Lcl Translation/Z', hipsJoint.AnimationNode )
rotateX = findAnimationNode( 'Lcl Rotation/Y', hipsJoint.AnimationNode )
rotateY = findAnimationNode( 'Lcl Rotation/Y', hipsJoint.AnimationNode )
rotateZ = findAnimationNode( 'Lcl Rotation/Y', hipsJoint.AnimationNode )
xVal = translateX.FCurve.Keys[0].Value
yVal = translateY.FCurve.Keys[0].Value
zVal = translateZ.FCurve.Keys[0].Value
xRot = rotateX.FCurve.Keys[0].Value
yRot = rotateY.FCurve.Keys[0].Value
zRot = rotateZ.FCurve.Keys[0].Value
'''
Create new Animation Layer
'''
lSystem.CurrentTake.CreateNewLayer()
lCount = lSystem.CurrentTake.GetLayerCount()
lSystem.CurrentTake.GetLayer(lCount-1).Name= "PythonLayer1"
lSystem.CurrentTake.SetCurrentLayer(lCount-1)
'''
#Reposition the root joint to the hips coordinate on the Y Plane 0
'''
rootJoint.Translation = FBVector3d(xVal, 0.0, zVal)
rootJoint.Rotation = FBVector3d(0.0, yRot, 0.0)
lPlayer.Key()
'''
Merge Layers
'''
layer0 = lSystem.CurrentTake.GetLayer(0)
layer0.SelectLayer(True, False)
layer1 = lSystem.CurrentTake.GetLayer(1)
layer1.SelectLayer(True, False)
lSystem.CurrentTake.MergeLayers(FBAnimationLayerMergeOptions.kFBAnimLayerMerge_SelectedLayer_CompleteScene, True, FBMergeLayerMode.kFBMergeLayerModeAutomatic)
'''
Bake back to skeleton
'''
## Plot the character animation to skeleton
lOptions = FBPlotOptions()
lOptions.ConstantKeyReducerKeepOneKey = True
lOptions.PlotAllTakes = False
lOptions.PlotOnFrame = True
lOptions.PlotPeriod = FBTime( 0, 0, 0, 1 )
lOptions.PlotTranslationOnRootOnly = True
lOptions.PreciseTimeDiscontinuities = True
lOptions.RotationFilterToApply = FBRotationFilter.kFBRotationFilterGimbleKiller
lOptions.UseConstantKeyReducer = False
lchar.PlotAnimation (FBCharacterPlotWhere.kFBCharacterPlotOnSkeleton,lOptions )
'''
Clear keyframes on the root joint
'''
ClearAnim( rootJoint.AnimationNode )
'''
Move root to position and rotation 0,0,0
'''
rootJoint.Translation = FBVector3d(0.0, 0.0, 0.0)
rootJoint.Rotation = FBVector3d(0.0, 0.0, 0.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment