# Copyright 2009 Autodesk, Inc.  All rights reserved.
# Use of this software is subject to the terms of the Autodesk license agreement 
# provided at the time of installation or download, or which otherwise accompanies
# this software in either electronic or hard copy form.
#
# Topic: FBContraintRelation, FBConnect
#
from pyfbsdk import FBConstraintRelation, FBModelMarker, FBConnect

# 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

# Create a constraint relation. No need to use the FBConstraintManager.
lConstraintRelation = FBConstraintRelation( 'AConstraintRelation' )

# Create an object which will be constrained in the relation.
lModel = FBModelMarker( 'AModelMarker' )
lModel.Visible = True

# Create a source and place it in 2D space.
lSineRampBox = lConstraintRelation.CreateFunctionBox( 'Sources', 'Sine Ramp' )
lConstraintRelation.SetBoxPosition( lSineRampBox, 30, 30 )

# Create a converter.
lNumberToVectorBox = lConstraintRelation.CreateFunctionBox( 'Converters', 'Number to Vector' )
lConstraintRelation.SetBoxPosition( lNumberToVectorBox, 400, 30 )

# Now insert the marker in the constraint. Set its position.
lPlaceHolderBox = lConstraintRelation.ConstrainObject( lModel )
lConstraintRelation.SetBoxPosition(lPlaceHolderBox, 700, 30)

# In order to set up the constraint, we need to connect the boxe's
# animation node together.

# We will connect the sine ramp to the converter.
lSineRampOut       = FindAnimationNode( lSineRampBox.AnimationNodeOutGet(), 'Result' )
lNumberToVectorIn  = FindAnimationNode( lNumberToVectorBox.AnimationNodeInGet(), 'Y' )
if lSineRampOut and lNumberToVectorIn:
    FBConnect( lSineRampOut, lNumberToVectorIn )

# And connect the converter to the translation of the marker.
lNumberToVectorOut = FindAnimationNode( lNumberToVectorBox.AnimationNodeOutGet(), 'Result' )
lModelIn           = FindAnimationNode( lPlaceHolderBox.AnimationNodeInGet(), 'Translation' )
if lNumberToVectorOut and lModelIn:
    FBConnect( lNumberToVectorOut, lModelIn )

# Finally activate the constraint.
lConstraintRelation.Active = True

# Cleanup of local variables.
del( lModelIn, lNumberToVectorOut, lNumberToVectorIn, lSineRampOut, lPlaceHolderBox, lNumberToVectorBox, lSineRampBox, lModel, lConstraintRelation )

# Cleanup of local functions.
del( FindAnimationNode )

# Cleanup of imported symbols.
del( FBConstraintRelation, FBModelMarker, FBConnect )