Advertisement
Guest User

SM Rig Corrector

a guest
Nov 5th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import bpy, math
  2. from mathutils import Vector
  3.  
  4. identifiers = ['L', 'R']
  5. armature = bpy.context.active_object
  6. new_constraints = []
  7.  
  8. bpy.ops.object.mode_set(mode='EDIT')
  9.  
  10. for b in armature.data.edit_bones:
  11.     if b.name[0] in identifiers and '.' not in b.name:
  12.         b.name = b.name[1:] + '.' + b.name[0]
  13.    
  14.     if 'Foot.' in b.name:
  15.         # prep parent with slight bend
  16.         b.parent.tail -= Vector((0, 0, 3))
  17.        
  18.         # create new IK bone
  19.         ikb = armature.data.edit_bones.new('Foot_IK.' + b.name[-1])
  20.         ikb.head = b.head
  21.         ikb.tail = ikb.head - Vector((0, 18, 0))
  22.        
  23.         # and target
  24.         ikt = armature.data.edit_bones.new('Foot_IK_Target.' + b.name[-1])
  25.         ikt.head = b.parent.head + Vector((0, 0, 20))
  26.         ikt.tail = ikt.head + Vector((0, 0, 10))
  27.        
  28.         # setup constraint
  29.         ikc = armature.pose.bones[b.parent.name].constraints.new('IK')
  30.         ikc.target = armature
  31.         ikc.subtarget = ikb.name
  32.         ikc.pole_target = armature
  33.         ikc.pole_subtarget = ikt.name
  34.         ikc.pole_angle = -math.pi / 2
  35.         ikc.chain_count = 2
  36.         new_constraints.append(ikc)
  37.        
  38.     elif 'Hand.' in b.name:
  39.         # prep parent with slight bend
  40.         b.parent.head -= Vector((0, 0, 2))
  41.        
  42.         # create new IK bone
  43.         ikb = armature.data.edit_bones.new('Hand_IK.' + b.name[-1])
  44.         ikb.head = b.head
  45.         ikb.tail = ikb.head - Vector((0, 18, 0))
  46.        
  47.         # and target
  48.         ikt = armature.data.edit_bones.new('Hand_IK_Target.' + b.name[-1])
  49.         ikt.head = b.parent.head - Vector((0, 0, 20))
  50.         ikt.tail = ikt.head - Vector((0, 0, 10))
  51.        
  52.         # setup constraint
  53.         ikc = armature.pose.bones[b.parent.name].constraints.new('IK')
  54.         ikc.target = armature
  55.         ikc.subtarget = ikb.name
  56.         ikc.pole_target = armature
  57.         ikc.pole_subtarget = ikt.name
  58.         ikc.pole_angle = -math.pi / 2
  59.         ikc.chain_count = 2
  60.         new_constraints.append(ikc)
  61.        
  62.  
  63. bpy.ops.object.mode_set(mode='POSE')
  64.  
  65. # hack to force constraint data to refresh
  66. for nc in new_constraints:
  67.     nc.iterations = nc.iterations
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement