MotionBuilder Python – A Better Way To Create Constraints


Thanks to kilianeczka for the heads up, there is even an EASIER easy way to create a constraint using MotionBuilder and Python

import pyfbsdk as fb


This omits the need to find the number of constraints and then search all the constraints for one that has the name ‘Parent/Child’.

In my previous post about constraint (link), I was creating different constraint types by using the predefined numeric value that represented them.

example:  lMyCons = FBConstraintManager() .TypeCreateConstraint(10)

The number “10” would represent the “Parent/Child” constraint type.

The above method worked for me but it didn’t for many others and the reason being was that the number did not always represent the specific constraint type “Parent/Child”.  Depending on the release version of MotionBuilder you are using and if you have unique constraints created for your project – the numbers will vary, so we can agree that using “lMyCons = FBConstraintManager() .TypeCreateConstraint(10)” to create a “Parent/Child” constraint is not the smartest of way . With each release of MotionBuider relying solely on a numerical value to represent our constraint type is not future proof .

On to a better way!!

In all honesty we will still be using a numerical value to tell our Python script which constraint we desire, but we will be doing it in a different way – we will use the name of the desired constraint as our final condition in our script.  Let’s make a script that goes through every constraint within MotioBuilder and when the constraint’s name matches our desired name, then and only then will we have MotionBuilder create the constraint.

So to do this we will have to cycle through every constraint, which is an easy enough task once we know the amount of constraint that exist within MotionBuilder – to do this we will use the class FBConstraintManager().TypeGetCount().

import pyfbsdk as fb


This will return the number of constraint MotionBuilder has available and that number will be used in a range to list all our constraints with the help of FBConstraintManager().TypeGetName().  🙂

import pyfbsdk as fb

for i in range( fb.FBConstraintManager().TypeGetCount() ):
    print fb.FBConstraintManager().TypeGetName(i)

With the above script we now can get a list of all the constraint names within our scene.

Since we now know how many constraints there are and what their names are – we can now isolate the constraint we want by its name.

Let’s use the name “Parent/Child” as an example:

import pyfbsdk as fb

##For each entry in the range of 0 to the total # of constraints
## i will represent the current number being processed within the range 0 to the total # of constraints
for i in range( fb.FBConstraintManager().TypeGetCount() ):
    ## Look for the string "Parent/Child" in the constraints name
    if "Parent/Child" in fb.FBConstraintManager().TypeGetName(i):
        ##When a match occurs then create that constraint

By finding the total number of constraint and then searching all the constraints for a name is kind of the long way around but it is the most reliable that I have come across within MotionBuilder.

So now lets write a little function that takes all of the above and puts it to good use.

import pyfbsdk as fb

Usage Example: CreateParentConstraint("Marker", "Marker 1", True, 100)

This will create a Parent/Child constraint with the object named "Marker" as the parent, the object names "Marker 1" as the child. The child will maintain its original position and the weight of the constraint will be set to 100% 

def CreateParentConstraint(parent, child, offset, weight): 
    ##Get Object By Name
    def GetObjectByName(name):
        lModel = fb.FBFindModelByLabelName(name) 
        return lModel
    ##Set Parent Object    
    objParent = GetObjectByName(parent)
    ##Set Child Object
    objChild = GetObjectByName(child)
    ##Create "Parent/Child" Constraint 
    for i in range( fb.FBConstraintManager().TypeGetCount() ):
        if "Parent/Child" in fb.FBConstraintManager().TypeGetName(i):
            lMyConstraint = fb.FBConstraintManager().TypeCreateConstraint(i)
    #for index, element in enumerate(selected_objects):
    lMyConstraint.ReferenceAdd (0, objChild)
    lMyConstraint.ReferenceAdd (1, objParent)
    #Snap if user desires
    if offset == True:
    #weight of the constraint
    lMyConstraint.Weight = weight
    ##Activate Constraint
    lMyConstraint.Active = True

So there we have it, this is a better way to create a constraint than my previous post has show.

As always I hope this helps.


Add a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.