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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
22 from rna_prop_ui import rna_idprop_ui_prop_get
24 from mathutils import Vector
25 from rigify import RigifyError
26 from rigify_utils import copy_bone_simple
28 #METARIG_NAMES = ("cpy",)
32 for action in bpy.data.actions:
35 def get_unmarked_action():
36 for action in bpy.data.actions:
37 if action.tag != True:
41 def add_action(name=None):
44 action = get_unmarked_action()
50 def metarig_template():
51 # generated by rigify.write_meta_rig
52 bpy.ops.object.mode_set(mode='EDIT')
53 obj = bpy.context.active_object
55 bone = arm.edit_bones.new('Bone')
56 bone.head[:] = 0.0000, 0.0000, 0.0000
57 bone.tail[:] = 0.0000, 0.0000, 1.0000
59 bone.connected = False
61 bpy.ops.object.mode_set(mode='OBJECT')
62 pbone = obj.pose.bones['Bone']
63 pbone['type'] = 'copy'
66 def metarig_definition(obj, orig_bone_name):
68 bone = bb[orig_bone_name]
72 chain += [bone.parent.parent.name, bone.parent.name, bone.name]
73 except AttributeError:
74 raise RigifyError("'%s' rig type requires a chain of two parents (bone: %s)" % (RIG_TYPE, orig_bone_name))
76 chain += [child.name for child in bone.children_recursive_basename]
79 raise RigifyError("'%s' rig type requires a chain of 10 bones (bone: %s)" % (RIG_TYPE, orig_bone_name))
84 chain += [bb[chain[9]].children[0].name]
85 chain += [bb[chain[10]].children[0].name]
87 raise RigifyError("'%s' rig type requires a chain of 10 bones (bone: %s)" % (RIG_TYPE, orig_bone_name))
92 def deform(obj, definitions, base_names, options):
93 bpy.ops.object.mode_set(mode='EDIT')
95 eb = obj.data.edit_bones
100 lid1 = make_lid_stretch_bone(obj, "MCH-lid", definitions[2], definitions[3], 1.0)
101 lid2 = make_lid_stretch_bone(obj, "MCH-lid", definitions[3], definitions[4], 1.0)
102 lid22 = make_lid_stretch_bone(obj, "MCH-lid", definitions[4], definitions[5], 1.0)
103 lid33 = make_lid_stretch_bone(obj, "MCH-lid", definitions[4], definitions[3], 1.0)
104 lid3 = make_lid_stretch_bone(obj, "MCH-lid", definitions[5], definitions[4], 1.0)
105 lid4 = make_lid_stretch_bone(obj, "MCH-lid", definitions[6], definitions[5], 1.0)
107 dlid22 = copy_bone_simple(obj.data, lid22, "MCH-lid", parent=True).name
108 dlid33 = copy_bone_simple(obj.data, lid33, "MCH-lid", parent=True).name
109 eb[dlid22].bbone_segments = 8
110 eb[dlid33].bbone_segments = 8
112 eb[lid1].parent = eb[definitions[2]]
113 eb[lid2].parent = eb[definitions[3]]
114 eb[lid22].parent = eb[definitions[4]]
115 eb[lid33].parent = eb[definitions[4]]
116 eb[lid3].parent = eb[definitions[5]]
117 eb[lid4].parent = eb[definitions[6]]
120 lid5 = make_lid_stretch_bone(obj, "MCH-lid", definitions[6], definitions[7], 1.0)
121 lid6 = make_lid_stretch_bone(obj, "MCH-lid", definitions[7], definitions[8], 1.0)
122 lid66 = make_lid_stretch_bone(obj, "MCH-lid", definitions[8], definitions[9], 1.0)
123 lid77 = make_lid_stretch_bone(obj, "MCH-lid", definitions[8], definitions[7], 1.0)
124 lid7 = make_lid_stretch_bone(obj, "MCH-lid", definitions[9], definitions[8], 1.0)
125 lid8 = make_lid_stretch_bone(obj, "MCH-lid", definitions[2], definitions[9], 1.0)
127 dlid66 = copy_bone_simple(obj.data, lid66, "MCH-lid", parent=True).name
128 dlid77 = copy_bone_simple(obj.data, lid77, "MCH-lid", parent=True).name
129 eb[dlid66].bbone_segments = 8
130 eb[dlid77].bbone_segments = 8
132 eb[lid5].parent = eb[definitions[6]]
133 eb[lid6].parent = eb[definitions[7]]
134 eb[lid66].parent = eb[definitions[8]]
135 eb[lid77].parent = eb[definitions[8]]
136 eb[lid7].parent = eb[definitions[9]]
137 eb[lid8].parent = eb[definitions[2]]
140 dlid1 = copy_bone_simple(obj.data, lid1, "DEF-" + base_names[definitions[2]], parent=True).name
141 dlid2 = copy_bone_simple(obj.data, lid2, "DEF-" + base_names[definitions[3]], parent=True).name
142 dlid3 = copy_bone_simple(obj.data, lid3, "DEF-" + base_names[definitions[4]], parent=True).name
143 dlid4 = copy_bone_simple(obj.data, lid4, "DEF-" + base_names[definitions[5]], parent=True).name
145 eb[dlid2].parent = eb[dlid1]
146 eb[dlid22].parent = eb[dlid2]
148 eb[dlid3].parent = eb[dlid4]
149 eb[dlid33].parent = eb[dlid3]
151 eb[dlid2].connected = True
152 eb[dlid22].connected = True
153 eb[dlid3].connected = True
154 eb[dlid33].connected = True
156 eb[dlid1].bbone_segments = 8
157 eb[dlid2].bbone_segments = 8
158 eb[dlid3].bbone_segments = 8
159 eb[dlid4].bbone_segments = 8
162 dlid5 = copy_bone_simple(obj.data, lid5, "DEF-" + base_names[definitions[6]], parent=True).name
163 dlid6 = copy_bone_simple(obj.data, lid6, "DEF-" + base_names[definitions[7]], parent=True).name
164 dlid7 = copy_bone_simple(obj.data, lid7, "DEF-" + base_names[definitions[8]], parent=True).name
165 dlid8 = copy_bone_simple(obj.data, lid8, "DEF-" + base_names[definitions[9]], parent=True).name
167 eb[dlid6].parent = eb[dlid5]
168 eb[dlid66].parent = eb[dlid6]
170 eb[dlid7].parent = eb[dlid8]
171 eb[dlid77].parent = eb[dlid7]
173 eb[dlid6].connected = True
174 eb[dlid66].connected = True
175 eb[dlid7].connected = True
176 eb[dlid77].connected = True
178 eb[dlid5].bbone_segments = 8
179 eb[dlid6].bbone_segments = 8
180 eb[dlid7].bbone_segments = 8
181 eb[dlid8].bbone_segments = 8
184 bpy.ops.object.mode_set(mode='OBJECT')
187 con = pb[dlid1].constraints.new('COPY_TRANSFORMS')
191 con = pb[dlid22].constraints.new('COPY_TRANSFORMS')
193 con.subtarget = lid22
195 con = pb[dlid33].constraints.new('COPY_TRANSFORMS')
197 con.subtarget = lid33
199 con = pb[dlid2].constraints.new('COPY_TRANSFORMS')
203 con = pb[dlid3].constraints.new('COPY_TRANSFORMS')
207 con = pb[dlid4].constraints.new('COPY_TRANSFORMS')
211 con = pb[dlid5].constraints.new('COPY_TRANSFORMS')
215 con = pb[dlid6].constraints.new('COPY_TRANSFORMS')
219 con = pb[dlid66].constraints.new('COPY_TRANSFORMS')
221 con.subtarget = lid66
223 con = pb[dlid77].constraints.new('COPY_TRANSFORMS')
225 con.subtarget = lid77
227 con = pb[dlid7].constraints.new('COPY_TRANSFORMS')
231 con = pb[dlid8].constraints.new('COPY_TRANSFORMS')
240 def control(obj, definitions, base_names, options):
241 bpy.ops.object.mode_set(mode='EDIT')
243 eb = obj.data.edit_bones
247 head_e = eb[definitions[0]]
248 eye_e = eb[definitions[1]]
252 flo1 = copy_bone_simple(obj.data, definitions[1], "MCH-"+base_names[definitions[2]]+".flower", parent=True).name
253 flo2 = copy_bone_simple(obj.data, definitions[1], "MCH-"+base_names[definitions[3]]+".flower", parent=True).name
254 flo3 = copy_bone_simple(obj.data, definitions[1], "MCH-"+base_names[definitions[4]]+".flower", parent=True).name
255 flo4 = copy_bone_simple(obj.data, definitions[1], "MCH-"+base_names[definitions[5]]+".flower", parent=True).name
256 flo5 = copy_bone_simple(obj.data, definitions[1], "MCH-"+base_names[definitions[6]]+".flower", parent=True).name
257 flo6 = copy_bone_simple(obj.data, definitions[1], "MCH-"+base_names[definitions[7]]+".flower", parent=True).name
258 flo7 = copy_bone_simple(obj.data, definitions[1], "MCH-"+base_names[definitions[8]]+".flower", parent=True).name
259 flo8 = copy_bone_simple(obj.data, definitions[1], "MCH-"+base_names[definitions[9]]+".flower", parent=True).name
261 eb[flo1].tail = eb[definitions[2]].head
262 eb[flo2].tail = eb[definitions[3]].head
263 eb[flo3].tail = eb[definitions[4]].head
264 eb[flo4].tail = eb[definitions[5]].head
265 eb[flo5].tail = eb[definitions[6]].head
266 eb[flo6].tail = eb[definitions[7]].head
267 eb[flo7].tail = eb[definitions[8]].head
268 eb[flo8].tail = eb[definitions[9]].head
271 # Make eye lids on tips of flowers
272 flid1 = copy_bone_simple(obj.data, definitions[2], "MCH-"+base_names[definitions[2]]).name
273 flid2 = copy_bone_simple(obj.data, definitions[3], "MCH-"+base_names[definitions[3]]).name
274 flid3 = copy_bone_simple(obj.data, definitions[4], "MCH-"+base_names[definitions[4]]).name
275 flid4 = copy_bone_simple(obj.data, definitions[5], "MCH-"+base_names[definitions[5]]).name
276 flid5 = copy_bone_simple(obj.data, definitions[6], "MCH-"+base_names[definitions[6]]).name
277 flid6 = copy_bone_simple(obj.data, definitions[7], "MCH-"+base_names[definitions[7]]).name
278 flid7 = copy_bone_simple(obj.data, definitions[8], "MCH-"+base_names[definitions[8]]).name
279 flid8 = copy_bone_simple(obj.data, definitions[9], "MCH-"+base_names[definitions[9]]).name
281 eb[flid1].parent = eb[flo1]
282 eb[flid2].parent = eb[flo2]
283 eb[flid3].parent = eb[flo3]
284 eb[flid4].parent = eb[flo4]
285 eb[flid5].parent = eb[flo5]
286 eb[flid6].parent = eb[flo6]
287 eb[flid7].parent = eb[flo7]
288 eb[flid8].parent = eb[flo8]
291 # Make eye lid controls
292 lid1 = copy_bone_simple(obj.data, definitions[2], base_names[definitions[2]]).name
293 lid2 = copy_bone_simple(obj.data, definitions[3], base_names[definitions[3]]).name
294 lid3 = copy_bone_simple(obj.data, definitions[4], base_names[definitions[4]]).name
295 lid4 = copy_bone_simple(obj.data, definitions[5], base_names[definitions[5]]).name
296 lid5 = copy_bone_simple(obj.data, definitions[6], base_names[definitions[6]]).name
297 lid6 = copy_bone_simple(obj.data, definitions[7], base_names[definitions[7]]).name
298 lid7 = copy_bone_simple(obj.data, definitions[8], base_names[definitions[8]]).name
299 lid8 = copy_bone_simple(obj.data, definitions[9], base_names[definitions[9]]).name
301 size = eb[lid1].length
302 eb[lid1].tail = eb[lid1].head + Vector(0,size,0)
303 eb[lid2].tail = eb[lid2].head + Vector(0,size,0)
304 eb[lid3].tail = eb[lid3].head + Vector(0,size,0)
305 eb[lid4].tail = eb[lid4].head + Vector(0,size,0)
306 eb[lid5].tail = eb[lid5].head + Vector(0,size,0)
307 eb[lid6].tail = eb[lid6].head + Vector(0,size,0)
308 eb[lid7].tail = eb[lid7].head + Vector(0,size,0)
309 eb[lid8].tail = eb[lid8].head + Vector(0,size,0)
320 eb[lid1].parent = head_e
321 eb[lid2].parent = head_e
322 eb[lid3].parent = head_e
323 eb[lid4].parent = head_e
324 eb[lid5].parent = head_e
325 eb[lid6].parent = head_e
326 eb[lid7].parent = head_e
327 eb[lid8].parent = head_e
329 lower_lid_ctrl = copy_bone_simple(obj.data, definitions[10], base_names[definitions[10]]).name
330 upper_lid_ctrl = copy_bone_simple(obj.data, definitions[11], base_names[definitions[11]]).name
331 eb[lower_lid_ctrl].parent = head_e
332 eb[upper_lid_ctrl].parent = head_e
333 distance = (eb[lower_lid_ctrl].head - eb[upper_lid_ctrl].head).length
336 bpy.ops.object.mode_set(mode='OBJECT')
339 pb[lower_lid_ctrl].lock_location = True, False, True
340 pb[upper_lid_ctrl].lock_location = True, False, True
342 # Add eye close action if it doesn't already exist
343 action_name = "eye_close"
344 if action_name in bpy.data.actions:
345 close_action = bpy.data.actions[action_name]
347 close_action = add_action(name=action_name)
349 # Add close property (useful when making the animation in the action)
350 prop_name = "close_action"
351 prop = rna_idprop_ui_prop_get(pb[upper_lid_ctrl], prop_name, create=True)
352 pb[upper_lid_ctrl][prop_name] = 1.0
353 prop["soft_min"] = 0.0
354 prop["soft_max"] = 1.0
358 close_driver_path = pb[upper_lid_ctrl].path_from_id() + '["close_action"]'
362 # Flowers track lid controls
363 con = pb[flo1].constraints.new('DAMPED_TRACK')
367 con = pb[flo2].constraints.new('DAMPED_TRACK')
371 con = pb[flo3].constraints.new('DAMPED_TRACK')
375 con = pb[flo4].constraints.new('DAMPED_TRACK')
379 con = pb[flo5].constraints.new('DAMPED_TRACK')
383 con = pb[flo6].constraints.new('DAMPED_TRACK')
387 con = pb[flo7].constraints.new('DAMPED_TRACK')
391 con = pb[flo8].constraints.new('DAMPED_TRACK')
396 # ORG bones to flower lids
397 con = pb[definitions[2]].constraints.new('COPY_TRANSFORMS')
399 con.subtarget = flid1
401 con = pb[definitions[3]].constraints.new('COPY_TRANSFORMS')
403 con.subtarget = flid2
405 con = pb[definitions[4]].constraints.new('COPY_TRANSFORMS')
407 con.subtarget = flid3
409 con = pb[definitions[5]].constraints.new('COPY_TRANSFORMS')
411 con.subtarget = flid4
413 con = pb[definitions[6]].constraints.new('COPY_TRANSFORMS')
415 con.subtarget = flid5
417 con = pb[definitions[7]].constraints.new('COPY_TRANSFORMS')
419 con.subtarget = flid6
421 con = pb[definitions[8]].constraints.new('COPY_TRANSFORMS')
423 con.subtarget = flid7
425 con = pb[definitions[9]].constraints.new('COPY_TRANSFORMS')
427 con.subtarget = flid8
430 # Action constraints, upper lid
431 con = pb[lid1].constraints.new('ACTION')
433 con.subtarget = upper_lid_ctrl
434 con.action = close_action
435 con.transform_channel = 'LOCATION_Y'
436 con.frame_start = -30
438 con.minimum = -distance*2
439 con.maximum = distance
440 con.target_space = 'LOCAL'
441 fcurve = con.driver_add("influence", 0)
442 driver = fcurve.driver
443 driver.type = 'AVERAGE'
444 var = driver.variables.new()
445 var.targets[0].id_type = 'OBJECT'
446 var.targets[0].id = obj
447 var.targets[0].data_path = close_driver_path
450 con = pb[lid2].constraints.new('ACTION')
452 con.subtarget = upper_lid_ctrl
453 con.action = close_action
454 con.transform_channel = 'LOCATION_Y'
455 con.frame_start = -30
457 con.minimum = -distance*2
458 con.maximum = distance
459 con.target_space = 'LOCAL'
460 fcurve = con.driver_add("influence", 0)
461 driver = fcurve.driver
462 driver.type = 'AVERAGE'
463 var = driver.variables.new()
464 var.targets[0].id_type = 'OBJECT'
465 var.targets[0].id = obj
466 var.targets[0].data_path = close_driver_path
468 con = pb[lid3].constraints.new('ACTION')
470 con.subtarget = upper_lid_ctrl
471 con.action = close_action
472 con.transform_channel = 'LOCATION_Y'
473 con.frame_start = -30
475 con.minimum = -distance*2
476 con.maximum = distance
477 con.target_space = 'LOCAL'
478 fcurve = con.driver_add("influence", 0)
479 driver = fcurve.driver
480 driver.type = 'AVERAGE'
481 var = driver.variables.new()
482 var.targets[0].id_type = 'OBJECT'
483 var.targets[0].id = obj
484 var.targets[0].data_path = close_driver_path
486 con = pb[lid4].constraints.new('ACTION')
488 con.subtarget = upper_lid_ctrl
489 con.action = close_action
490 con.transform_channel = 'LOCATION_Y'
491 con.frame_start = -30
493 con.minimum = -distance*2
494 con.maximum = distance
495 con.target_space = 'LOCAL'
496 fcurve = con.driver_add("influence", 0)
497 driver = fcurve.driver
498 driver.type = 'AVERAGE'
499 var = driver.variables.new()
500 var.targets[0].id_type = 'OBJECT'
501 var.targets[0].id = obj
502 var.targets[0].data_path = close_driver_path
504 con = pb[lid5].constraints.new('ACTION')
506 con.subtarget = upper_lid_ctrl
507 con.action = close_action
508 con.transform_channel = 'LOCATION_Y'
509 con.frame_start = -30
511 con.minimum = -distance*2
512 con.maximum = distance
513 con.target_space = 'LOCAL'
514 fcurve = con.driver_add("influence", 0)
515 driver = fcurve.driver
516 driver.type = 'AVERAGE'
517 var = driver.variables.new()
518 var.targets[0].id_type = 'OBJECT'
519 var.targets[0].id = obj
520 var.targets[0].data_path = close_driver_path
522 # Action constraints, lower lid
523 con = pb[lid5].constraints.new('ACTION')
525 con.subtarget = lower_lid_ctrl
526 con.action = close_action
527 con.transform_channel = 'LOCATION_Y'
528 con.frame_start = -30
530 con.minimum = -distance
531 con.maximum = distance*2
532 con.target_space = 'LOCAL'
533 fcurve = con.driver_add("influence", 0)
534 driver = fcurve.driver
535 driver.type = 'AVERAGE'
536 var = driver.variables.new()
537 var.targets[0].id_type = 'OBJECT'
538 var.targets[0].id = obj
539 var.targets[0].data_path = close_driver_path
541 con = pb[lid6].constraints.new('ACTION')
543 con.subtarget = lower_lid_ctrl
544 con.action = close_action
545 con.transform_channel = 'LOCATION_Y'
546 con.frame_start = -30
548 con.minimum = -distance
549 con.maximum = distance*2
550 con.target_space = 'LOCAL'
551 fcurve = con.driver_add("influence", 0)
552 driver = fcurve.driver
553 driver.type = 'AVERAGE'
554 var = driver.variables.new()
555 var.targets[0].id_type = 'OBJECT'
556 var.targets[0].id = obj
557 var.targets[0].data_path = close_driver_path
559 con = pb[lid7].constraints.new('ACTION')
561 con.subtarget = lower_lid_ctrl
562 con.action = close_action
563 con.transform_channel = 'LOCATION_Y'
564 con.frame_start = -30
566 con.minimum = -distance
567 con.maximum = distance*2
568 con.target_space = 'LOCAL'
569 fcurve = con.driver_add("influence", 0)
570 driver = fcurve.driver
571 driver.type = 'AVERAGE'
572 var = driver.variables.new()
573 var.targets[0].id_type = 'OBJECT'
574 var.targets[0].id = obj
575 var.targets[0].data_path = close_driver_path
577 con = pb[lid8].constraints.new('ACTION')
579 con.subtarget = lower_lid_ctrl
580 con.action = close_action
581 con.transform_channel = 'LOCATION_Y'
582 con.frame_start = -30
584 con.minimum = -distance
585 con.maximum = distance*2
586 con.target_space = 'LOCAL'
587 fcurve = con.driver_add("influence", 0)
588 driver = fcurve.driver
589 driver.type = 'AVERAGE'
590 var = driver.variables.new()
591 var.targets[0].id_type = 'OBJECT'
592 var.targets[0].id = obj
593 var.targets[0].data_path = close_driver_path
595 con = pb[lid1].constraints.new('ACTION')
597 con.subtarget = lower_lid_ctrl
598 con.action = close_action
599 con.transform_channel = 'LOCATION_Y'
600 con.frame_start = -30
602 con.minimum = -distance
603 con.maximum = distance*2
604 con.target_space = 'LOCAL'
605 fcurve = con.driver_add("influence", 0)
606 driver = fcurve.driver
607 driver.type = 'AVERAGE'
608 var = driver.variables.new()
609 var.targets[0].id_type = 'OBJECT'
610 var.targets[0].id = obj
611 var.targets[0].data_path = close_driver_path
617 layer = list(bb[definitions[2]].layer)
618 bb[lid1].layer = layer
619 bb[lid2].layer = layer
620 bb[lid3].layer = layer
621 bb[lid4].layer = layer
622 bb[lid5].layer = layer
623 bb[lid6].layer = layer
624 bb[lid7].layer = layer
625 bb[lid8].layer = layer
633 def main(obj, bone_definition, base_names, options):
635 control(obj, bone_definition, base_names, options)
637 deform(obj, bone_definition, base_names, options)
644 def make_lid_stretch_bone(obj, name, bone1, bone2, roll_alpha):
645 eb = obj.data.edit_bones
648 # Create the bone, pointing from bone1 to bone2
649 bone_e = copy_bone_simple(obj.data, bone1, name, parent=True)
650 bone_e.connected = False
651 bone_e.tail = eb[bone2].head
654 # Align the bone roll with the average direction of bone1 and bone2
655 vec = bone_e.y_axis.cross(((1.0-roll_alpha)*eb[bone1].y_axis) + (roll_alpha*eb[bone2].y_axis)).normalize()
657 ang = acos(vec * bone_e.x_axis)
660 c1 = vec * bone_e.x_axis
661 bone_e.roll -= (ang*2)
662 c2 = vec * bone_e.x_axis
665 bone_e.roll += (ang*2)
667 bpy.ops.object.mode_set(mode='OBJECT')
671 con = bone_p.constraints.new('COPY_LOCATION')
673 con.subtarget = bone1
675 con = bone_p.constraints.new('DAMPED_TRACK')
677 con.subtarget = bone2
679 con = bone_p.constraints.new('STRETCH_TO')
681 con.subtarget = bone2
682 con.volume = 'NO_VOLUME'
684 bpy.ops.object.mode_set(mode='EDIT')