[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [24919] trunk/blender/release/scripts/ modules/rigify.py: basic support for Sintels arm rig with property driven ik/ fk switching and stretch bones to display connections, still need to do shoulder hinge and hand delta offset bones

Campbell Barton ideasman42 at gmail.com
Thu Nov 26 14:36:15 CET 2009


Revision: 24919
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=24919
Author:   campbellbarton
Date:     2009-11-26 14:36:15 +0100 (Thu, 26 Nov 2009)

Log Message:
-----------
basic support for Sintels arm rig with property driven ik/fk switching and stretch bones to display connections, still need to do shoulder hinge and hand delta offset bones

Modified Paths:
--------------
    trunk/blender/release/scripts/modules/rigify.py

Modified: trunk/blender/release/scripts/modules/rigify.py
===================================================================
--- trunk/blender/release/scripts/modules/rigify.py	2009-11-26 12:22:43 UTC (rev 24918)
+++ trunk/blender/release/scripts/modules/rigify.py	2009-11-26 13:36:15 UTC (rev 24919)
@@ -18,6 +18,7 @@
 
 import bpy
 from functools import reduce
+from Mathutils import Vector
 
 # TODO, have these in a more general module
 from rna_prop_ui import rna_idprop_ui_get, rna_idprop_ui_prop_get
@@ -36,31 +37,47 @@
     else:
         bone = arm.bones[bone_name]
     
-    return obj, arm, pbone, bone
+    return arm, pbone, bone
 
 def bone_basename(name):
     return name.split(".")[0]
 
+def copy_bone_simple(arm, from_bone, name):
+    ebone = arm.edit_bones[from_bone]
+    ebone_new = arm.edit_bones.new(name)
+    ebone_new.head = ebone.head
+    ebone_new.tail = ebone.tail
+    ebone_new.roll = ebone.roll
+    return ebone_new
+
+
 def add_stretch_to(obj, from_name, to_name, name):
     '''
     Adds a bone that stretches from one to another
     '''
     
-    is_editmode = (obj.mode == 'EDIT')
-    if not is_editmode:
-        bpy.ops.object.mode_set(mode='EDIT')
+    mode_orig = obj.mode
+    bpy.ops.object.mode_set(mode='EDIT')
 
     arm = obj.data
     stretch_ebone = arm.edit_bones.new(name)
     stretch_name = stretch_ebone.name
+    del name
+    
     head = stretch_ebone.head = arm.edit_bones[from_name].head.copy()
-    tail = stretch_ebone.tail = arm.edit_bones[to_name].head.copy()
+    #tail = stretch_ebone.tail = arm.edit_bones[to_name].head.copy()
+    
+    # annoying exception for zero length bones, since its using stretch_to the rest pose doesnt really matter
+    #if (head - tail).length < 0.1:
+    if 1:
+        tail = stretch_ebone.tail = arm.edit_bones[from_name].tail.copy()
 
 
     # Now for the constraint
     bpy.ops.object.mode_set(mode='OBJECT')
     from_pbone = obj.pose.bones[from_name]
     to_pbone = obj.pose.bones[to_name]
+    
     stretch_pbone = obj.pose.bones[stretch_name]
     
     con = stretch_pbone.constraints.new('COPY_LOCATION')
@@ -70,22 +87,63 @@
     con = stretch_pbone.constraints.new('STRETCH_TO')
     con.target = obj
     con.subtarget = to_name   
-    con.original_length = (head-tail).length
+    con.original_length = (head - tail).length
     con.keep_axis = 'PLANE_X'
     con.volume = 'NO_VOLUME'
 
-    if is_editmode:
-        bpy.ops.object.mode_set(mode='EDIT')
-    #else:
-    #    bpy.ops.object.mode_set(mode='OBJECT')
+    bpy.ops.object.mode_set(mode=mode_orig)
 
 
+def add_pole_target_bone(obj, base_name, name, mode='CROSS'):
+    '''
+    Does not actually create a poll target, just the bone to use as a poll target
+    '''
+    mode_orig = obj.mode
+    bpy.ops.object.mode_set(mode='EDIT')
+    
+    arm = obj.data
+    
+    poll_ebone = arm.edit_bones.new(base_name + "_poll")
+    base_ebone = arm.edit_bones[base_name]
+    poll_name = poll_ebone.name
+    parent_ebone = base_ebone.parent
+    
+    base_head = base_ebone.head.copy()
+    base_tail = base_ebone.tail.copy()
+    base_dir = base_head - base_tail
+    
+    parent_head = parent_ebone.head.copy()
+    parent_tail = parent_ebone.tail.copy()
+    parent_dir = parent_head - parent_tail
+    
+    distance = (base_dir.length + parent_dir.length)
+    
+    if mode == 'CROSS':
+        offset = base_dir.copy().normalize() - parent_dir.copy().normalize()
+        offset.length = distance
+    else:
+        offset = Vector(0,0,0)
+        if mode[0]=="+":
+            val = distance
+        else:
+            val = -distance
+        
+        setattr(offset, mode[1].lower(), val)
+    
+    poll_ebone.head = base_head + offset
+    poll_ebone.tail = base_head + (offset * (1.0 - (1.0 / 4.0)))
+    
+    bpy.ops.object.mode_set(mode=mode_orig)
+    
+    return poll_name
+
+
 def gen_finger(obj, orig_bone_name):
     
     # *** EDITMODE
     
     # get assosiated data 
-    obj, arm, orig_pbone, orig_ebone = get_bone_data(obj, orig_bone_name)
+    arm, orig_pbone, orig_ebone = get_bone_data(obj, orig_bone_name)
     
     obj.animation_data_create() # needed if its a new armature with no keys
     
@@ -121,7 +179,7 @@
     driver_bone_pairs = []
 
     for child_bone_name in children:
-        obj, arm, pbone_child, child_ebone = get_bone_data(obj, child_bone_name)
+        arm, pbone_child, child_ebone = get_bone_data(obj, child_bone_name)
         
         # finger.02 --> finger_driver.02
         driver_bone_name = child_bone_name.split('.')
@@ -157,17 +215,17 @@
     bpy.ops.object.mode_set(mode='OBJECT')
     
     
-    obj, arm, orig_pbone, orig_bone = get_bone_data(obj, orig_bone_name)
-    obj, arm, control_pbone, control_bone= get_bone_data(obj, control_bone_name)
+    arm, orig_pbone, orig_bone = get_bone_data(obj, orig_bone_name)
+    arm, control_pbone, control_bone= get_bone_data(obj, control_bone_name)
     
     
     # only allow Y scale
     control_pbone.lock_scale = (True, False, True)
     
-    control_pbone["bend_ratio"]= 0.4
+    control_pbone["bend_ratio"] = 0.4
     prop = rna_idprop_ui_prop_get(control_pbone, "bend_ratio", create=True)
-    prop["min"] = 0.0
-    prop["max"] = 1.0
+    prop["soft_min"] = 0.0
+    prop["soft_max"] = 1.0
     
     con = orig_pbone.constraints.new('COPY_LOCATION')
     con.target = obj
@@ -191,7 +249,7 @@
         if i==2:
             break
         
-        obj, arm, driver_pbone, driver_bone = get_bone_data(obj, driver_bone_name)
+        arm, driver_pbone, driver_bone = get_bone_data(obj, driver_bone_name)
         
         driver_pbone.rotation_mode = 'YZX'
         fcurve_driver = driver_pbone.driver_add("rotation_euler", 0)
@@ -221,7 +279,7 @@
         elif i==1:
             driver.expression = '(-scale+1.0)*pi*2.0*br'
         
-        obj, arm, child_pbone, child_bone = get_bone_data(obj, child_bone_name)
+        arm, child_pbone, child_bone = get_bone_data(obj, child_bone_name)
 
         # only allow X rotation
         driver_pbone.lock_rotation = child_pbone.lock_rotation = (False, True, True)
@@ -234,6 +292,8 @@
     Use this bone to define a delta thats applied to its child in pose mode.
     '''
     
+    arm = obj.data
+    
     mode_orig = obj.mode
     bpy.ops.object.mode_set(mode='OBJECT')
     
@@ -244,47 +304,75 @@
         print("only 1 child supported for delta")
     
     child_name = children[0].name
+    arm, child_pbone, child_bone = get_bone_data(obj, child_name)
     
-    delta_head = delta_pbone.head.copy()
-    delta_tail = delta_pbone.tail.copy()
-    delta_matrix = delta_pbone.matrix.copy()
+    delta_phead = delta_pbone.head.copy()
+    delta_ptail = delta_pbone.tail.copy()
+    delta_pmatrix = delta_pbone.matrix.copy()
     
+    child_phead = child_pbone.head.copy()
+    child_ptail = child_pbone.tail.copy()
+    child_pmatrix = child_pbone.matrix.copy()
+    
+    
     children = delta_pbone.children
 
     bpy.ops.object.mode_set(mode='EDIT')
     
-    arm = obj.data
+    delta_ebone = arm.edit_bones[delta_name]
+    child_ebone = arm.edit_bones[child_name]
     
-    # XXX -probably should allow via the UI
-    for ebone in arm.edit_bones:
-        ebone.selected = ebone.head_selected = ebone.tail_selected = False
+    delta_head = delta_ebone.head.copy()
+    delta_tail = delta_ebone.tail.copy()    
     
-    # Select for deleting
-    delta_ebone = arm.edit_bones[delta_name]
-    delta_ebone.selected = delta_ebone.head_selected = delta_ebone.tail_selected = True
+    # arm, parent_pbone, parent_bone = get_bone_data(obj, delta_name)
+    child_head = child_ebone.head.copy()
+    child_tail = child_ebone.tail.copy()
     
-    bpy.ops.armature.delete()
+    arm.edit_bones.remove(delta_ebone)
+    del delta_ebone # cant use thz
     
     bpy.ops.object.mode_set(mode='OBJECT')
     
     
     # Move the child bone to the deltas location
     obj.animation_data_create()
-    child_pbone = obj.pose[child_name]
+    child_pbone = obj.pose.bones[child_name]
     
     # ------------------- drivers
-    fcurve_driver = child_pbone.driver_add("rotation_euler", 0)
-    #fcurve_driver = obj.animation_data.drivers[-1] # XXX, WATCH THIS
-    driver = fcurve_driver.driver
-    driver.type = 'AVERAGE'
-    mod = driver.modifiers.new('GENERATOR')
     
+    child_pbone.rotation_mode = 'XYZ'
     
+    rot =  delta_pmatrix.invert().rotationPart() * child_pmatrix.rotationPart()
+    rot = rot.invert().toEuler()
     
-    obj, arm, parent_pbone, parent_bone = get_bone_data(obj, delta_name)
+    fcurve_drivers = child_pbone.driver_add("rotation_euler", -1)
+    for i, fcurve_driver in enumerate(fcurve_drivers):
+        driver = fcurve_driver.driver
+        driver.type = 'AVERAGE'
+        #mod = fcurve_driver.modifiers.new('GENERATOR')
+        mod = fcurve_driver.modifiers[0]
+        mod.poly_order = 1
+        mod.coefficients[0] = rot[i]
+        mod.coefficients[1] = 0.0
+    
+    # tricky, find the transform to drive the bone to this location.
+    delta_head_offset =  child_pmatrix.rotationPart() * (delta_phead - child_phead)
+    
+    fcurve_drivers = child_pbone.driver_add("location", -1)
+    for i, fcurve_driver in enumerate(fcurve_drivers):
+        driver = fcurve_driver.driver
+        driver.type = 'AVERAGE'
+        #mod = fcurve_driver.modifiers.new('GENERATOR')
+        mod = fcurve_driver.modifiers[0]
+        mod.poly_order = 1
+        mod.coefficients[0] = delta_head_offset[i]
+        mod.coefficients[1] = 0.0
+    
+    
+    # arm, parent_pbone, parent_bone = get_bone_data(obj, delta_name)
     bpy.ops.object.mode_set(mode='EDIT')
     
-    
     bpy.ops.object.mode_set(mode=mode_orig)
 
 
@@ -293,6 +381,11 @@
     the bone with the 'arm' property is the upper arm, this assumes a chain as follows.
     [shoulder, upper_arm, forearm, hand]
     ...where this bone is 'upper_arm'
+    
+    there are 3 chains
+    - Original
+    - IK, MCH-%s_ik
+    - IKSwitch, MCH-%s ()
     """
     
     def validate_chain():
@@ -300,8 +393,8 @@
         Sanity check and return the arm as a list of bone names.
         '''
         # do a sanity check
-        obj, arm, orig_pbone, orig_ebone = get_bone_data(obj, orig_bone_name)
-        shoulder_pbone = arm_pbone.parent
+        arm, orig_pbone, orig_ebone = get_bone_data(obj, orig_bone_name)
+        shoulder_pbone = orig_pbone.parent
         
         if not shoulder_pbone:

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list