Added more IK functionality to UI. You can now toggle IK for selected bones.
authorBenjy Cook <benjycook@hotmail.com>
Mon, 27 Jun 2011 12:46:53 +0000 (12:46 +0000)
committerBenjy Cook <benjycook@hotmail.com>
Mon, 27 Jun 2011 12:46:53 +0000 (12:46 +0000)
release/scripts/startup/ui_mocap.py

index 9c540771b443e1286e2092f116ba37c220976afb..014daa7ef8a3c4d7f1c11a9b55160ca63b7414f0 100644 (file)
@@ -26,8 +26,54 @@ from bpy import *
 from mathutils import Vector
 from math import isfinite
 
+def toggleIKBone(self, context):
+    if self.IKRetarget:
+        if not self.is_in_ik_chain:
+            print(self.name + " IK toggled ON!")
+            ik = self.constraints.new('IK')
+            #ik the whole chain up to the root
+            chainLen = len(self.bone.parent_recursive)
+            ik.chain_count = chainLen
+            for bone in self.parent_recursive:
+                if bone.is_in_ik_chain:
+                    bone.IKRetarget = True
+    else:
+        print(self.name + " IK toggled OFF!")
+        cnstrn_bone = False
+        if hasIKConstraint(self):
+            cnstrn_bone = self
+        elif self.is_in_ik_chain:
+            cnstrn_bone = [child for child in self.children_recursive if hasIKConstraint(child)][0]
+        if cnstrn_bone:
+            # remove constraint, and update IK retarget for all parents of cnstrn_bone up to chain_len
+            ik = [constraint for constraint in cnstrn_bone.constraints if constraint.type == "IK"][0]
+            cnstrn_bone.constraints.remove(ik)
+            cnstrn_bone.IKRetarget = False
+            for bone in cnstrn_bone.parent_recursive:
+                if not bone.is_in_ik_chain:
+                    bone.IKRetarget = False
+            
 
 bpy.types.Bone.map = bpy.props.StringProperty()
+bpy.types.PoseBone.IKRetarget = bpy.props.BoolProperty(name="IK",
+    description = "Toggles IK Retargeting method for given bone",
+    update = toggleIKBone)
+
+def hasIKConstraint(pose_bone):
+    return ("IK" in [constraint.type for constraint in pose_bone.constraints])
+    
+def updateIKRetarget():
+    for obj in bpy.data.objects:
+        if obj.pose:
+            bones = obj.pose.bones
+            for pose_bone in bones:
+                if pose_bone.is_in_ik_chain or hasIKConstraint(pose_bone):
+                    pose_bone.IKRetarget = True
+                else:
+                    pose_bone.IKRetarget = False
+
+updateIKRetarget()
+
 import retarget
 import mocap_tools
 
@@ -66,17 +112,19 @@ class MocapPanel(bpy.types.Panel):
                     enduser_arm = enduser_obj.data
                     perf_pose_bones = enduser_obj.pose.bones
                     for bone in perf.bones:
-                        row = self.layout.row(align=True)
+                        row = self.layout.row()
                         row.label(bone.name)
                         row.prop_search(bone, "map", enduser_arm, "bones")
                         label_mod = "FK"
                         if bone.map:
                             pose_bone = perf_pose_bones[bone.map]
                             if pose_bone.is_in_ik_chain:
-                                label_mod = "IK"
-                            if "IK" in [constraint.type for constraint in pose_bone.constraints]:
-                                label_mod = "IKEnd"
+                                label_mod = "ik chain"
+                            if hasIKConstraint(pose_bone):
+                                label_mod = "ik end"
+                            row.prop(pose_bone, 'IKRetarget')
                         row.label(label_mod)
+                            
                     self.layout.operator("mocap.retarget", text='RETARGET!')
 
 
@@ -132,4 +180,4 @@ def unregister():
 
 
 if __name__ == "__main__":
-    register()
+    register()
\ No newline at end of file