Skip to content

Instantly share code, notes, and snippets.

@leestuartx
Created June 15, 2015 17:07
Show Gist options
  • Save leestuartx/2861947bbe35667dfe31 to your computer and use it in GitHub Desktop.
Save leestuartx/2861947bbe35667dfe31 to your computer and use it in GitHub Desktop.
Rotate Character 90 Degrees
#Rotates a character 90 degrees without modifying the orientation of the root
#Create new layer, Rotate root 90, Set Key, merge layers, Bake to rig,
#Create new layer, rotate root to 0, set key, merge layers, bake to skeleton
#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];
#Set Time to 0
lPlayer = FBPlayerControl()
#lPlayer.Goto(FBTime(0, 0, 0, 0))
rootJoint.Translation.SetAnimated(True)
rootJoint.Rotation.SetAnimated(True)
'''
Create new Animation Layer
'''
lSystem.CurrentTake.CreateNewLayer()
lCount = lSystem.CurrentTake.GetLayerCount()
lSystem.CurrentTake.GetLayer(lCount-1).Name= "PythonLayer1"
lSystem.CurrentTake.SetCurrentLayer(lCount-1)
rootJoint.Rotation = FBVector3d(0.0, 90.0, 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 to the rig
'''
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 )
'''
Create new Animation Layer
'''
lSystem.CurrentTake.CreateNewLayer()
lCount = lSystem.CurrentTake.GetLayerCount()
lSystem.CurrentTake.GetLayer(lCount-1).Name= "PythonLayer1"
lSystem.CurrentTake.SetCurrentLayer(lCount-1)
rootJoint.Rotation = FBVector3d(0.0, 0.0, 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 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment