#-----------------------------------------------------------------#
#	SCRIPT: BipedToCharacterV2.py
#	AUTHOR:	Brett Ineson
#	UPDATED:		Nov 2007
# 	DESCRIPTION:	automate the characterization process 
#                           for a Biped with any prefix
#
#  Note:  you must select a skeleton joint before running script
#           as Max places prefix on joints not Fbx_Root
#
#  this version only works with MotionBuilder ext 2 patch C or later builds
#  -- added 'characterize' function missing from previous version
#-----------------------------------------------------------------#


from pyfbsdk import *


# Biped to Character Map:

lBipedMap = (('Reference', 'Fbx_Root'),
             ('Hips',''),
             ( 'LeftUpLeg', 'L Thigh' ),
             ( 'LeftLeg', 'L Calf' ),
             ( 'LeftFoot', 'L Foot'),
             ( 'RightUpLeg', 'R Thigh'),
             ( 'RightLeg', 'R Calf'),
             ( 'RightFoot', 'R Foot'),
             ( 'Spine', 'Spine'),
             ( 'LeftArm', 'L UpperArm'),
             ( 'LeftForeArm', 'L Forearm'),
             ( 'LeftHand', 'L Hand'),
             ( 'RightArm', 'R UpperArm'),
             ( 'RightForeArm', 'R Forearm'),
             ( 'RightHand', 'R Hand'),
             ( 'Head', 'Head'),
             ( 'LeftShoulder', 'L Clavicle'),
             ( 'RightShoulder', 'R Clavicle'),
             ( 'Neck', 'Neck'),
             ( 'Spine1', 'Spine1'),
             ( 'Spine2', 'Spine2'),
             ( 'Spine3', 'Spine3'),
             ( 'Spine4', 'Spine4'),
             ( 'Spine5', 'Spine5'),
             ( 'Spine6', 'Spine6'),
             ( 'Spine7', 'Spine7'),
             ( 'Spine7', 'Spine8'),
             ( 'Spine9', 'Spine9'),
             ( 'Neck1', 'Neck1'),
             ( 'Neck2', 'Neck2'),
             ( 'Neck3', 'Neck3'),
             ( 'Neck4', 'Neck4'),
             ( 'Neck5', 'Neck5'),
             ( 'Neck6', 'Neck6'),
             ( 'Neck7', 'Neck7'),
             ( 'Neck8', 'Neck8'),
             ( 'Neck9', 'Neck9'),
             ( 'LeftHandThumb1', 'L Finger0'),
             ( 'LeftHandThumb2', 'L Finger01'),
             ( 'LeftHandThumb3', 'L Finger02'),
             ( 'LeftHandIndex1', 'L Finger1'),
             ( 'LeftHandIndex2', 'L Finger11'),
             ( 'LeftHandIndex3', 'L Finger12'),
             ( 'LeftHandMiddle1', 'L Finger2'),
             ( 'LeftHandMiddle2', 'L Finger21'),
             ( 'LeftHandMiddle3', 'L Finger22'),
             ( 'LeftHandRing1', 'L Finger3'),
             ( 'LeftHandRing2', 'L Finger31'),
             ( 'LeftHandRing3', 'L Finger32'),
             ( 'LeftHandPinky1', 'L Finger4'),
             ( 'LeftHandPinky2', 'L Finger41'),
             ( 'LeftHandPinky3', 'L Finger42'),
             ( 'RightHandThumb1', 'R Finger0'),
             ( 'RightHandThumb2', 'R Finger01'),
             ( 'RightHandThumb3', 'R Finger02'),
             ( 'RightHandIndex1', 'R Finger1'),
             ( 'RightHandIndex2', 'R Finger11'),
             ( 'RightHandIndex3', 'R Finger12'),
             ( 'RightHandMiddle1', 'R Finger2'),
             ( 'RightHandMiddle2', 'R Finger21'),
             ( 'RightHandMiddle3', 'R Finger22'),
             ( 'RightHandRing1', 'R Finger3'),
             ( 'RightHandRing2', 'R Finger31'),
             ( 'RightHandRing3', 'R Finger32'),
             ( 'RightHandPinky1', 'R Finger4'),
             ( 'RightHandPinky2', 'R Finger41'),
             ( 'RightHandPinky3', 'R Finger42'),
             ( 'LeftFootThumb1', 'L Toe4'),
             ( 'LeftFootThumb2', 'L Toe41'),
             ( 'LeftFootThumb3', 'L Toe42'),
             ( 'LeftFootIndex1', 'L Toe3'),
             ( 'LeftFootIndex2', 'L Toe31'),
             ( 'LeftFootIndex3', 'L Toe32'),
             ( 'LeftFootMiddle1', 'L Toe2'),
             ( 'LeftFootMiddle2', 'L Toe21'),
             ( 'LeftFootMiddle3', 'L Toe22'),
             ( 'LeftFootRing1', 'L Toe1'),
             ( 'LeftFootRing2', 'L Toe11'),
             ( 'LeftFootRing3', 'L Toe12'),
             ( 'LeftFootPinky1', 'L Toe0'),
             ( 'LeftFootPinky2', 'L Toe01'),
             ( 'LeftFootPinky3', 'L Toe02'),
             ( 'RightFootThumb1', 'R Toe4'),
             ( 'RightFootThumb2', 'R Toe41'),
             ( 'RightFootThumb3', 'R Toe42'),
             ( 'RightFootIndex1', 'R Toe3'),
             ( 'RightFootIndex2', 'R Toe31'),
             ( 'RightFootIndex3', 'R Toe32'),
             ( 'RightFootMiddle1', 'R Toe2'),
             ( 'RightFootMiddle2', 'R Toe21'),
             ( 'RightFootMiddle3', 'R Toe22'),
             ( 'RightFootRing1', 'R Toe1'),
             ( 'RightFootRing2', 'R Toe11'),
             ( 'RightFootRing3', 'R Toe12'),
             ( 'RightFootPinky1', 'R Toe0'),
             ( 'RightFootPinky2', 'R Toe01'),
             ( 'RightFootPinky3', 'R Toe02'),
             ( 'LeftUpLegRoll', 'LThighTwist'),
             ( 'LeftLegRoll', 'LCalfTwist'),
             ( 'RightUpLegRoll', 'RThighTwist'),
             ( 'RightLegRoll', 'RCalfTwist'),
             ( 'LeftArmRoll', 'LUpArmTwist'),
             ( 'LeftForeArmRoll', 'L ForeTwist'),
             ( 'RightArmRoll', 'RUpArmTwist'),
             ( 'RightForeArmRoll', 'R ForeTwist'))

#------------------------------------------------------------------

def addJointToCharacter ( characterObject, slot, jointName ):
        myJoint = FBFindModelByName(jointName)
        
        property = characterObject.PropertyList.Find(slot + "Link")
        property.append (myJoint)
    
#------------------------------------------------------------------
	
	
# Create an empty FBModelList object.
lModels = FBModelList()

# Obtain the list of selected models.
FBGetSelectedModels(lModels)

# check if there is a selection
if len( lModels ) == 0:
  FBMessageBox( "Message", "Please select a Biped joint", "OK", None, None )
  
else:

    # grab prefix and create Character Node
    lBiped = lModels[0]
    lName = lBiped.Name
    lSplit = lName.split()
    lPrefix = lSplit[0]
    myBiped = FBCharacter(lPrefix)

                
    # Create a FBProgress object and set default values for the caption and text.    
    lFbp = FBProgress()
    lFbp.Caption = ""
    lFbp.Text = " -----------------------------------   Creating Biped character"
    lCount = 0.0

    # assign Biped to Character Mapping.
    for ( pslot, pjointName ) in lBipedMap:
        if ( pjointName ) == 'Fbx_Root':
            addJointToCharacter (myBiped, pslot, pjointName)
        else:
            if len ( pjointName ) == 0:
                addJointToCharacter (myBiped, pslot, lPrefix)            
            else:   
                addJointToCharacter (myBiped, pslot, (lPrefix + " " + pjointName))          
            
            lCount += 1
            lVal = lCount / len(lBipedMap) * 100            
            lFbp.Percent = int(lVal)


    lCharacter = FBApplication().CurrentCharacter
    switchOn = lCharacter.SetCharacterizeOn( True )
    
    print "Character mapping created for " + (myBiped.Name)
    
lFbp.FBDelete()

             
