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 bone_class_instance, copy_bone_simple
21 from rna_prop_ui import rna_idprop_ui_prop_get
23 # not used, defined for completeness
24 METARIG_NAMES = ("body", "head")
26 def metarig_template():
27 bpy.ops.object.mode_set(mode='EDIT')
28 obj = bpy.context.object
30 bone = arm.edit_bones.new('body')
31 bone.head[:] = -0.0000, -0.2771, -1.3345
32 bone.tail[:] = -0.0000, -0.1708, -0.3984
34 bone.connected = False
35 bone = arm.edit_bones.new('head')
36 bone.head[:] = -0.0000, -0.1708, -0.1984
37 bone.tail[:] = 0.0000, -0.1708, 1.6016
39 bone.connected = False
40 bone.parent = arm.edit_bones['body']
41 bone = arm.edit_bones.new('neck.01')
42 bone.head[:] = 0.0000, -0.1708, -0.1984
43 bone.tail[:] = -0.0000, -0.0994, 0.1470
45 bone.connected = False
46 bone.parent = arm.edit_bones['head']
47 bone = arm.edit_bones.new('neck.02')
48 bone.head[:] = -0.0000, -0.0994, 0.1470
49 bone.tail[:] = 0.0000, -0.2428, 0.5162
52 bone.parent = arm.edit_bones['neck.01']
53 bone = arm.edit_bones.new('neck.03')
54 bone.head[:] = 0.0000, -0.2428, 0.5162
55 bone.tail[:] = 0.0000, -0.4190, 0.8722
58 bone.parent = arm.edit_bones['neck.02']
59 bone = arm.edit_bones.new('neck.04')
60 bone.head[:] = 0.0000, -0.4190, 0.8722
61 bone.tail[:] = 0.0000, -0.5111, 1.1956
64 bone.parent = arm.edit_bones['neck.03']
65 bone = arm.edit_bones.new('neck.05')
66 bone.head[:] = 0.0000, -0.5111, 1.1956
67 bone.tail[:] = 0.0000, -0.5391, 1.6081
70 bone.parent = arm.edit_bones['neck.04']
72 bpy.ops.object.mode_set(mode='OBJECT')
73 pbone = obj.pose.bones['head']
74 pbone['type'] = 'neck'
77 def metarig_definition(obj, orig_bone_name):
79 The bone given is the head, its parent is the body,
80 # its only child the first of a chain with matching basenames.
82 body -> head -> neck_01 -> neck_02 -> neck_03.... etc
85 head = arm.bones[orig_bone_name]
88 children = head.children
89 if len(children) != 1:
90 print("expected the head to have only 1 child.")
93 bone_definition = [body.name, head.name, child.name]
94 bone_definition.extend([child.name for child in child.children_recursive_basename])
95 return bone_definition
98 def main(obj, bone_definition, base_names):
99 from Mathutils import Vector
103 # Initialize container classes for convenience
104 mt = bone_class_instance(obj, ["body", "head"]) # meta
105 mt.body = bone_definition[0]
106 mt.head = bone_definition[1]
109 neck_chain = bone_definition[2:]
111 mt_chain = bone_class_instance(obj, [("neck_%.2d" % (i + 1)) for i in range(len(neck_chain))]) # 99 bones enough eh?
112 for i, attr in enumerate(mt_chain.attr_names):
113 setattr(mt_chain, attr, neck_chain[i])
116 neck_chain_basename = mt_chain.neck_01_e.basename
117 neck_chain_segment_length = mt_chain.neck_01_e.length
119 ex = bone_class_instance(obj, ["body", "head", "head_hinge","neck_socket"]) # hinge & extras
121 # Add the head hinge at the bodys location, becomes the parent of the original head
124 # Copy the head bone and offset
125 ex.head_e = copy_bone_simple(arm, mt.head, "MCH_%s" % mt.head, parent=True)
126 ex.head = ex.head_e.name
128 head_length = ex.head_e.length
129 ex.head_e.head.y += head_length / 2.0
130 ex.head_e.tail.y += head_length / 2.0
132 # Yes, use the body bone but call it a head hinge
133 ex.head_hinge_e = copy_bone_simple(arm, mt.body, "MCH_%s_hinge" % mt.head, parent=True)
134 ex.head_hinge = ex.head_hinge_e.name
135 ex.head_hinge_e.head.y += head_length / 4.0
136 ex.head_hinge_e.tail.y += head_length / 4.0
138 # reparent the head, assume its not connected
139 mt.head_e.parent = ex.head_hinge_e
141 # Insert the neck socket, the head copys this loation
142 ex.neck_socket_e = arm.edit_bones.new("MCH-%s_socked" % neck_chain_basename)
143 ex.neck_socket = ex.neck_socket_e.name
144 ex.neck_socket_e.parent = mt.body_e
145 ex.neck_socket_e.head = mt.head_e.head
146 ex.neck_socket_e.tail = mt.head_e.head - Vector(0.0, neck_chain_segment_length / 2.0, 0.0)
147 ex.neck_socket_e.roll = 0.0
149 # offset the head, not really needed since it has a copyloc constraint
150 mt.head_e.head.y += head_length / 4.0
151 mt.head_e.tail.y += head_length / 4.0
153 for i, attr in enumerate(mt_chain.attr_names):
154 neck_e = getattr(mt_chain, attr + "_e")
156 # dont store parent names, re-reference as each chain bones parent.
157 neck_e_parent = arm.edit_bones.new("MCH-rot_%s" % neck_e.name)
158 neck_e_parent.head = neck_e.head
159 neck_e_parent.tail = neck_e.head + Vector(0.0, 0.0, neck_chain_segment_length / 2.0)
160 neck_e_parent.roll = 0.0
162 orig_parent = neck_e.parent
163 neck_e.connected = False
164 neck_e.parent = neck_e_parent
165 neck_e_parent.connected = False
168 neck_e_parent.parent = mt.body_e
170 neck_e_parent.parent = orig_parent
173 bpy.ops.object.mode_set(mode='OBJECT')
179 # Simple one off constraints, no drivers
180 con = mt.head_p.constraints.new('COPY_LOCATION')
182 con.subtarget = ex.neck_socket
184 con = ex.head_p.constraints.new('COPY_ROTATION')
186 con.subtarget = mt.head
189 prop = rna_idprop_ui_prop_get(mt.head_p, "hinge", create=True)
190 mt.head_p["hinge"] = 0.0
191 prop["soft_min"] = 0.0
192 prop["soft_max"] = 1.0
194 con = ex.head_hinge_p.constraints.new('COPY_ROTATION')
197 con.subtarget = mt.body
200 hinge_driver_path = mt.head_p.path_to_id() + '["hinge"]'
202 fcurve = con.driver_add("influence", 0)
203 driver = fcurve.driver
204 tar = driver.targets.new()
205 driver.type = 'AVERAGE'
207 tar.id_type = 'OBJECT'
209 tar.rna_path = hinge_driver_path
211 #mod = fcurve_driver.modifiers.new('GENERATOR')
212 mod = fcurve.modifiers[0]
214 mod.coefficients[0] = 1.0
215 mod.coefficients[1] = -1.0
217 head_driver_path = mt.head_p.path_to_id()
219 # b01/max(0.001,b01+b02+b03+b04+b05)
220 target_names = [("b%.2d" % (i + 1)) for i in range(len(neck_chain))]
221 expression_suffix = "/max(0.001,%s)" % "+".join(target_names)
224 for i, attr in enumerate(mt_chain.attr_names):
225 neck_p = getattr(mt_chain, attr + "_p")
226 neck_p.lock_location = True, True, True
227 neck_p.lock_location = True, True, True
228 neck_p.lock_rotations_4d = True
231 prop_name = "bend_%.2d" % (i + 1)
232 prop = rna_idprop_ui_prop_get(mt.head_p, prop_name, create=True)
233 mt.head_p[prop_name] = 1.0
234 prop["soft_min"] = 0.0
235 prop["soft_max"] = 1.0
237 # add parent constraint
238 neck_p_parent = neck_p.parent
241 con = neck_p_parent.constraints.new('COPY_ROTATION')
242 con.name = "Copy Rotation"
244 con.subtarget = ex.head
245 con.owner_space = 'LOCAL'
246 con.target_space = 'LOCAL'
248 fcurve = con.driver_add("influence", 0)
249 driver = fcurve.driver
250 driver.type = 'SCRIPTED'
251 # b01/max(0.001,b01+b02+b03+b04+b05)
252 driver.expression = target_names[i] + expression_suffix
253 fcurve.modifiers.remove(0) # grr dont need a modifier
255 for j in range(len(neck_chain)):
256 tar = driver.targets.new()
257 tar.name = target_names[j]
258 tar.id_type = 'OBJECT'
260 tar.rna_path = head_driver_path + ('["bend_%.2d"]' % (j + 1))
262 # no blending the result of this