Saving MotionBuilder Character Animation with Python


When I get Mocap it’s usually in a messy scene. Optics, Markets, strange NameSpace, etc. they all can bog down a scene. One of the first thing I do is save out the Character Animation and load it back into a clean scene.

Using Python to save out Character Animation is great! Python can help ensure consistency with the pipeline you set up.

I did some searching and was able to find some info that allowed me to quickly creat a script that would do the following:

  • Check for a folder structure and if it does not exist then the script will creat one.
  • Set up options in which to save the current Character’s Animations.
  • Save out Character Animation  .fbx files.

The end result of the script’s out put would look like this:

“C:\MBChrAnimFiles\SceneName\SceneName–CharacterNameSpace.fbx”

I thought organizing the data this way would be helpful if I ever had to go back to my original Raw Data, the folder and file structure tells me what the original file was named and what Character it was saved from.

Save Character Animation Script:

from pyfbsdk import *
import os
from time import gmtime, strftime

lSystem = FBSystem()
lApp = FBApplication()

lSaveCharCurrent = lApp.CurrentCharacter
lSaveCharCurrentName = lSaveCharCurrent.LongName[:-10]
lSaveTakeNameCurrent = lSystem.CurrentTake.Name
lSaveFBXFileFullName = lApp.FBXFileName

# The place you want your animations to be saved
lSaveDirRoot = r"C:\MBChrAnimFiles"

# Variables for the sub folder and scene name
lSaveDirSub = os.path.splitext(os.path.basename(lSaveFBXFileFullName))[0]
lSaveSceneName = os.path.splitext(os.path.basename(lSaveFBXFileFullName))[0]

# Variable for the paths
lSavePath = lSaveDirRoot + "\\" + lSaveDirSub + "\\"

# Variable for the file name
lSaveFileName = lSaveSceneName + "--" + lSaveCharCurrentName

# Variable for the full path "Root\SubFolder\FileName"
lSaveChrAnim = lSavePath + lSaveFileName
  
# Directory setup to save in (if director it not there then we make it)
if not os.path.exists(lSavePath):
    os.makedirs(lSavePath)    

# Save Animation Options    
lSaveOptions = FBFbxOptions (False) # false = will not save options 
lSaveOptions.SaveCharacter = True
lSaveOptions.SaveControlSet = True
lSaveOptions.SaveCharacterExtention = True
lSaveOptions.ShowFileDialog = False
lSaveOptions.ShowOptionslDialog = False

print lSaveCharCurrentName + "'s Data from " + lSaveSceneName + " is being saved. Please wait...."

# Save out animation
lApp.SaveCharacterRigAndAnimation(lSaveChrAnim, lSaveCharCurrent, lSaveOptions)

print lSaveCharCurrentName + "'s Data from " + lSaveSceneName + " has been saved."

This was one of those tools that I love. Running the script saves you from a number of clicks and also so much typing – it allows you to click and move on with your creation. 🙂

A big thanks to Eduardo Simioni, I was lucky to work with him on Assassin’s Unity. He is a great guy who has an awesome blog full of Python knowledge and Animation Tech, I wish I still worked with him as he is a wealth on knowledge – thank you Eduardo.

I hope this helps.

 

Leave a reply:

Your email address will not be published.

Site Footer