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 #####
20 from rigify import get_bone_data, copy_bone_simple
21 from rna_prop_ui import rna_idprop_ui_get, rna_idprop_ui_prop_get
23 # not used, defined for completeness
24 METARIG_NAMES = tuple()
26 def metarig_template():
27 bpy.ops.object.mode_set(mode='EDIT')
28 obj = bpy.context.object
30 bone = arm.edit_bones.new('hand')
31 bone.head[:] = 0.0082, -1.2492, 0.0000
32 bone.tail[:] = 0.0423, -0.4150, 0.0000
34 bone.connected = False
35 bone = arm.edit_bones.new('palm.03')
36 bone.head[:] = 0.0000, 0.0000, -0.0000
37 bone.tail[:] = 0.0506, 1.2781, -0.1299
39 bone.connected = False
40 bone.parent = arm.edit_bones['hand']
41 bone = arm.edit_bones.new('palm.04')
42 bone.head[:] = 0.5000, -0.0000, 0.0000
43 bone.tail[:] = 0.6433, 1.2444, -0.1299
45 bone.connected = False
46 bone.parent = arm.edit_bones['hand']
47 bone = arm.edit_bones.new('palm.05')
48 bone.head[:] = 1.0000, 0.0000, 0.0000
49 bone.tail[:] = 1.3961, 1.0084, -0.1299
51 bone.connected = False
52 bone.parent = arm.edit_bones['hand']
53 bone = arm.edit_bones.new('palm.02')
54 bone.head[:] = -0.5000, 0.0000, -0.0000
55 bone.tail[:] = -0.5674, 1.2022, -0.1299
57 bone.connected = False
58 bone.parent = arm.edit_bones['hand']
59 bone = arm.edit_bones.new('palm.01')
60 bone.head[:] = -1.0000, 0.0000, -0.0000
61 bone.tail[:] = -1.3286, 1.0590, -0.1299
63 bone.connected = False
64 bone.parent = arm.edit_bones['hand']
65 bone = arm.edit_bones.new('palm.06')
66 bone.head[:] = 1.3536, -0.2941, 0.0000
67 bone.tail[:] = 2.1109, 0.4807, -0.1299
69 bone.connected = False
70 bone.parent = arm.edit_bones['hand']
72 bpy.ops.object.mode_set(mode='OBJECT')
73 pbone = obj.pose.bones['hand']
74 pbone['type'] = 'palm'
77 def metarig_definition(obj, orig_bone_name):
79 The bone given is the first in a chain
80 Expects an array of children sorted with the little finger lowest.
82 parent -> [pinky, ring... etc]
85 bone_definition = [orig_bone_name]
86 palm_ebone = arm.bones[orig_bone_name]
88 children = [ebone.name for ebone in palm_ebone.children]
89 children.sort() # simply assume the pinky has the lowest name
90 bone_definition.extend(children)
92 return bone_definition
95 def main(obj, bone_definition, base_names):
96 arm, palm_pbone, palm_ebone = get_bone_data(obj, bone_definition[0])
97 children = bone_definition[1:]
99 # Make a copy of the pinky
100 # simply assume the pinky has the lowest name
101 pinky_ebone = arm.edit_bones[children[0]]
102 ring_ebone = arm.edit_bones[children[1]]
104 control_ebone = copy_bone_simple(arm, pinky_ebone.name, "palm_control", parent=True)
105 control_name = control_ebone.name
107 offset = (pinky_ebone.head - ring_ebone.head)
109 control_ebone.translate(offset)
111 bpy.ops.object.mode_set(mode='OBJECT')
114 arm, control_pbone, control_ebone = get_bone_data(obj, control_name)
115 arm, pinky_pbone, pinky_ebone = get_bone_data(obj, children[0])
117 control_pbone.rotation_mode = 'YZX'
118 control_pbone.lock_rotation = False, True, True
120 driver_fcurves = pinky_pbone.driver_add("rotation_euler")
123 controller_path = control_pbone.path_to_id()
126 control_pbone["spread"] = 0.0
127 prop = rna_idprop_ui_prop_get(control_pbone, "spread", create=True)
128 prop["soft_min"] = -1.0
129 prop["soft_max"] = 1.0
133 driver = driver_fcurves[0].driver
134 driver.type = 'AVERAGE'
136 tar = driver.targets.new()
138 tar.id_type = 'OBJECT'
140 tar.rna_path = controller_path + ".rotation_euler[0]"
144 driver = driver_fcurves[1].driver
145 driver.expression = "-x/4.0"
147 tar = driver.targets.new()
149 tar.id_type = 'OBJECT'
151 tar.rna_path = controller_path + ".rotation_euler[0]"
155 driver = driver_fcurves[2].driver
156 driver.expression = "(1.0-cos(x))-s"
157 tar = driver.targets.new()
159 tar.id_type = 'OBJECT'
161 tar.rna_path = controller_path + ".rotation_euler[0]"
163 tar = driver.targets.new()
165 tar.id_type = 'OBJECT'
167 tar.rna_path = controller_path + '["spread"]'
170 for i, child_name in enumerate(children):
171 child_pbone = obj.pose.bones[child_name]
172 child_pbone.rotation_mode = 'YZX'
174 if child_name != children[-1] and child_name != children[0]:
176 # this is somewhat arbitrary but seems to look good
177 inf = i / (len(children)+1)
179 inf = ((inf * inf) + inf) / 2.0
181 # used for X/Y constraint
182 inf_minor = inf * inf
184 con = child_pbone.constraints.new('COPY_ROTATION')
185 con.name = "Copy Z Rot"
187 con.subtarget = children[0] # also pinky_pbone
188 con.owner_space = con.target_space = 'LOCAL'
189 con.use_x, con.use_y, con.use_z = False, False, True
192 con = child_pbone.constraints.new('COPY_ROTATION')
193 con.name = "Copy XY Rot"
195 con.subtarget = children[0] # also pinky_pbone
196 con.owner_space = con.target_space = 'LOCAL'
197 con.use_x, con.use_y, con.use_z = True, True, False
198 con.influence = inf_minor
201 child_pbone = obj.pose.bones[children[-1]]
202 child_pbone.rotation_mode = 'QUATERNION'
204 # no blending the result of this