MotionBuilder Python Clean Character From Scene with FBDelete()

This is a script that came about while researching and writing examples for my previous post on FBDelete(). This bit of script will go through a number of constructors within the FBSystem() class.

Here is a list of the classes in which our list will go through:

  • fb.FBSystem().Scene.Constraints
  • fb.FBSystem().Scene.Handles
  • fb.FBSystem().Scene.UserObjects
  • fb.FBSystem().Scene.ControlSets
  • fb.FBSystem().Scene.CharacterExtensions,
  • fb.FBSystem().Scene.Characters
  • fb.FBSystem().Scene.Materials
  • fb.FBSystem().Scene.Shaders
  • fb.FBSystem().Scene.Textures
  • fb.FBSystem().Scene.Folders
  • fb.FBSystem().Scene.Groups,
  • fb.FBSystem().Scene.ObjectPoses
  • fb.FBSystem().Scene.CharacterPoses
  • fb.FBSystem().Scene.KeyingGroups
  • fb.FBSystem().Scene.Notes
  • fb.FBSystem().Scene.VideoClips
  • fb.FBSystem().Scene.Components

It’s worth noting that there is an order in which you should go through this operators to prevent MotionBulder from Crashing or the script to error out. The above order is what I found works.

import pyfbsdk as fb
import pyfbsdk_additions as fba

def removechar(namespace):
    ##Create A List Of All The Scene Eelements We Want To Deal With First
    ##Some Of The Order Is Important As To Not Have MoBu Crash
    lFBList = [fb.FBSystem().Scene.Constraints, fb.FBSystem().Scene.Handles, fb.FBSystem().Scene.UserObjects, fb.FBSystem().Scene.ControlSets, fb.FBSystem().Scene.CharacterExtensions,
    fb.FBSystem().Scene.Characters, fb.FBSystem().Scene.Materials, fb.FBSystem().Scene.Shaders, fb.FBSystem().Scene.Textures, fb.FBSystem().Scene.Folders, fb.FBSystem().Scene.Groups,
    fb.FBSystem().Scene.ObjectPoses, fb.FBSystem().Scene.CharacterPoses, fb.FBSystem().Scene.KeyingGroups, fb.FBSystem().Scene.Notes, fb.FBSystem().Scene.VideoClips, fb.FBSystem().Scene.Components]
    ##With Each Item In Our List lFBList
    for item in lFBList:
        ##Create An Empty List That We Will Store To 
        lRemovelist = []
        ##For Every Thing Returned Per Item Within Our lFBList
        for i in item:
            ##Look To See If Our "namespace" Provided Exists Within The Items LongName
            if namespace in i.LongName:
                ##If Found Store The Item To Our List lRemovelist
                lRemovelist.extend([i])
            ##If Not Found Skip    
            else: pass
        ##Take Every Item We Stored In Our List lRemovelist     
        for item in lRemovelist:
            ##Delete It
            item.FBDelete()

A big thanks to Marcus Krautwurst, as he worked out the order in which to prevent crashes as well as Alex Forsythe for reworking the script. Alex’s script is a little beyond my current way of scripting but it was great to break it down and learn from it. Researching the map and lambda function proved to be really interesting.

I hope this helps.

Add a Comment

Your email address will not be published. Required fields are marked *