Advertisement
Guest User

CharacterizeFromDictionary.py

a guest
Oct 8th, 2012
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. '''
  2. CharacterizeFromDictionary.py
  3.  
  4. Defines a Characterize function that attempts to perform characterization
  5. based on a dictionary mapping from HumanIK node names to joint names when
  6. provided with the skeleton under which those joints can be found.
  7.  
  8. http://tech-artists.org/forum/showthread.php?3201
  9. Alex Forsythe, October 8th, 2012
  10. '''
  11.  
  12. from pyfbsdk import *
  13.  
  14. def Characterize(character, rootJoint, mappings, useBiped = True):
  15.     '''
  16.    Clears all current characterization for the given character and attempts
  17.    to characterize it using the skeleton whose joints are contained beneath
  18.    rootJoint. mappings should be a dict with HumanIK node names (sans 'Link',
  19.    e.g. 'LeftHandExtraFinger2') as keys and joint short names as values. The
  20.    optional useBiped parameter can be set false to use quadruped
  21.    characterization instead of the default biped. Returns success.
  22.    '''
  23.     # Ensure that we start with a blank slate
  24.     ClearCharacterization(character)
  25.  
  26.     # Iterate over the list of mappings
  27.     for nodeName in mappings:
  28.  
  29.         # Get the property object for the requested node
  30.         propertyName = '%sLink' % nodeName
  31.         nodeProperty = character.PropertyList.Find(propertyName)
  32.         assert nodeProperty is not None, '%s is not a valid node name.' % nodeName
  33.  
  34.         # Find the joint within the skeleton
  35.         jointName = mappings[nodeName]
  36.         jointModel = FindDescendantModel(rootJoint, jointName)
  37.         assert jointModel is not None, 'No joint with short name %s was found beneath %s.' % (jointName, rootJoint.LongName)
  38.  
  39.         # Associate the joint with the character node
  40.         nodeProperty.append(jointModel)
  41.  
  42.     # Finally, attempt characterization, complaining to the user if unsuccessful
  43.     if not character.SetCharacterizeOn(useBiped):
  44.         errorMessage = character.GetCharacterizeError()
  45.         FBMessageBox('Characterization Error!', errorMessage, 'OK')
  46.         return False
  47.  
  48.     # If nothing went wrong, inform the user of our success
  49.     return True
  50.  
  51. def ClearCharacterization(character):
  52.     '''
  53.    Disables and resets characterization for the given character, removing all
  54.    model mappings.
  55.    '''
  56.     character.SetCharacterizeOff()
  57.     for prop in character.PropertyList:
  58.         if prop.Name.endswith('Link') and isinstance(prop, FBPropertyListObject):
  59.             prop.removeAll()
  60.  
  61. def FindDescendantModel(topModel, searchModelName):
  62.     '''
  63.    Recursively searches the given branch for a model with the specified short
  64.    name, returning that model if found. Otherwise, returns None.
  65.    '''
  66.     if topModel.Name == searchModelName:
  67.         return topModel
  68.  
  69.     for childModel in topModel.Children:
  70.         foundModel = FindDescendantModel(childModel, searchModelName)
  71.         if foundModel:
  72.             return foundModel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement