Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk® MotionBuilder® / Python / How do you connect boxes within a constraint with python?
  RSS 2.0 ATOM  

How do you connect boxes within a constraint with python?
Rate this thread
 
60363
 
Permlink of this thread  
avatar
  • Total Posts: 31
  • Joined: 08 January 2007 12:09 PM

Hi,

I have a scene into which I’ve merged some relation constraints.  I would like to use python to connect the constraints to joints already existing in the scene.

How do you connect box attributes in Motionbuilder with python?  I would like to connect ‘Number to Vector.Result’ to ‘LeftElbow_helper.Rotation(Lcl)’.  Please see the screen-grab below.

Many thanks!



Attachment Attachment
Replies: 1
/userdata/avatar/c4ccr1ftb.jpg

Hi,

This is the code to create the three nodes you need and how to hook them up.  If you have pre-existing nodes you would need to define them and plug them back into this code.  Hope it helps…

#CREATE RELATION CONSTRAINT
RollRelation = FBConstraintRelation( ‘RollDriverRelation’ )

#CREATE NODES
SenderBox = RollRelation.SetAsSource( yourSource ) #You need to give your source
RollRelation.SetBoxPosition( SenderBox, 30, 30 )

ConvertBox = RollRelation.CreateFunctionBox( ‘Converters’, ‘Number to Vector’ )
RollRelation.SetBoxPosition( ConvertBox, 400, 30 )

ResultBox = RollRelation.ConstrainObject( yourReceiver) #You need to give your receiver
RollRelation.SetBoxPosition( ResultBox, 700, 30)
ResultBox.UseGlobalTransforms = False # makes translation/rotation local “Lcl”

#POPULATE CONSTRAINT (LINK THEM UP)
SenderOut = FindAnimationNode( SenderBox.AnimationNodeOutGet(), ‘->you fill in<-’ )
ConvertIn = FindAnimationNode( ConvertBox.AnimationNodeInGet(), ‘Y’ )
if SenderOut and ConvertIn:  #This checks to make sure everything is there to hook up
FBConnect( SenderOut, ConvertIn )

ConvertOut = FindAnimationNode( ConvertBox.AnimationNodeOutGet(), ‘Result’ )
ResultIn = FindAnimationNode( ResultBox.AnimationNodeInGet(), ‘Lcl Rotation’ )
if ConvertOut and ResultIn:
FBConnect( ConvertOut, ResultIn )

#TURN THE CONSTRAINT ON
RollRelation.Active = True

Author: Newt Zac

Replied: 27 September 2011 05:56 AM  
avatar

Hi, and thanks for the reply.

All seems to be working until I get to the actual connection part.  I wouldn’t be surprised if my error is just syntax or something.  Below works until i get to “#POPULATE CONSTRAINT (LINK THEM UP)”.  I get this error when I execute that chunk:

File “<string>”, line 5
FBConnect( SenderOut, ConvertIn )
^
IndentationError: expected an indented block”.

Below is my whack at reverse engineering your script, and thanks for the help!:

from pyfbsdk import *

#CREATE RELATION CONSTRAINT
RollRelation = FBConstraintRelation( “RollDriverRelation” )

#CREATE NODES
lSenderJoint = FBFindModelByName ( “LeftForeArm” )
SenderBox = RollRelation.SetAsSource( lSenderJoint ) #You need to give your source
RollRelation.SetBoxPosition( SenderBox, 30, 30 )
SenderBox.UseGlobalTransforms = False # makes translation/rotation local “Lcl”

ConvertBox = RollRelation.CreateFunctionBox( “Converters”, “Number to Vector” )
RollRelation.SetBoxPosition( ConvertBox, 400, 30 )

lRevieverJoint = FBFindModelByName ( “LeftElbow_helper” )
ResultBox = RollRelation.ConstrainObject( lRevieverJoint ) #You need to give your receiver
RollRelation.SetBoxPosition( ResultBox, 700, 30)
ResultBox.UseGlobalTransforms = False # makes translation/rotation local “Lcl”

#POPULATE CONSTRAINT (LINK THEM UP)
SenderOut = FindAnimationNode( SenderBox.AnimationNodeOutGet(), “Lcl Rotation” )
ConvertIn = FindAnimationNode( ConvertBox.AnimationNodeInGet(), “Y” )
if SenderOut and ConvertIn:  #This checks to make sure everything is there to hook up
FBConnect( SenderOut, ConvertIn )

ConvertOut = FindAnimationNode( ConvertBox.AnimationNodeOutGet(), “Result” )
ResultIn = FindAnimationNode( ResultBox.AnimationNodeInGet(), “Lcl Rotation” )
if ConvertOut and ResultIn:
FBConnect( ConvertOut, ResultIn )

#TURN THE CONSTRAINT ON
RollRelation.Active = True



Replies: 1
/userdata/avatar/c4ccr1ftb.jpg

Sorry,

You need to indent the lines that are below the if statments.  My post wasn’t indented properly.  Should read:

SenderOut = FindAnimationNode( SenderBox.AnimationNodeOutGet(), ‘->you fill in<-’ )
ConvertIn = FindAnimationNode( ConvertBox.AnimationNodeInGet(), ‘Y’ )
if SenderOut and ConvertIn:  #This checks to make sure everything is there to hook up
FBConnect( SenderOut, ConvertIn )

Author: Newt Zac

Replied: 03 October 2011 11:26 AM  
avatar

Ahh, thanks.  Yes, i’ve noticed copy pasting can be tough with python because tabs can get lost.  Makes sense. 

I was also given this solution by Sergey from http://neill3d.com/.  Super nice he’s given me permission to post this.  Super cool solution for a circumstance were you are importing everything into your scene and need to query all the objects, constraints and boxes.

from pyfbsdk import*

# Define a utility function to look up a named animation node
# under a given parent. Will not recurse in the hierarchy.
def FindAnimationNode( pParent, pName ):
lResult = None
for lNode in pParent.Nodes:
if lNode.Name == pName:
lResult = lNode
break
return lResult

def FindRelationBox( pCon, pName ):
lResult = None
for lBox in pCon.Boxes:
if lBox.Name == pName:
lResult = lBox
break
return lResult

for lCon in FBSystem().Scene.Constraints:
print lCon.Name
if lCon.Name == ‘Relation’:

cubeBox = FindRelationBox(lCon, ‘Cube’)
vectorToNumberBox = FindRelationBox(lCon, ‘Vector to Number’)

if cubeBox and vectorToNumberBox:
outNode = FindAnimationNode( cubeBox.AnimationNodeOutGet(), ‘Lcl Rotation’)
inNode = FindAnimationNode( vectorToNumberBox.AnimationNodeInGet(), ‘V’ )

if outNode and inNode:
FBConnect(outNode, inNode)



Attachment Attachment
Replies: 0