# 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: FBFindModelByName, FBCharacter, FBMessageBox

from pyfbsdk import *

# List of bones on which we need to set the degrees of freedom.
lBonesName = ( 'LeftForeArm', 'RightForeArm', 'LeftLeg', 'RightLeg' )

# List of characters that we have handled.
lAffectedCharacterList = []

# Needed to get to the list of characters in the scene.
lSystem = FBSystem()
lScene = lSystem.Scene

# Create a function to set GameMode on a character.
def SetGameModeOnSelectedCharacters(pCharacter, pState = True):

    print 'Setting game mode for character \'%s\'' % pCharacter.Name

    # List of character bones, on which we will set the DOF
    lBones = []
    global lBonesName
    # Find the desired bones in the current character by prefixing
    # the character name as the namespace.
    for lBoneName in lBonesName:
        lBone = FBFindModelByName( '%s:%s' % ( pCharacter.Name, lBoneName ))
        if lBone: lBones.append( lBone )

    # Check that we have indeed found all the bones.
    if len( lBonesName ) == len( lBones ):

        # Then set the DOF for X and Y rotation.
        for lBone in lBones:
            lBone.PropertyList.Find( 'RotationActive' ).Data = pState
            lBone.PropertyList.Find( 'RotationMinX' ).Data = pState
            lBone.PropertyList.Find( 'RotationMinY' ).Data = pState
            lBone.PropertyList.Find( 'RotationMaxX' ).Data = pState
            lBone.PropertyList.Find( 'RotationMaxY' ).Data = pState

        # Add the current character to the list of modified characters.
        lAffectedCharacterList.append( lCharacter.Name )

        # If we have a mismatch in bone names, or if we are missing
        # bones, we inform the user that no work was done.
        FBMessageBox( 'Warning', 'Unable to set game mode for character \'%s\'' % lCharacter.Name, "Ok" )

# Look at all the characters in the scene.
for lCharacter in lScene.Characters:

    # Then locate those that are selected.
    if lCharacter.Selected:

# Feedback to the user, to let him know which characters were modified,
# if any.
if len( lAffectedCharacterList ) == 0:
    if FBApplication().CurrentCharacter !=None:
        if FBMessageBox( 'Information', 'Could not set Game Mode. No character is selected. \n Do you want to enable Game Mode on the current Character?', 'Yes', 'No' )==1:
        FBMessageBox( 'Information', 'Could not set game mode. No character were selected.', 'Ok' )

    FBMessageBox( 'Information', 'Set game mode for characters :%s' % lAffectedCharacterList, 'Ok' )