Tasks/CreateAndPopulateAConstraintRelation.py

Tasks/CreateAndPopulateAConstraintRelation.py
1 # Copyright 2009 Autodesk, Inc. All rights reserved.
2 # Use of this software is subject to the terms of the Autodesk license agreement
3 # provided at the time of installation or download, or which otherwise accompanies
4 # this software in either electronic or hard copy form.
5 #
6 # Topic: FBContraintRelation, FBConnect
7 #
8 from pyfbsdk import FBConstraintRelation, FBModelMarker, FBConnect
9 
10 # Define a utility function to look up a named animation node
11 # under a given parent. Will not recurse in the hierarchy.
12 def FindAnimationNode( pParent, pName ):
13  lResult = None
14  for lNode in pParent.Nodes:
15  if lNode.Name == pName:
16  lResult = lNode
17  break
18  return lResult
19 
20 # Create a constraint relation. No need to use the FBConstraintManager.
21 lConstraintRelation = FBConstraintRelation( 'AConstraintRelation' )
22 
23 # Create an object which will be constrained in the relation.
24 lModel = FBModelMarker( 'AModelMarker' )
25 lModel.Translation.SetAnimated(True)
26 lModel.Visible = True
27 
28 # Create a source and place it in 2D space.
29 lSineRampBox = lConstraintRelation.CreateFunctionBox( 'Sources', 'Sine Ramp' )
30 lConstraintRelation.SetBoxPosition( lSineRampBox, 30, 30 )
31 
32 # Create a converter.
33 lNumberToVectorBox = lConstraintRelation.CreateFunctionBox( 'Converters', 'Number to Vector' )
34 lConstraintRelation.SetBoxPosition( lNumberToVectorBox, 400, 30 )
35 
36 # Now insert the marker in the constraint. Set its position.
37 lPlaceHolderBox = lConstraintRelation.ConstrainObject( lModel )
38 lConstraintRelation.SetBoxPosition(lPlaceHolderBox, 700, 30)
39 
40 # In order to set up the constraint, we need to connect the boxe's
41 # animation node together.
42 
43 # We will connect the sine ramp to the converter.
44 lSineRampOut = FindAnimationNode( lSineRampBox.AnimationNodeOutGet(), 'Result' )
45 lNumberToVectorIn = FindAnimationNode( lNumberToVectorBox.AnimationNodeInGet(), 'Y' )
46 if lSineRampOut and lNumberToVectorIn:
47  FBConnect( lSineRampOut, lNumberToVectorIn )
48 
49 # And connect the converter to the translation of the marker.
50 lNumberToVectorOut = FindAnimationNode( lNumberToVectorBox.AnimationNodeOutGet(), 'Result' )
51 lModelIn = FindAnimationNode( lPlaceHolderBox.AnimationNodeInGet(), 'Translation' )
52 if lNumberToVectorOut and lModelIn:
53  FBConnect( lNumberToVectorOut, lModelIn )
54 
55 # Finally activate the constraint.
56 lConstraintRelation.Active = True
57 
58 # Cleanup of local variables.
59 del( lModelIn, lNumberToVectorOut, lNumberToVectorIn, lSineRampOut, lPlaceHolderBox, lNumberToVectorBox, lSineRampBox, lModel, lConstraintRelation )
60 
61 # Cleanup of local functions.
62 del( FindAnimationNode )
63 
64 # Cleanup of imported symbols.
65 del( FBConstraintRelation, FBModelMarker, FBConnect )