Rigify now generates the rig into the same armature every time, so you don't have to re-hook-up things like armature modifiers, parenting, etc.
By default the generated rig object is named "rig". But you can add a custom "rig_object_name" property to the metarig to specify the name of the object to generate into.
This commit is contained in:
@@ -154,6 +154,8 @@ def generate_rig(context, obj_orig, prefix="ORG-", META_DEF=True):
|
||||
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)
|
||||
@@ -164,23 +166,85 @@ def generate_rig(context, obj_orig, prefix="ORG-", META_DEF=True):
|
||||
rest_backup = obj_orig.data.pose_position
|
||||
obj_orig.data.pose_position = 'REST'
|
||||
|
||||
|
||||
bpy.ops.object.mode_set(mode='OBJECT')
|
||||
|
||||
scene = context.scene
|
||||
|
||||
# copy object and data
|
||||
# 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, type='ARMATURE')
|
||||
obj.data = 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.selected = False
|
||||
obj = obj_orig.copy()
|
||||
obj.data = obj_orig.data.copy()
|
||||
scene.objects.link(obj)
|
||||
scene.objects.active = obj
|
||||
obj.selected = 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.selected = False # deselect all objects
|
||||
temp_rig_1.selected = True
|
||||
temp_rig_2.selected = 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.selected = False # deselect all objects
|
||||
obj.selected = True
|
||||
scene.objects.active = obj
|
||||
|
||||
# Copy over the pose_bone custom properties
|
||||
for bone in obj_orig.pose.bones:
|
||||
for prop in bone.keys():
|
||||
obj.pose.bones[bone.name][prop] = bone[prop]
|
||||
|
||||
# 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
|
||||
|
||||
@@ -255,7 +319,7 @@ def generate_rig(context, obj_orig, prefix="ORG-", META_DEF=True):
|
||||
# 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
|
||||
|
||||
@@ -268,6 +332,7 @@ def generate_rig(context, obj_orig, prefix="ORG-", META_DEF=True):
|
||||
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)
|
||||
@@ -386,7 +451,10 @@ def generate_rig(context, obj_orig, prefix="ORG-", META_DEF=True):
|
||||
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.global_undo = global_undo
|
||||
|
||||
print("Done.\n")
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
Reference in New Issue
Block a user