1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 # ##### END GPL LICENSE BLOCK #####
22 from math import radians
23 from rigify import RigifyError, get_layer_dict
24 from rigify_utils import bone_class_instance, copy_bone_simple, add_pole_target_bone, add_stretch_to, blend_bone_list, get_side_name, get_base_name
25 from rna_prop_ui import rna_idprop_ui_prop_get
26 from Mathutils import Vector
28 METARIG_NAMES = "shoulder", "arm", "forearm", "hand"
31 def metarig_template():
32 # generated by rigify.write_meta_rig
33 bpy.ops.object.mode_set(mode='EDIT')
34 obj = bpy.context.active_object
36 bone = arm.edit_bones.new('shoulder')
37 bone.head[:] = 0.0000, -0.0425, 0.0000
38 bone.tail[:] = 0.0942, -0.0075, 0.0333
40 bone.connected = False
41 bone = arm.edit_bones.new('upper_arm')
42 bone.head[:] = 0.1066, -0.0076, -0.0010
43 bone.tail[:] = 0.2855, 0.0206, -0.0104
45 bone.connected = False
46 bone.parent = arm.edit_bones['shoulder']
47 bone = arm.edit_bones.new('forearm')
48 bone.head[:] = 0.2855, 0.0206, -0.0104
49 bone.tail[:] = 0.4550, -0.0076, -0.0023
52 bone.parent = arm.edit_bones['upper_arm']
53 bone = arm.edit_bones.new('hand')
54 bone.head[:] = 0.4550, -0.0076, -0.0023
55 bone.tail[:] = 0.5423, -0.0146, -0.0131
58 bone.parent = arm.edit_bones['forearm']
60 bpy.ops.object.mode_set(mode='OBJECT')
61 pbone = obj.pose.bones['upper_arm']
62 pbone['type'] = 'arm_biped_generic'
65 def metarig_definition(obj, orig_bone_name):
66 mt = bone_class_instance(obj, METARIG_NAMES) # meta
67 mt.arm = orig_bone_name
70 mt.shoulder_p = mt.arm_p.parent
73 raise RigifyError("could not find '%s' parent, skipping:" % orig_bone_name)
75 mt.shoulder = mt.shoulder_p.name
77 # We could have some bones attached, find the bone that has this as its 2nd parent
79 for pbone in obj.pose.bones:
80 index = pbone.parent_index(mt.arm_p)
81 if index == 2 and pbone.bone.connected and pbone.bone.parent.connected:
85 raise RigifyError("Found %s possible hands attached to this arm, expected 1 from bone: %s" % ([pbone.name for pbone in hands], orig_bone_name))
87 # first add the 2 new bones
89 mt.hand = mt.hand_p.name
91 mt.forearm_p = mt.hand_p.parent
92 mt.forearm = mt.forearm_p.name
97 def ik(obj, definitions, base_names, options):
101 mt = bone_class_instance(obj, METARIG_NAMES)
102 mt.shoulder, mt.arm, mt.forearm, mt.hand = definitions
105 ik = bone_class_instance(obj, ["pole", "pole_vis", "hand_vis"])
106 ik_chain = mt.copy(to_fmt="MCH-%s_ik", base_names=base_names, exclude_attrs=["shoulder"])
108 # IK needs no parent_index
109 ik_chain.hand_e.connected = False
110 ik_chain.hand_e.parent = None
111 ik_chain.hand_e.local_location = False
112 ik_chain.rename("hand", get_base_name(base_names[mt.hand]) + "_ik" + get_side_name(mt.hand))
114 ik_chain.arm_e.connected = False
115 ik_chain.arm_e.parent = mt.shoulder_e
117 # Add the bone used for the arms poll target
118 #ik.pole = add_pole_target_bone(obj, mt.forearm, get_base_name(base_names[mt.forearm]) + "_target" + get_side_name(mt.forearm), mode='ZAVERAGE')
119 ik.pole = add_pole_target_bone(obj, mt.forearm, "elbow_target" + get_side_name(mt.forearm), mode='ZAVERAGE')
122 ik.pole_e.local_location = False
124 # option: elbow_parent
125 elbow_parent_name = options.get("elbow_parent", "")
127 if elbow_parent_name:
129 elbow_parent_e = arm.edit_bones[elbow_parent_name]
131 # TODO, old/new parent mapping
132 raise RigifyError("parent bone from property 'arm_biped_generic.elbow_parent' not found '%s'" % elbow_parent_name)
133 ik.pole_e.parent = elbow_parent_e
135 # update bones after this!
136 ik.hand_vis = add_stretch_to(obj, mt.hand, ik_chain.hand, "VIS-%s_ik" % base_names[mt.hand])
137 ik.pole_vis = add_stretch_to(obj, mt.forearm, ik.pole, "VIS-%s_ik" % base_names[mt.forearm])
140 ik.hand_vis_e.restrict_select = True
141 ik.pole_vis_e.restrict_select = True
143 bpy.ops.object.mode_set(mode='OBJECT')
150 ik_chain.forearm_p.ik_dof_x = True
151 ik_chain.forearm_p.ik_dof_y = False
152 ik_chain.forearm_p.ik_dof_z = False
154 con = ik_chain.forearm_p.constraints.new('IK')
156 con.subtarget = ik_chain.hand
157 con.pole_target = obj
158 con.pole_subtarget = ik.pole
161 con.use_stretch = True
162 con.use_target = True
163 con.use_rotation = False
165 con.pole_angle = -90.0 # XXX, RAD2DEG
167 # last step setup layers
168 layers = get_layer_dict(options)
170 for attr in ik_chain.attr_names:
171 getattr(ik_chain, attr + "_b").layer = lay
172 for attr in ik.attr_names:
173 getattr(ik, attr + "_b").layer = lay
176 bpy.ops.object.mode_set(mode='EDIT')
177 # don't blend the shoulder
178 return [None] + ik_chain.names()
181 def fk(obj, definitions, base_names, options):
185 mt = bone_class_instance(obj, METARIG_NAMES)
186 mt.shoulder, mt.arm, mt.forearm, mt.hand = definitions
189 ex = bone_class_instance(obj, ["socket", "hand_delta"])
190 fk_chain = mt.copy(base_names=base_names)
192 # shoulder is used as a hinge
193 fk_chain.rename("shoulder", "MCH-%s_hinge" % base_names[mt.arm])
194 fk_chain.shoulder_e.translate(Vector(0.0, fk_chain.shoulder_e.length / 2, 0.0))
196 # upper arm constrains to this.
197 ex.socket_e = copy_bone_simple(arm, mt.arm, "MCH-%s_socket" % base_names[mt.arm])
198 ex.socket = ex.socket_e.name
199 ex.socket_e.connected = False
200 ex.socket_e.parent = mt.shoulder_e
201 ex.socket_e.length *= 0.5
203 # insert the 'DLT-hand', between the forearm and the hand
204 # copies forarm rotation
205 ex.hand_delta_e = copy_bone_simple(arm, fk_chain.hand, "DLT-%s" % base_names[mt.hand], parent=True)
206 ex.hand_delta = ex.hand_delta_e.name
207 ex.hand_delta_e.length *= 0.5
208 ex.hand_delta_e.connected = False
209 if "hand_roll" in options:
210 ex.hand_delta_e.roll += radians(options["hand_roll"])
212 fk_chain.hand_e.connected = False
213 fk_chain.hand_e.parent = ex.hand_delta_e
215 bpy.ops.object.mode_set(mode='OBJECT')
221 # Set rotation modes and axis locks
222 fk_chain.forearm_p.rotation_mode = 'XYZ'
223 fk_chain.forearm_p.lock_rotation = (False, True, True)
224 fk_chain.hand_p.rotation_mode = 'ZXY'
226 con = fk_chain.arm_p.constraints.new('COPY_LOCATION')
228 con.subtarget = ex.socket
230 fk_chain.hand_p.lock_location = True, True, True
231 con = ex.hand_delta_p.constraints.new('COPY_ROTATION')
233 con.subtarget = fk_chain.forearm
236 # Hinge constraint & driver
237 con = fk_chain.shoulder_p.constraints.new('COPY_ROTATION')
240 con.subtarget = mt.shoulder
241 driver_fcurve = con.driver_add("influence", 0)
242 driver = driver_fcurve.driver
245 controller_path = fk_chain.arm_p.path_to_id()
247 fk_chain.arm_p["hinge"] = 0.0
248 prop = rna_idprop_ui_prop_get(fk_chain.arm_p, "hinge", create=True)
249 prop["soft_min"] = 0.0
250 prop["soft_max"] = 1.0
254 driver = driver_fcurve.driver
255 driver.type = 'AVERAGE'
257 tar = driver.targets.new()
259 tar.id_type = 'OBJECT'
261 tar.data_path = controller_path + '["hinge"]'
263 mod = driver_fcurve.modifiers[0]
265 mod.coefficients[0] = 1.0
266 mod.coefficients[1] = -1.0
271 # last step setup layers
272 layers = get_layer_dict(options)
274 for attr in fk_chain.attr_names:
275 getattr(fk_chain, attr + "_b").layer = lay
277 lay = layers["extra"]
278 for attr in ex.attr_names:
279 getattr(ex, attr + "_b").layer = lay
282 bpy.ops.object.mode_set(mode='EDIT')
283 return None, fk_chain.arm, fk_chain.forearm, fk_chain.hand
286 def main(obj, bone_definition, base_names, options):
287 bones_fk = fk(obj, bone_definition, base_names, options)
288 bones_ik = ik(obj, bone_definition, base_names, options)
290 bpy.ops.object.mode_set(mode='OBJECT')
291 blend_bone_list(obj, bone_definition, bones_fk, bones_ik, target_bone=bones_fk[1], blend_default=1.0)