Using Python to Create a layer on the Current Selected Take in MotionBuilder.
Posted On February 9, 2017
In MotionBuilder using animation layers are great for edits, blending, additives and in general adding quality passes to your animations.
So below I’m going to demo how I learned to add layers using Python.
To create an Animation Layer on the Current Take script:
from pyfbsdk import * lSystem = FBSystem() ##Create a new layer on the current take lSystem.CurrentTake.CreateNewLayer() # Get the Layer Count for the current take lCount = lSystem.CurrentTake.GetLayerCount() # Use the layer count to name the layer: "lCount-1" = top most layer, "lCount-2" = second top most layer, "lCount-3" = third from the top layer, etc. lSystem.CurrentTake.GetLayer(lCount-1).Name= "Creat Through Python" #Set the top most layer as the selected/active one (Keys will be added to that layer) #if you have 5 Animation layers and want the second one from the top selected you would use "lCount-2" lSystem.CurrentTake.SetCurrentLayer(lCount-1)
Now let’s setup a Python Script to Merge all Layers and then Delete Merged Layers:
from pyfbsdk import * lSystem = FBSystem() #Merge all object in the scene to the BaseAnimation layer, delete the source layers if they are empty FBSystem().CurrentTake.MergeLayers(FBAnimationLayerMergeOptions.kFBAnimLayerMerge_AllLayers_CompleteScene, True, FBMergeLayerMode.kFBMergeLayerModeAutomatic) #Info - Merge option for animation layers: # kFBAnimLayerMerge_SelectedLayers_SelectedProperties = Merge the animation of the selected properties of the selected models from the selected layers to the selected layer with the lowest index. # kFBAnimLayerMerge_AllLayers_SelectedProperties = Merge the animation of the selected properties of the selected models from all the layers to the BaseAnimation layer. # kFBAnimLayerMerge_SelectedLayers_AllProperties = Merge the animation of all properties of the selected models from the selected layers to the selected layer with the lowest index. # kFBAnimLayerMerge_AllLayers_AllProperties = Merge the animation of all properties of the selected models from all the layers to the BaseAnimation layer. # kFBAnimLayerMerge_SelectedLayers_CompleteScene = Merge the animation of all properties from the selected layers to the selected layer with the lowest index. # kFBAnimLayerMerge_AllLayers_CompleteScene = Merge the animation of all properties from all the layers to the BaseAnimation layer.
Since we used “kFBAnimLayerMerge_AllLayers_CompleteScene” all our Merged Layers were deleted leaving a clean layer stack within the MotionBuilder take.
4 Comments
lSystem.CurrentTake.SetCurrentLayer(lCount-1)
it’s not working.
(Select new layer and base)
Visually it may have them both highlighted but if you were to do something like:
print FBSystem().CurrentTake.GetCurrentLayer()
the result will be a single number equal to (lCount-1)
I’ve tried this as well and it appears to have both the base and top layer selected.
GetCurrentLayer does return a single index, but if you read the latest documentation about SetCurrentLayer it says:
“Note that this will not deselect the other layers.”
is there an easy way to de-select a layer? so after SetCurrentLayer, you could de-select the base layer.
You could write a quick function to go through every layer within the take and set the layer.Selected = False.