From ede3f0285d351923ee337ecc4b79d04afd745df2 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Mon, 25 Jan 2010 14:19:12 +0000 Subject: 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. --- release/scripts/modules/rigify/__init__.py | 82 ++++++++++++++++++++++++++--- release/scripts/modules/rigify/arm_biped.py | 4 +- release/scripts/modules/rigify/leg_biped.py | 3 +- release/scripts/modules/rigify/mouth.py | 19 ++----- 4 files changed, 83 insertions(+), 25 deletions(-) diff --git a/release/scripts/modules/rigify/__init__.py b/release/scripts/modules/rigify/__init__.py index 089542263e6..7cf66932ce6 100644 --- a/release/scripts/modules/rigify/__init__.py +++ b/release/scripts/modules/rigify/__init__.py @@ -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) + 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 diff --git a/release/scripts/modules/rigify/arm_biped.py b/release/scripts/modules/rigify/arm_biped.py index f3d449e5aff..56a7fca25a4 100644 --- a/release/scripts/modules/rigify/arm_biped.py +++ b/release/scripts/modules/rigify/arm_biped.py @@ -19,7 +19,7 @@ # import bpy -from math import radians +from math import radians, pi from rigify import RigifyError, get_layer_dict, ORG_PREFIX from rigify_utils import bone_class_instance, copy_bone_simple, add_pole_target_bone, add_stretch_to, blend_bone_list, get_side_name, get_base_name from rna_prop_ui import rna_idprop_ui_prop_get @@ -162,7 +162,7 @@ def ik(obj, definitions, base_names, options): con.use_target = True con.use_rotation = False con.chain_length = 2 - con.pole_angle = -90.0 # XXX, RAD2DEG + con.pole_angle = -pi/2 # last step setup layers if "ik_layer" in options: diff --git a/release/scripts/modules/rigify/leg_biped.py b/release/scripts/modules/rigify/leg_biped.py index 5c214cea362..5c243727516 100644 --- a/release/scripts/modules/rigify/leg_biped.py +++ b/release/scripts/modules/rigify/leg_biped.py @@ -19,6 +19,7 @@ # import bpy +from math import pi from rigify import RigifyError, get_layer_dict from rigify_utils import bone_class_instance, copy_bone_simple, blend_bone_list, get_side_name, get_base_name from rna_prop_ui import rna_idprop_ui_prop_get @@ -226,7 +227,7 @@ def ik(obj, bone_definition, base_names, options): con = ik_chain.shin_p.constraints.new('IK') con.chain_length = 2 con.iterations = 500 - con.pole_angle = -90.0 # XXX - in deg! + con.pole_angle = -pi/2 con.use_tail = True con.use_stretch = True con.use_target = True diff --git a/release/scripts/modules/rigify/mouth.py b/release/scripts/modules/rigify/mouth.py index 77545bc09fd..7e0ec74a787 100644 --- a/release/scripts/modules/rigify/mouth.py +++ b/release/scripts/modules/rigify/mouth.py @@ -121,15 +121,11 @@ def deform(obj, definitions, base_names, options): eb = obj.data.edit_bones pb = obj.pose.bones - print("YAHOO") - # Options req_options = ["mesh"] for option in req_options: if option not in options: raise RigifyError("'%s' rig type requires a '%s' option (bone: %s)" % (RIG_TYPE, option, base_names[definitions[0]])) - - print("YAHOO2") meshes = options["mesh"].replace(" ", "").split(",") @@ -214,9 +210,7 @@ def deform(obj, definitions, base_names, options): eb[dlip7].bbone_segments = 8 eb[dlip8].bbone_segments = 8 - print("OBJECT MODE1") bpy.ops.object.mode_set(mode='OBJECT') - print("OBJECT MODE2") # Constraints con = pb[dlip1].constraints.new('COPY_TRANSFORMS') @@ -258,8 +252,6 @@ def deform(obj, definitions, base_names, options): rotdiff_r = acos(eb[lip1].matrix.to_quat() * eb[lip8].matrix.to_quat()) * 2 rotdiff_l = acos(eb[lip4].matrix.to_quat() * eb[lip5].matrix.to_quat()) * 2 - print (rotdiff_l) - bpy.ops.object.mode_set(mode='OBJECT') @@ -286,10 +278,10 @@ def deform(obj, definitions, base_names, options): # Set up the variable var.type = "ROTATION_DIFF" - var.targets[0].id_type = 'OBJECT' + #var.targets[0].id_type = 'OBJECT' var.targets[0].id = obj var.targets[0].bone_target = lip4 - var.targets[1].id_type = 'OBJECT' + #var.targets[1].id_type = 'OBJECT' var.targets[1].id = obj var.targets[1].bone_target = lip5 @@ -321,10 +313,10 @@ def deform(obj, definitions, base_names, options): # Set up the variable var.type = "ROTATION_DIFF" - var.targets[0].id_type = 'OBJECT' + #var.targets[0].id_type = 'OBJECT' var.targets[0].id = obj var.targets[0].bone_target = lip1 - var.targets[1].id_type = 'OBJECT' + #var.targets[1].id_type = 'OBJECT' var.targets[1].id = obj var.targets[1].bone_target = lip8 @@ -723,12 +715,9 @@ def control(obj, definitions, base_names, options): def main(obj, bone_definition, base_names, options): # Create control rig - print("CONTROL") control(obj, bone_definition, base_names, options) - print("DEFORM") # Create deform rig deform(obj, bone_definition, base_names, options) - print("DONE") return (None,) -- cgit v1.2.3