[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [27089] trunk/blender/release/scripts/ modules/rigify: Made a more generic rig type for giving animators direct control over shape keys .

Nathan Vegdahl cessen at cessen.com
Mon Feb 22 19:07:16 CET 2010


Revision: 27089
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=27089
Author:   cessen
Date:     2010-02-22 19:07:16 +0100 (Mon, 22 Feb 2010)

Log Message:
-----------
Made a more generic rig type for giving animators direct control over shape keys.  With this rig type you can attach shape keys both to the transforms of the bone and any number custom properties on the bone.

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

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

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

Modified: trunk/blender/release/scripts/modules/rigify/leg_quadruped.py
===================================================================
--- trunk/blender/release/scripts/modules/rigify/leg_quadruped.py	2010-02-22 16:34:27 UTC (rev 27088)
+++ trunk/blender/release/scripts/modules/rigify/leg_quadruped.py	2010-02-22 18:07:16 UTC (rev 27089)
@@ -129,6 +129,9 @@
 
     # keep the foot_ik as the parent
     ik_chain.toe_e.connected = False
+    
+    # Foot uses pose space, not local space, for translation
+    ik_chain.foot_e.local_location = False
 
     # must be after disconnecting the toe
     ik_chain.foot_e.align_orientation(mt_chain.toe_e)
@@ -259,6 +262,8 @@
     con = ik.foot_roll_01_p.constraints.new('COPY_ROTATION')
     con.target = obj
     con.subtarget = ik.foot_roll
+    con.target_space = 'LOCAL'
+    con.owner_space = 'LOCAL'
 
 
     # IK

Copied: trunk/blender/release/scripts/modules/rigify/shape_key_control.py (from rev 27075, trunk/blender/release/scripts/modules/rigify/shape_key_transforms.py)
===================================================================
--- trunk/blender/release/scripts/modules/rigify/shape_key_control.py	                        (rev 0)
+++ trunk/blender/release/scripts/modules/rigify/shape_key_control.py	2010-02-22 18:07:16 UTC (rev 27089)
@@ -0,0 +1,320 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+#  This program is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License
+#  as published by the Free Software Foundation; either version 2
+#  of the License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software Foundation,
+#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8 compliant>
+
+import bpy
+from rigify import RigifyError
+from rigify_utils import copy_bone_simple
+from rna_prop_ui import rna_idprop_ui_prop_get
+
+#METARIG_NAMES = ("cpy",)
+RIG_TYPE = "shape_key_control"
+
+
+def addget_shape_key(obj, name="Key"):
+    """ Fetches a shape key, or creates it if it doesn't exist
+    """
+    # Create a shapekey set if it doesn't already exist
+    if obj.data.shape_keys is None:
+        shape = obj.add_shape_key(name="Basis", from_mix=False)
+        obj.active_shape_key_index = 0
+
+    # Get the shapekey, or create it if it doesn't already exist
+    if name in obj.data.shape_keys.keys:
+        shape_key = obj.data.shape_keys.keys[name]
+    else:
+        shape_key = obj.add_shape_key(name=name, from_mix=False)
+
+    return shape_key
+
+
+def addget_shape_key_driver(obj, name="Key"):
+    """ Fetches the driver for the shape key, or creates it if it doesn't
+        already exist.
+    """
+    driver_path = 'keys["' + name + '"].value'
+    fcurve = None
+    driver = None
+    new = False
+    if obj.data.shape_keys.animation_data is not None:
+        for driver_s in obj.data.shape_keys.animation_data.drivers:
+            if driver_s.data_path == driver_path:
+                fcurve = driver_s
+    if fcurve == None:
+        fcurve = obj.data.shape_keys.keys[name].driver_add("value", 0)
+        fcurve.driver.type = 'AVERAGE'
+        new = True
+
+    return fcurve, new
+
+
+# TODO:
+def metarig_template():
+    # generated by rigify.write_meta_rig
+    #bpy.ops.object.mode_set(mode='EDIT')
+    #obj = bpy.context.active_object
+    #arm = obj.data
+    #bone = arm.edit_bones.new('Bone')
+    #bone.head[:] = 0.0000, 0.0000, 0.0000
+    #bone.tail[:] = 0.0000, 0.0000, 1.0000
+    #bone.roll = 0.0000
+    #bone.connected = False
+    #
+    #bpy.ops.object.mode_set(mode='OBJECT')
+    #pbone = obj.pose.bones['Bone']
+    #pbone['type'] = 'copy'
+    pass
+
+
+def metarig_definition(obj, orig_bone_name):
+    bone = obj.data.bones[orig_bone_name]
+    return [bone.name]
+
+
+def main(obj, definitions, base_names, options):
+    """ A rig that drives shape keys with the local transforms and/or custom
+        properties of a single bone.
+        A different shape can be driven by the negative value of a transform as
+        well by giving a comma-separated list of two shapes.
+    
+        Required options:
+            mesh:  name of mesh object(s) to add/get shapekeys to/from
+                   (if multiple objects, make a comma-separated list)
+        Optional options:
+            loc_<x/y/z>:       name of the shape key to tie to translation of the bone
+            loc_<x/y/z>_fac:   default multiplier of the bone influence on the shape key
+            rot_<x/y/z>:       name of the shape key to tie to rotation of the bone
+            rot_<x/y/z>_fac:   default multiplier of the bone influence on the shape key
+            scale_<x/y/z>:     name of the shape key to tie to scale of the bone
+            scale_<x/y/z>_fac: default multiplier of the bone influence on the shape key
+            shape_key_sliders:     comma-separated list of custom properties to create sliders out of for driving shape keys
+            <custom_prop>:         for each property listed in shape_key_sliders, specify a shape key for it to drive
+            
+    """
+    
+    bpy.ops.object.mode_set(mode='EDIT')
+    eb = obj.data.edit_bones
+    pb = obj.pose.bones
+
+    org_bone = definitions[0]
+    
+    # Options
+    req_options = ["mesh"]
+    for option in req_options:
+        if option not in options:
+            raise RigifyError("'%s' rig type requires a '%s' option (bone: %s)" % (RIG_TYPE, option, base_names[definitions[0]]))
+
+    meshes = options["mesh"].replace(" ", "").split(",")
+    
+    bone = copy_bone_simple(obj.data, org_bone, base_names[org_bone], parent=True).name
+    
+    bpy.ops.object.mode_set(mode='OBJECT')
+    
+    # Set rotation mode and axis locks
+    pb[bone].rotation_mode = pb[org_bone].rotation_mode
+    pb[bone].lock_location = tuple(pb[org_bone].lock_location)
+    pb[bone].lock_rotation = tuple(pb[org_bone].lock_rotation)
+    pb[bone].lock_rotation_w = pb[org_bone].lock_rotation_w
+    pb[bone].lock_rotations_4d = pb[org_bone].lock_rotations_4d
+    pb[bone].lock_scale = tuple(pb[org_bone].lock_scale)
+    
+    # List of rig options for specifying shape keys
+    # Append '_fac' to the end for the name of the corresponding 'factor
+    # default' option for that shape
+    shape_key_options = ["loc_x",
+                         "loc_y",
+                         "loc_z",
+                         "rot_x",
+                         "rot_y",
+                         "rot_z",
+                         "scale_x",
+                         "scale_y",
+                         "scale_z"]
+                         
+    driver_paths = {"loc_x":".location[0]",
+                    "loc_y":".location[1]",
+                    "loc_z":".location[2]",
+                    "rot_x":".rotation_euler[0]",
+                    "rot_y":".rotation_euler[1]",
+                    "rot_z":".rotation_euler[2]",
+                    "qrot_x":".rotation_quaternion[1]",
+                    "qrot_y":".rotation_quaternion[2]",
+                    "qrot_z":".rotation_quaternion[3]",
+                    "scale_x":".scale[0]",
+                    "scale_y":".scale[1]",
+                    "scale_z":".scale[2]"}
+    
+    # Create the shape keys and drivers for transforms
+    shape_info = []
+    for option in shape_key_options:
+        if option in options:
+            shape_names = options[option].replace(" ", "").split(",")
+            
+            var_name = bone.replace(".","").replace("-","_") + "_" + option
+            # Different RNA paths for euler vs quat
+            if option in (shape_key_options[3:6]+shape_key_options[12:15]) \
+            and pb[bone].rotation_mode == 'QUATERNION':
+                var_path = driver_paths['q' + option]
+            else:
+                var_path = driver_paths[option]
+            
+            if (option+"_fac") in options:
+                fac = options[option+"_fac"]
+            else:
+                fac = 1.0
+            
+            # Positive
+            if shape_names[0] != "":
+                # Different expressions for loc/rot/scale and positive/negative
+                if option in shape_key_options[:3]:
+                    # Location
+                    expression = var_name + " * " + str(fac)
+                elif option in shape_key_options[3:6]:
+                    # Rotation
+                    # Different expressions for euler vs quats
+                    if pb[bone].rotation_mode == 'QUATERNION':
+                        expression = "2 * asin(" + var_name + ") * " + str(fac)
+                    else:
+                        expression = var_name + " * " + str(fac)
+                elif option in shape_key_options[6:9]:
+                    # Scale
+                    expression = "(1.0 - " + var_name + ") * " + str(fac) + " * -2"
+                shape_name = shape_names[0]
+                create_shape_and_driver(obj, bone, meshes, shape_name, var_name, var_path, expression)
+                
+            # Negative
+            if shape_names[0] != "" and len(shape_names) > 1:
+                # Different expressions for loc/rot/scale and positive/negative
+                if option in shape_key_options[:3]:
+                    # Location
+                    expression = var_name + " * " + str(fac) + " * -1"
+                elif option in shape_key_options[3:6]:
+                    # Rotation
+                    # Different expressions for euler vs quats
+                    if pb[bone].rotation_mode == 'QUATERNION':
+                        expression = "-2 * asin(" + var_name + ") * " + str(fac)
+                    else:
+                        expression = var_name + " * " + str(fac) + " * -1"
+                elif option in shape_key_options[6:9]:
+                    # Scale
+                    expression = "(1.0 - " + var_name + ") * " + str(fac) + " * 2"
+                shape_name = shape_names[1]
+                create_shape_and_driver(obj, bone, meshes, shape_name, var_name, var_path, expression)
+    

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list