rna function
authorCampbell Barton <ideasman42@gmail.com>
Fri, 4 Dec 2009 02:32:34 +0000 (02:32 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Fri, 4 Dec 2009 02:32:34 +0000 (02:32 +0000)
 editbone.align(vector), to align the bones z axis to a localspace direction.

finished leg rig pose mode data

release/scripts/modules/rigify/__init__.py
release/scripts/modules/rigify/leg.py
source/blender/editors/armature/editarmature.c
source/blender/makesrna/intern/makesrna.c
source/blender/makesrna/intern/rna_armature.c
source/blender/makesrna/intern/rna_armature_api.c [new file with mode: 0644]
source/blender/makesrna/intern/rna_internal.h

index 107823f3720a218334c2e2c0bc81d4fa27dfe290..e960d06a1b29db78d7a60d9147871aa49743b6f3 100644 (file)
@@ -55,8 +55,22 @@ def _bone_class_instance_update(self):
             setattr(self, member + "_e", ebones.get(name, None))
 
 
+def _bone_class_instance_rename(self, attr, new_name):
+    ''' Rename bones, editmode only
+    '''
+    
+    if self.obj.mode != 'EDIT':
+        raise Exception("Only rename in editmode supported")
+    
+    ebone = getattr(self, attr + "_e")
+    ebone.name = new_name
+    
+    # we may not get what is asked for so get the name from the editbone
+    setattr(self, attr, ebone.name)
+    
+
 def _bone_class_instance_copy(self, from_prefix="", to_prefix=""):
-    orig_name_ls = []
+    from_name_ls = []
     new_name_ls = []
     new_slot_ls = []
 
@@ -66,16 +80,23 @@ def _bone_class_instance_copy(self, from_prefix="", to_prefix=""):
         # orig_names[attr] = bone_name_orig
         
         # insert prefix
-        bone_name = from_prefix + bone_name_orig
-        ebone.name = to_prefix + bone_name
-        bone_name = ebone.name # cant be sure we get what we ask for
+        if from_prefix:
+            bone_name = from_prefix + bone_name_orig
+            ebone.name = bone_name
+            bone_name = ebone.name # cant be sure we get what we ask for
+        else:
+            bone_name = bone_name_orig
+            
         setattr(self, attr, bone_name)
 
         new_slot_ls.append(attr)
-        orig_name_ls.append(bone_name)
-        new_name_ls.append(bone_name_orig)
-
-    new_bones = copy_bone_simple_list(self.obj.data, orig_name_ls, new_name_ls, True)
+        from_name_ls.append(bone_name)
+        new_name_ls.append(to_prefix + bone_name_orig)
+    print("RUN!")
+    print("from_name_ls", from_name_ls)
+    print("new_name_ls", new_name_ls)
+    
+    new_bones = copy_bone_simple_list(self.obj.data, from_name_ls, new_name_ls, True)
     new_bc = bone_class_instance(self.obj, new_slot_ls)
 
     for i, attr in enumerate(new_slot_ls):
@@ -95,7 +116,7 @@ def _bone_class_instance_blend(self, from_bc, to_bc, target_bone=None, target_pr
     
     XXX - toggles editmode, need to re-validate all editbones :(
     '''
-    if self.attr_names != to_bc.attr_names:
+    if self.attr_names != from_bc.attr_names or self.attr_names != to_bc.attr_names:
         raise Exception("can only blend between matching chains")
 
     obj = self.obj
@@ -127,6 +148,18 @@ def _bone_class_instance_blend(self, from_bc, to_bc, target_bone=None, target_pr
         new_pbone = getattr(self, attr + "_p")
         from_bone_name = getattr(from_bc, attr)
         to_bone_name = getattr(to_bc, attr)
+
+        a = getattr(from_bc, attr+"_p")
+        b = getattr(to_bc, attr+"_p")
+        
+        if a.name != from_bone_name:
+            raise Exception("a")
+        if b.name != to_bone_name:
+            raise Exception("b")
+        if from_bone_name == to_bone_name:
+            print(from_bc, to_bc)
+            raise Exception("Matching from/to bone names:" + from_bone_name)
+        
         if use_loc:
             con = new_pbone.constraints.new('COPY_LOCATION')
             con.target = obj
@@ -144,6 +177,7 @@ def _bone_class_instance_blend(self, from_bc, to_bc, target_bone=None, target_pr
             blend_target(driver)
         
         if use_rot:
+            print(from_bone_name, to_bone_name)
             con = new_pbone.constraints.new('COPY_ROTATION')
             con.target = obj
             con.subtarget = from_bone_name
@@ -173,6 +207,7 @@ def bone_class_instance(obj, slots, name="BoneContainer"):
         "obj":obj, \
         "attr_names":attr_names, \
         "update":_bone_class_instance_update, \
+        "rename":_bone_class_instance_rename, \
         "copy":_bone_class_instance_copy, \
         "blend":_bone_class_instance_blend, \
     }
index 535ed1061f7fa978f610b4376f1af1ca0c8c4ee4..54dc76dafcfbc63acf7011bdd51db45a3ce15c89 100644 (file)
@@ -103,7 +103,7 @@ def main(obj, orig_bone_name):
     mt_chain = bone_class_instance(obj, ["thigh", "shin", "foot", "toe"])
     mt = bone_class_instance(obj, ["hips", "heel"])
     #ex = bone_class_instance(obj, [""])
-    ex = bone_class_instance(obj, ["thigh_socket", "foot_roll_1", "foot_roll_2", "foot_roll_3"])
+    ex = bone_class_instance(obj, ["thigh_socket", "thigh_hinge", "foot_roll_1", "foot_roll_2", "foot_roll_3"])
     # children of ik_foot
     ik = bone_class_instance(obj, ["foot", "foot_roll", "foot_roll_01", "foot_roll_02", "knee_target"])
     
@@ -132,22 +132,29 @@ def main(obj, orig_bone_name):
     ex.thigh_socket = ex.thigh_socket_e.name
     ex.thigh_socket_e.tail = ex.thigh_socket_e.head + Vector(0.0, 0.0, ex.thigh_socket_e.length / 4.0)
 
+    ex.thigh_hinge_e = copy_bone_simple(arm, mt_chain.thigh, "MCH-%s_hinge" % mt_chain.thigh, parent=True)
+    ex.thigh_hinge = ex.thigh_hinge_e.name
+    ex.thigh_hinge_e.tail = ex.thigh_hinge_e.head + Vector(0.0, 0.0, mt_chain.thigh_e.head.length)
+    ex.thigh_hinge_e.translate(Vector(-(mt.hips_e.head.x - mt_chain.thigh_e.head.x), 0.0, 0.0))
+    ex.thigh_hinge_e.length = mt.hips_e.length
+    
+
+
     # Make a new chain, ORG are the original bones renamed.
     fk_chain = mt_chain.copy(from_prefix="ORG-") # fk has no prefix!
     ik_chain = fk_chain.copy(to_prefix="MCH-")
+    
+    fk_chain.thigh_e.connected = False
+    fk_chain.thigh_e.parent = ex.thigh_hinge_e
 
     # fk_chain.thigh_socket_e.parent = MCH-leg_hinge
-    
+
     # simple rename
-    fk_chain.thigh_e.name = fk_chain.thigh_e.name + "_ik"
-    fk_chain.thigh = ik_chain.thigh_e.name
-    
-    fk_chain.shin_e.name = fk_chain.shin_e.name + "_ik"
-    fk_chain.shin = ik_chain.shin_e.name
-    
-    
+    ik_chain.rename("thigh", ik_chain.thigh + "_ik")
+    ik_chain.rename("shin", ik_chain.shin + "_ik")
+
     # ik foot, no parents
-    base_foot_name = ik_chain.foot # whatever the foot is called, use that!
+    base_foot_name = fk_chain.foot # whatever the foot is called, use that!
     ik.foot_e = copy_bone_simple(arm, fk_chain.foot, "%s_ik" % base_foot_name)
     ik.foot = ik.foot_e.name
     ik.foot_e.tail.z = ik.foot_e.head.z
@@ -175,20 +182,17 @@ def main(obj, orig_bone_name):
     del base_foot_name
     
     # rename 'MCH-toe' --> to 'toe_ik' and make the child of ik.foot_roll_01
-    fk_chain.toe_e.name = ik_chain.toe + "_ik"
-    fk_chain.toe = fk_chain.toe_e.name
-    fk_chain.toe_e.connected = True
-    fk_chain.toe_e.parent = ik.foot_roll_01_e
+    # ------------------ FK or IK?
+    ik_chain.rename("toe", fk_chain.toe + "_ik") # only fk for the basename
+    ik_chain.toe_e.connected = False
+    ik_chain.toe_e.parent = ik.foot_roll_01_e
     
     # re-parent ik_chain.foot to the 
-    fk_chain.foot_e.connected = False
-    fk_chain.foot_e.parent = ik.foot_roll_02_e
-    
+    ik_chain.foot_e.connected = False
+    ik_chain.foot_e.parent = ik.foot_roll_02_e
     
-    # add remaining ik helper bones.
     
     # knee target is the heel moved up and forward on its local axis
-    
     ik.knee_target_e = copy_bone_simple(arm, mt.heel, "knee_target")
     ik.knee_target = ik.knee_target_e.name
     offset = ik.knee_target_e.tail - ik.knee_target_e.head
@@ -199,6 +203,10 @@ def main(obj, orig_bone_name):
     ik.knee_target_e.length *= 0.5
     ik.knee_target_e.parent = ik.foot_e
 
+    # roll the bone to point up... could also point in the same direction as ik.foot_roll
+    # ik.foot_roll_02_e.matrix * Vector(0.0, 0.0, 1.0) # ACK!, no rest matrix in editmode
+    ik.foot_roll_01_e.align((0.0, 0.0, -1.0))
+
     bpy.ops.object.mode_set(mode='OBJECT')
 
     ik.update()
@@ -210,7 +218,75 @@ def main(obj, orig_bone_name):
     con = fk_chain.thigh_p.constraints.new('COPY_LOCATION')
     con.target = obj
     con.subtarget = ex.thigh_socket
+
+    # hinge
+    prop = rna_idprop_ui_prop_get(fk_chain.thigh_p, "hinge", create=True)
+    fk_chain.thigh_p["hinge"] = 0.5
+    prop["soft_min"] = 0.0
+    prop["soft_max"] = 1.0
+    
+    con = ex.thigh_hinge_p.constraints.new('COPY_ROTATION')
+    con.target = obj
+    con.subtarget = mt.hips
+    
+    # add driver
+    hinge_driver_path = fk_chain.thigh_p.path_to_id() + '["hinge"]'
+    
+    fcurve = con.driver_add("influence", 0)
+    driver = fcurve.driver
+    tar = driver.targets.new()
+    driver.type = 'AVERAGE'
+    tar.name = "var"
+    tar.id_type = 'OBJECT'
+    tar.id = obj
+    tar.rna_path = hinge_driver_path
+
+    mod = fcurve.modifiers[0]
+    mod.poly_order = 1
+    mod.coefficients[0] = 1.0
+    mod.coefficients[1] = -1.0
     
     
     # adds constraints to the original bones.
     mt_chain.blend(fk_chain, ik_chain, target_bone=ik.foot, target_prop="ik", use_loc=False)
+    
+    
+    # IK
+    con = ik_chain.shin_p.constraints.new('IK')
+    con.chain_length = 2
+    con.iterations = 500
+    con.pole_angle = -90.0 # XXX - in deg!
+    con.use_tail = True
+    con.use_stretch = True
+    con.use_target = True
+    con.use_rotation = False
+    con.weight = 1.0
+    
+    con.target = obj
+    con.subtarget = ik.foot
+    
+    con.pole_target = obj
+    con.pole_subtarget = ik.knee_target
+    
+    # foot roll
+    cons = [ \
+        (ik.foot_roll_01_p.constraints.new('COPY_ROTATION'), ik.foot_roll_01_p.constraints.new('LIMIT_ROTATION')), \
+        (ik.foot_roll_02_p.constraints.new('COPY_ROTATION'), ik.foot_roll_02_p.constraints.new('LIMIT_ROTATION'))
+    ]
+    
+    for con, con_l in cons:
+        con.target = obj
+        con.subtarget = ik.foot_roll
+        con.use_x, con.use_y, con.use_z = True, False, False
+        con.target_space = con.owner_space = 'LOCAL'
+        
+        con = con_l
+        con.use_limit_x, con.use_limit_y, con.use_limit_z = True, False, False
+        con.owner_space = 'LOCAL'
+        
+        if con_l is cons[-1][-1]:
+            con.minimum_x = 0.0
+            con.maximum_x = 180.0 # XXX -deg
+        else:
+            con.minimum_x = -180.0 # XXX -deg
+            con.maximum_x = 0.0
index a9efe4be9cb8255fe448a46390da6c00427be82c..4564ec4947792f601e226e40077e9ab505502d12 100644 (file)
@@ -2026,7 +2026,7 @@ float ED_rollBoneToVector(EditBone *bone, float new_up_axis[3])
 
 
 /* Set roll value for given bone -> Z-Axis Point up (original method) */
-void auto_align_ebone_zaxisup(Scene *scene, View3D *v3d, EditBone *ebone)
+static void auto_align_ebone_zaxisup(Scene *scene, View3D *v3d, EditBone *ebone)
 {
        float   delta[3], curmat[3][3];
        float   xaxis[3]={1.0f, 0.0f, 0.0f}, yaxis[3], zaxis[3]={0.0f, 0.0f, 1.0f};
@@ -2054,16 +2054,13 @@ void auto_align_ebone_zaxisup(Scene *scene, View3D *v3d, EditBone *ebone)
        mat3_to_vec_roll(diffmat, delta, &ebone->roll);
 }
 
-/* Set roll value for given bone -> Z-Axis point towards cursor */
-void auto_align_ebone_tocursor(Scene *scene, View3D *v3d, EditBone *ebone)
+void auto_align_ebone_topoint(EditBone *ebone, float *cursor)
 {
-       Object *obedit= scene->obedit; // XXX get from context
-       float   *cursor= give_cursor(scene, v3d);
        float   delta[3], curmat[3][3];
        float   mat[4][4], tmat[4][4], imat[4][4];
        float   rmat[4][4], rot[3];
        float   vec[3];
-       
+
        /* find the current bone matrix as a 4x4 matrix (in Armature Space) */
        sub_v3_v3v3(delta, ebone->tail, ebone->head);
        vec_roll_to_mat3(delta, ebone->roll, curmat);
@@ -2071,8 +2068,7 @@ void auto_align_ebone_tocursor(Scene *scene, View3D *v3d, EditBone *ebone)
        VECCOPY(mat[3], ebone->head);
        
        /* multiply bone-matrix by object matrix (so that bone-matrix is in WorldSpace) */
-       mul_m4_m4m4(tmat, mat, obedit->obmat);
-       invert_m4_m4(imat, tmat);
+       invert_m4_m4(imat, mat);
        
        /* find position of cursor relative to bone */
        mul_v3_m4v3(vec, imat, cursor);
@@ -2093,6 +2089,18 @@ void auto_align_ebone_tocursor(Scene *scene, View3D *v3d, EditBone *ebone)
        }
 }
 
+static void auto_align_ebone_tocursor(Scene *scene, View3D *v3d, EditBone *ebone)
+{
+       float cursor_local[3];
+       float   *cursor= give_cursor(scene, v3d);
+       float imat[3][3];
+
+       copy_m3_m4(imat, scene->obedit->obmat);
+       invert_m3(imat);
+       copy_v3_v3(cursor_local, cursor);
+       mul_m3_v3(imat, cursor_local);
+       auto_align_ebone_topoint(ebone, cursor_local);
+}
 
 static EnumPropertyItem prop_calc_roll_types[] = {
        {0, "GLOBALUP", 0, "Z-Axis Up", ""},
index 543e1f3ecc03a74b6d8d95d3ce8cc3d7208c9455..037b866edba7eebf396454053900f555ce342d0d 100644 (file)
@@ -1984,7 +1984,7 @@ RNAProcessItem PROCESS_ITEMS[]= {
        {"rna_action.c", "rna_action_api.c", RNA_def_action},
        {"rna_animation.c", "rna_animation_api.c", RNA_def_animation},
        {"rna_actuator.c", NULL, RNA_def_actuator},
-       {"rna_armature.c", NULL, RNA_def_armature},
+       {"rna_armature.c", "rna_armature_api.c", RNA_def_armature},
        {"rna_boid.c", NULL, RNA_def_boid},
        {"rna_brush.c", NULL, RNA_def_brush},
        {"rna_camera.c", NULL, RNA_def_camera},
index 0a5445d2642d47319995ae11a312fc0785bc386e..371f14be75372ce0b44ada19f31c7023c628ba29 100644 (file)
@@ -618,6 +618,8 @@ static void rna_def_edit_bone(BlenderRNA *brna)
        RNA_def_property_ui_text(prop, "Tail Selected", "");
        RNA_def_property_update(prop, 0, "rna_Armature_redraw_data");
 
+       RNA_api_armature_edit_bone(srna);
+
        RNA_define_verify_sdna(1);
 }
 
diff --git a/source/blender/makesrna/intern/rna_armature_api.c b/source/blender/makesrna/intern/rna_armature_api.c
new file mode 100644 (file)
index 0000000..31bd727
--- /dev/null
@@ -0,0 +1,66 @@
+/**
+ * ***** 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2009 Blender Foundation.
+ * All rights reserved.
+ *
+ * 
+ * Contributor(s): Campbell Barton
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+
+#include "RNA_define.h"
+#include "RNA_types.h"
+
+#ifdef RNA_RUNTIME
+
+#include <stddef.h>
+
+#include "BLI_blenlib.h"
+
+#include "ED_armature.h"
+
+void rna_EditBone_align(EditBone *ebo, float *no)
+{
+       if(!is_zero_v3(no)) {
+               float normal[3];
+               copy_v3_v3(normal, no);
+               normalize_v3(normal);
+               ebo->roll= ED_rollBoneToVector(ebo, normal);
+       }
+}
+
+#else
+
+void RNA_api_armature_edit_bone(StructRNA *srna)
+{
+       FunctionRNA *func;
+       PropertyRNA *parm;
+
+       func= RNA_def_function(srna, "align", "rna_EditBone_align");
+       RNA_def_function_ui_description(func, "Align the bone to a localspace vector.");
+       parm= RNA_def_float_vector(func, "vector", 3, NULL, -FLT_MAX, FLT_MAX, "Vector", "", -FLT_MAX, FLT_MAX);
+       RNA_def_property_flag(parm, PROP_REQUIRED);
+}
+
+#endif
index 64af7e07fd5ec1ce94e96b6a39e274b8107d8e0b..52945b6727588445412074a5047bfd951fed5b0c 100644 (file)
@@ -204,6 +204,7 @@ char *rna_TextureSlot_path(struct PointerRNA *ptr);
 /* API functions */
 
 void RNA_api_action(StructRNA *srna);
+void RNA_api_armature_edit_bone(StructRNA *srna);
 void RNA_api_drivers(StructRNA *srna);
 void RNA_api_image(struct StructRNA *srna);
 void RNA_api_keyconfig(struct StructRNA *srna);