1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
import bpy
from mathutils import Vector
# TODO, have these in a more general module
from rna_prop_ui import rna_idprop_ui_prop_get
SPECIAL_TYPES = "root",
LAYER_TYPES = "main", "extra", "ik", "fk"
ORG_LAYERS = [n == 31 for n in range(0, 32)]
MCH_LAYERS = [n == 30 for n in range(0, 32)]
DEF_LAYERS = [n == 29 for n in range(0, 32)]
ROOT_LAYERS = [n == 28 for n in range(0, 32)]
ORG_PREFIX = "ORG-"
MCH_PREFIX = "MCH-"
DEF_PREFIX = "DEF-"
WGT_PREFIX = "WGT-"
class RigifyError(Exception):
"""Exception raised for errors in the metarig.
"""
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)
def submodule_func_from_type(bone_type):
type_pair = bone_type.split(".")
# 'leg.ik' will look for an ik function in the leg module
# 'leg' will look up leg.main
if len(type_pair) == 1:
type_pair = type_pair[0], "main"
type_name, func_name = type_pair
# from rigify import leg
try:
submod = __import__(name="%s.%s" % (__package__, type_name), fromlist=[type_name])
except ImportError:
raise RigifyError("python module for type '%s' not found" % type_name)
reload(submod)
return type_name, submod, getattr(submod, func_name)
def get_submodule_types():
import os
submodules = []
files = os.listdir(os.path.dirname(__file__))
for f in files:
if not f.startswith("_") and f.endswith(".py"):
submodules.append(f[:-3])
return sorted(submodules)
def get_bone_type_options(pbone, type_name):
options = {}
bone_name = pbone.name
for key, value in pbone.items():
key_pair = key.rsplit(".")
# get all bone properties
""""
if key_pair[0] == type_name:
if len(key_pair) != 2:
raise RigifyError("option error for bone '%s', property name was not a pair '%s'" % (bone_name, key_pair))
options[key_pair[1]] = value
"""
options[key] = value
return options
def get_layer_dict(options):
'''
Extracts layer info from a bone options dict
defaulting to the layer index if not set.
'''
layer_default = [False] * 32
result = {}
for i, layer_type in enumerate(LAYER_TYPES):
# no matter if its not defined
layer_index = options.get("layer_" + layer_type, i + 2)
layer = layer_default[:]
layer[layer_index-1] = True
result[layer_type] = layer
return result
def validate_rig(context, obj):
'''
Makes no changes
only runs the metarig definitions and reports errors
'''
type_found = False
for pbone in obj.pose.bones:
bone_name = pbone.name
bone_type = pbone.get("type", "")
if bone_type:
bone_type_list = [bt for bt in bone_type.replace(",", " ").split()]
else:
bone_type_list = []
for bone_type in bone_type_list:
if bone_type.split(".")[0] in SPECIAL_TYPES:
continue
type_name, submod, type_func = submodule_func_from_type(bone_type)
reload(submod)
submod.metarig_definition(obj, bone_name)
type_found = True
get_bone_type_options(pbone, bone_type)
# missing, - check for duplicate root bone.
if not type_found:
raise RigifyError("This rig has no 'type' properties defined on any pose bones, nothing to do")
def generate_rig(context, obj_orig, prefix="ORG-", META_DEF=True):
'''
Main function for generating
'''
from collections import OrderedDict
import rigify_utils
reload(rigify_utils)
print("Begin...")
# Not needed but catches any errors before duplicating
validate_rig(context, obj_orig)
use_global_undo = context.user_preferences.edit.use_global_undo
context.user_preferences.edit.use_global_undo = False
mode_orig = context.mode
rest_backup = obj_orig.data.pose_position
obj_orig.data.pose_position = 'REST'
bpy.ops.object.mode_set(mode='OBJECT')
scene = context.scene
# Check if the generated rig already exists, so we can
# regenerate in the same object. If not, create a new
# object to generate the rig in.
print("Fetch rig.")
try:
name = obj_orig["rig_object_name"]
except KeyError:
name = "rig"
try:
obj = scene.objects[name]
except KeyError:
obj = bpy.data.objects.new(name, bpy.data.armatures.new(name))
scene.objects.link(obj)
obj.data.pose_position = 'POSE'
# Get rid of anim data in case the rig already existed
print("Clear rig animation data.")
obj.animation_data_clear()
# Select generated rig object
obj_orig.select = False
obj.select = True
scene.objects.active = obj
# Remove all bones from the generated rig armature.
bpy.ops.object.mode_set(mode='EDIT')
for bone in obj.data.edit_bones:
obj.data.edit_bones.remove(bone)
bpy.ops.object.mode_set(mode='OBJECT')
# Create temporary duplicates for merging
temp_rig_1 = obj_orig.copy()
temp_rig_1.data = obj_orig.data.copy()
scene.objects.link(temp_rig_1)
temp_rig_2 = obj_orig.copy()
temp_rig_2.data = obj.data
scene.objects.link(temp_rig_2)
# Select the temp rigs for merging
for objt in scene.objects:
objt.select = False # deselect all objects
temp_rig_1.select = True
temp_rig_2.select = True
scene.objects.active = temp_rig_2
# Merge the temporary rigs
bpy.ops.object.join(context)
# Delete the second temp rig
bpy.ops.object.delete()
# Select the generated rig
for objt in scene.objects:
objt.select = False # deselect all objects
obj.select = True
scene.objects.active = obj
# Copy over the pose_bone properties
for bone in obj_orig.pose.bones:
bone_gen = obj.pose.bones[bone.name]
# Rotation mode and transform locks
bone_gen.rotation_mode = bone.rotation_mode
bone_gen.lock_rotation = tuple(bone.lock_rotation)
bone_gen.lock_rotation_w = bone.lock_rotation_w
bone_gen.lock_rotations_4d = bone.lock_rotations_4d
bone_gen.lock_location = tuple(bone.lock_location)
bone_gen.lock_scale = tuple(bone.lock_scale)
# Custom properties
for prop in bone.keys():
bone_gen[prop] = bone[prop]
# Copy over bone properties
for bone in obj_orig.data.bones:
bone_gen = obj.data.bones[bone.name]
# B-bone stuff
bone_gen.bbone_segments = bone.bbone_segments
bone_gen.bbone_in = bone.bbone_in
bone_gen.bbone_out = bone.bbone_out
# Create proxy deformation rig
# TODO: remove this
if META_DEF:
obj_def = obj_orig.copy()
obj_def.data = obj_orig.data.copy()
scene.objects.link(obj_def)
scene.update()
print("On to the real work.")
arm = obj.data
# prepend the ORG prefix to the bones, and create the base_names mapping
base_names = {}
bpy.ops.object.mode_set(mode='EDIT')
for bone in arm.edit_bones:
bone_name = bone.name
bone.name = ORG_PREFIX + bone_name
base_names[bone.name] = bone_name
# create root_bone
bpy.ops.object.mode_set(mode='EDIT')
edit_bone = obj.data.edit_bones.new("root")
root_bone = edit_bone.name
edit_bone.head = (0.0, 0.0, 0.0)
edit_bone.tail = (0.0, 1.0, 0.0)
edit_bone.roll = 0.0
edit_bone.layers = ROOT_LAYERS
bpy.ops.object.mode_set(mode='OBJECT')
# key: bone name
# value: {type:definition, ...}
# where type is the submodule name - leg, arm etc
# and definition is a list of bone names
bone_definitions = {}
# key: bone name
# value: [functions, ...]
# each function is from the module. eg leg.ik, arm.main
bone_typeinfos = {}
# key: bone name
# value: [new_bone_name, ...]
# where each bone with a 'type' stores a list of bones that it created
# ...needed so we can override the root parent
bone_genesis = {}
# inspect all bones and assign their definitions before modifying
for pbone in obj.pose.bones:
bone_name = pbone.name
bone_type = pbone.get("type", "")
if bone_type:
bone_type_list = [bt for bt in bone_type.replace(",", " ").split()]
# not essential but means running autorig again wont do anything
del pbone["type"]
else:
bone_type_list = []
for bone_type in bone_type_list:
type_name, submod, type_func = submodule_func_from_type(bone_type)
reload(submod)
bone_def_dict = bone_definitions.setdefault(bone_name, {})
# Only calculate bone definitions once
if type_name not in bone_def_dict:
bone_def_dict[type_name] = submod.metarig_definition(obj, bone_name)
bone_typeinfo = bone_typeinfos.setdefault(bone_name, [])
bone_typeinfo.append((type_name, type_func))
# sort bones, not needed but gives more pradictable execution which may be useful in rare cases
bones_sorted = obj.pose.bones.values()
bones_sorted.sort(key=lambda pbone: pbone.name) # first sort by names
bones_sorted.sort(key=lambda pbone: len(pbone.parent_recursive)) # parents before children
# now we have all the info about bones we can start operating on them
# for pbone in obj.pose.bones:
for pbone in bones_sorted:
bone_name = pbone.name
print(bone_name)
if bone_name not in bone_typeinfos:
continue
bone_def_dict = bone_definitions[bone_name]
# Only blend results from the same submodule, eg.
# leg.ik and arm.fk could not be blended.
results = OrderedDict()
bone_names_pre = {bone.name for bone in arm.bones}
for type_name, type_func in bone_typeinfos[bone_name]:
print(" " + type_name)
# this bones definition of the current typeinfo
definition = bone_def_dict[type_name]
options = get_bone_type_options(pbone, type_name)
bpy.ops.object.mode_set(mode='EDIT')
ret = type_func(obj, definition, base_names, options)
bpy.ops.object.mode_set(mode='OBJECT')
if ret:
result_submod = results.setdefault(type_name, [])
if result_submod and len(result_submod[-1]) != len(ret):
raise RigifyError("bone lists not compatible: %s, %s" % (result_submod[-1], ret))
result_submod.append(ret)
for result_submod in results.values():
# blend 2 chains
definition = bone_def_dict[type_name]
if len(result_submod) == 2:
blend_bone_list(obj, definition, result_submod[0], result_submod[1], target_bone=bone_name)
bone_names_post = {bone.name for bone in arm.bones}
# Store which bones were created from this one
bone_genesis[bone_name] = list(bone_names_post - bone_names_pre)
# need a reverse lookup on bone_genesis so as to know immediately
# where a bone comes from
bone_genesis_reverse = {}
'''
for bone_name, bone_children in bone_genesis.items():
for bone_child_name in bone_children:
bone_genesis_reverse[bone_child_name] = bone_name
'''
if root_bone:
# assign all new parentless bones to this
bpy.ops.object.mode_set(mode='EDIT')
root_ebone = arm.edit_bones[root_bone]
for ebone in arm.edit_bones:
bone_name = ebone.name
if ebone.parent is None:
ebone.parent = root_ebone
'''
if ebone.parent is None and bone_name not in base_names:
# check for override
bone_creator = bone_genesis_reverse[bone_name]
pbone_creator = obj.pose.bones[bone_creator]
root_bone_override = pbone_creator.get("root", "")
if root_bone_override:
root_ebone_tmp = arm.edit_bones[root_bone_override]
else:
root_ebone_tmp = root_ebone
ebone.use_connect = False
ebone.parent = root_ebone_tmp
'''
bpy.ops.object.mode_set(mode='OBJECT')
if META_DEF:
# for pbone in obj_def.pose.bones:
for bone_name, bone_name_new in base_names.items():
#pbone_from = bone_name
pbone = obj_def.pose.bones[bone_name_new]
con = pbone.constraints.new('COPY_ROTATION')
con.target = obj
con.subtarget = bone_name
if not pbone.bone.use_connect:
con = pbone.constraints.new('COPY_LOCATION')
con.target = obj
con.subtarget = bone_name
# would be 'REST' from when copied
obj_def.data.pose_position = 'POSE'
# todo - make a more generic system?
layer_tot = [False] * 32
layer_last = layer_tot[:]
layer_last[31] = True
layer_second_last = layer_tot[:]
layer_second_last[30] = True
for bone_name, bone in arm.bones.items():
bone.use_deform = False # Non DEF bones shouldn't deform
if bone_name.startswith(ORG_PREFIX):
bone.layers = ORG_LAYERS
elif bone_name.startswith(MCH_PREFIX): # XXX fixme
bone.layers = MCH_LAYERS
elif bone_name.startswith(DEF_PREFIX): # XXX fixme
bone.layers = DEF_LAYERS
bone.use_deform = True
else:
# Assign bone appearance if there is a widget for it
obj.pose.bones[bone_name].custom_shape = context.scene.objects.get(WGT_PREFIX + bone_name)
layer_tot[:] = [max(lay) for lay in zip(layer_tot, bone.layers)]
# Only for demo'ing
layer_show = [a and not (b or c or d) for a, b, c, d in zip(layer_tot, ORG_LAYERS, MCH_LAYERS, DEF_LAYERS)]
arm.layers = layer_show
# obj.hide = True
obj.data.show_axes = False
bpy.ops.object.mode_set(mode=mode_orig)
obj_orig.data.pose_position = rest_backup
obj.data.pose_position = 'POSE'
obj_orig.data.pose_position = 'POSE'
context.user_preferences.edit.use_global_undo = use_global_undo
print("Done.\n")
return obj
def generate_test(context, metarig_type="", GENERATE_FINAL=True):
import os
new_objects = []
scene = context.scene
def create_empty_armature(name):
armature = bpy.data.armatures.new(name)
obj_new = bpy.data.objects.new(name, armature)
scene.objects.link(obj_new)
scene.objects.active = obj_new
for obj in scene.objects:
obj.select = False
obj_new.select = True
for module_name in get_submodule_types():
if (metarig_type and module_name != metarig_type):
continue
# XXX workaround!, problem with updating the pose matrix.
if module_name == "delta":
continue
type_name, submodule, func = submodule_func_from_type(module_name)
metarig_template = getattr(submodule, "metarig_template", None)
if metarig_template:
create_empty_armature("meta_" + module_name) # sets active
metarig_template()
obj = context.active_object
obj.location = scene.cursor_location
if GENERATE_FINAL:
obj_new = generate_rig(context, obj)
new_objects.append((obj, obj_new))
else:
new_objects.append((obj, None))
else:
print("note: rig type '%s' has no metarig_template(), can't test this" % module_name)
return new_objects
def generate_test_all(context, GRAPH=False):
import rigify
import rigify_utils
import graphviz_export
import os
reload(rigify)
reload(rigify_utils)
reload(graphviz_export)
new_objects = rigify.generate_test(context)
if GRAPH:
if(bpy.data.filepath):
base_name = os.path.splitext(bpy.data.filepath)[0]
else:
import tempfile
base_name = tempfile.mktemp(prefix=bpy.app.tempdir)
for obj, obj_new in new_objects:
for obj in (obj, obj_new):
fn = base_name + "-" + bpy.path.clean_name(obj.name)
path_dot = fn + ".dot"
path_png = fn + ".png"
saved = graphviz_export.graph_armature(obj, path_dot, CONSTRAINTS=True, DRIVERS=True)
#if saved:
# os.system("dot -Tpng %s > %s; eog %s" % (path_dot, path_png, path_png))
i = 0
for obj, obj_new in new_objects:
obj.data.draw_type = 'STICK'
obj.location[1] += i
obj_new.location[1] += i
obj_new.select = False
obj.select = True
i += 4
if __name__ == "__main__":
generate_rig(bpy.context, bpy.context.active_object)
|