Advertisement
Metssfm

fix_bone_tail()

Aug 17th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. def fix_bone_tail(edit_bones, bone=None):
  2.     # Recursive function to go through a bone hierarchy and move the bone tails to useful positions.
  3.     # Requires the armature to be in edit mode because I don't want to switch between object/edit in a recursive function.
  4.    
  5.     if(len(edit_bones) == 0):
  6.         raise W3ImporterError("Armature needs to be in edit mode for fix_bone_tail().")
  7.    
  8.     # Dictionary to help connect the bone tails to specific bone heads
  9.     connect_dict = {
  10.         'l_shoulder'            : 'l_bicep'     ,
  11.         'l_bicep'               : 'l_elbowRoll' ,
  12.         'l_elbowRoll'           : 'l_hand'      ,
  13.         'l_hand'                : 'l_middle1'   ,
  14.         'l_thigh'               : 'l_shin'      ,
  15.         'l_shin'                : 'l_foot'      ,
  16.         'l_foot'                : 'l_toe'       ,
  17.         'l_index_knuckleRoll'   : 'l_index2'    ,
  18.         'l_middle_knuckleRoll'  : 'l_middle2'   ,
  19.         'l_ring_knuckleRoll'    : 'l_ring2'     ,
  20.  
  21.         'r_shoulder'            : 'r_bicep'     ,
  22.         'r_bicep'               : 'r_elbowRoll' ,
  23.         'r_elbowRoll'           : 'r_hand'      ,
  24.         'r_hand'                : 'r_middle1'   ,
  25.         'r_thigh'               : 'r_shin'      ,
  26.         'r_shin'                : 'r_foot'      ,
  27.         'r_foot'                : 'r_toe'       ,
  28.         'r_index_knuckleRoll'   : 'r_index2'    ,
  29.         'r_middle_knuckleRoll'  : 'r_middle2'   ,
  30.         'r_ring_knuckleRoll'    : 'r_ring2'     ,
  31.  
  32.         'pelvis'                : 'None'        ,
  33.         'torso'                 : 'torso2'      ,
  34.         'torso2'                : 'torso3'      ,
  35.         'torso3'                : 'neck'        ,
  36.         'neck'                  : 'head'        ,
  37.         'head'                  : 'None'        ,
  38.         'jaw'                   : 'chin'        ,
  39.         'tongue2'               : 'lowwer_lip'  ,
  40.     }
  41.    
  42.     if(bone == None):
  43.         bone=edit_bones[0]
  44.    
  45.     # If a bone is in connect_dict, just move its tail to the bone specified in the dictionary.
  46.     if(bone.name in connect_dict):
  47.         target = edit_bones.get(connect_dict[bone.name])
  48.         if(target != None):
  49.             bone.tail = target.head
  50.     else:
  51.         # For bones with children, we'll just connect the bone to the first child.
  52.         if(len(bone.children) > 0):
  53.             bone.tail = bone.children[0].head
  54.        
  55.         # For bones with no children...
  56.         else:
  57.             if(bone.parent != None):
  58.                 # Get the parent's head->tail vector
  59.                 parent_vec = bone.parent.tail - bone.parent.head
  60.                 # If the bone has siblings, set the scale to an arbitrary amount.
  61.                 if( len(bone.parent.children) > 1):
  62.                     scale = 0.1
  63.                     if('tongue' in bone.name): scale = 0.03
  64.                     bone.tail = bone.head + parent_vec.normalized() * scale # Todo change this number to .05 if the apply_transforms() gets fixed.
  65.                 # If no siblings, just use the parents transforms.
  66.                 else:
  67.                     bone.tail = bone.head + parent_vec
  68.                
  69.                 # Special treatment for the children of some bones
  70.                 if(bone.parent.name in ['head', 'jaw']):
  71.                     bone.tail = bone.head+Vector((0, 0, .02))
  72.    
  73.     # Recursion over this bone's children.
  74.     for c in bone.children:
  75.         fix_bone_tail(edit_bones, c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement