From 34b236a361394aaa1f21c3043066b995c759f774 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Sat, 14 May 2011 23:37:05 +0000 Subject: Rigify: added a 'copy_chain' rig type. This is useful for bone chains that need their parent-child relationships preserved exactly. For example, bbone chains, and chains that users want to use auto-ik on. Also moved the 'copy' rig type to the basic collection. This will break some existing metarigs, but it is very easy to fix. Just change the 'copy' rig type on bones that use it to 'basic.copy'. --- rigify/metarigs/human.py | 4 +- rigify/rigs/basic/__init__.py | 0 rigify/rigs/basic/copy.py | 114 ++++++++++++++++++++++++++ rigify/rigs/basic/copy_chain.py | 172 ++++++++++++++++++++++++++++++++++++++++ rigify/rigs/copy.py | 114 -------------------------- 5 files changed, 288 insertions(+), 116 deletions(-) create mode 100644 rigify/rigs/basic/__init__.py create mode 100644 rigify/rigs/basic/copy.py create mode 100644 rigify/rigs/basic/copy_chain.py delete mode 100644 rigify/rigs/copy.py diff --git a/rigify/metarigs/human.py b/rigify/metarigs/human.py index 9a67504a..10e09316 100644 --- a/rigify/metarigs/human.py +++ b/rigify/metarigs/human.py @@ -551,7 +551,7 @@ def create(obj): pbone.rotation_mode = 'QUATERNION' pbone.bone.layers = [False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False] pbone = obj.pose.bones[bones['shoulder.L']] - pbone.rigify_type = 'copy' + pbone.rigify_type = 'basic.copy' pbone.lock_location = (True, True, True) pbone.lock_rotation = (False, True, False) pbone.lock_rotation_w = False @@ -559,7 +559,7 @@ def create(obj): pbone.rotation_mode = 'YXZ' pbone.bone.layers = [True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False] pbone = obj.pose.bones[bones['shoulder.R']] - pbone.rigify_type = 'copy' + pbone.rigify_type = 'basic.copy' pbone.lock_location = (True, True, True) pbone.lock_rotation = (False, True, False) pbone.lock_rotation_w = False diff --git a/rigify/rigs/basic/__init__.py b/rigify/rigs/basic/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/rigify/rigs/basic/copy.py b/rigify/rigs/basic/copy.py new file mode 100644 index 00000000..ed6d7816 --- /dev/null +++ b/rigify/rigs/basic/copy.py @@ -0,0 +1,114 @@ +#====================== 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 ======================== + +import bpy +from rigify.utils import MetarigError +from rigify.utils import copy_bone +from rigify.utils import strip_org, make_deformer_name +from rigify.utils import create_bone_widget + + +class Rig: + """ A "copy" rig. All it does is duplicate the original bone and + constrain it. + This is a control and deformation rig. + + """ + def __init__(self, obj, bone, params): + """ Gather and validate data about the rig. + """ + self.obj = obj + self.org_bone = bone + self.org_name = strip_org(bone) + self.params = params + + def generate(self): + """ Generate the rig. + Do NOT modify any of the original bones, except for adding constraints. + The main armature should be selected and active before this is called. + + """ + bpy.ops.object.mode_set(mode='EDIT') + + # Make a control bone (copy of original). + bone = copy_bone(self.obj, self.org_bone, self.org_name) + + # Make a deformation bone (copy of original, child of original). + def_bone = copy_bone(self.obj, self.org_bone, make_deformer_name(self.org_name)) + + # Get edit bones + eb = self.obj.data.edit_bones + bone_e = eb[bone] + def_bone_e = eb[def_bone] + + # Parent + def_bone_e.use_connect = False + def_bone_e.parent = eb[self.org_bone] + + bpy.ops.object.mode_set(mode='OBJECT') + pb = self.obj.pose.bones + + # Constrain the original bone. + con = pb[self.org_bone].constraints.new('COPY_TRANSFORMS') + con.name = "copy_transforms" + con.target = self.obj + con.subtarget = bone + + # Create control widget + create_bone_widget(self.obj, bone) + + @classmethod + def create_sample(self, obj): + """ Create a sample metarig for this rig type. + + """ + # generated by rigify.utils.write_metarig + bpy.ops.object.mode_set(mode='EDIT') + arm = obj.data + + bones = {} + + bone = arm.edit_bones.new('Bone') + bone.head[:] = 0.0000, 0.0000, 0.0000 + bone.tail[:] = 0.0000, 0.0000, 0.2000 + bone.roll = 0.0000 + bone.use_connect = False + bones['Bone'] = bone.name + + bpy.ops.object.mode_set(mode='OBJECT') + pbone = obj.pose.bones[bones['Bone']] + pbone.rigify_type = 'simple.bone' + pbone.lock_location = (False, False, False) + pbone.lock_rotation = (False, False, False) + pbone.lock_rotation_w = False + pbone.lock_scale = (False, False, False) + pbone.rotation_mode = 'QUATERNION' + pbone.rigify_parameters.add() + + bpy.ops.object.mode_set(mode='EDIT') + for bone in arm.edit_bones: + bone.select = False + bone.select_head = False + bone.select_tail = False + for b in bones: + bone = arm.edit_bones[bones[b]] + bone.select = True + bone.select_head = True + bone.select_tail = True + arm.edit_bones.active = bone + diff --git a/rigify/rigs/basic/copy_chain.py b/rigify/rigs/basic/copy_chain.py new file mode 100644 index 00000000..930e025b --- /dev/null +++ b/rigify/rigs/basic/copy_chain.py @@ -0,0 +1,172 @@ +#====================== 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 ======================== + +import bpy +from rigify.utils import MetarigError +from rigify.utils import copy_bone +from rigify.utils import connected_children_names +from rigify.utils import strip_org, make_deformer_name +from rigify.utils import create_bone_widget + + +class Rig: + """ A "copy_chain" rig. All it does is duplicate the original bone chain + and constrain it. + This is a control and deformation rig. + + """ + def __init__(self, obj, bone_name, params): + """ Gather and validate data about the rig. + """ + self.obj = obj + self.org_bones = [bone_name] + connected_children_names(obj, bone_name) + self.params = params + + if len(self.org_bones) <= 1: + raise MetarigError("RIGIFY ERROR: Bone '%s': input to rig type must be a chain of 2 or more bones." % (strip_org(bone))) + + def generate(self): + """ Generate the rig. + Do NOT modify any of the original bones, except for adding constraints. + The main armature should be selected and active before this is called. + + """ + bpy.ops.object.mode_set(mode='EDIT') + + # Create the deformation and control bone chains. + # Just copies of the original chain. + def_chain = [] + ctrl_chain = [] + for i in range(len(self.org_bones)): + name = self.org_bones[i] + + # Create bones + def_bone = copy_bone(self.obj, name) + ctrl_bone = copy_bone(self.obj, name) + + # Get edit bones + eb = self.obj.data.edit_bones + def_bone_e = eb[def_bone] + ctrl_bone_e = eb[ctrl_bone] + + # Set their names + def_bone_e.name = make_deformer_name(strip_org(name)) + ctrl_bone_e.name = strip_org(name) + + # Add them to their respective lists + def_chain += [def_bone_e.name] + ctrl_chain += [ctrl_bone_e.name] + + # Parenting + if i == 0: + # First bone + def_bone_e.parent = eb[self.org_bones[i]].parent + ctrl_bone_e.parent = eb[self.org_bones[i]].parent + else: + # The rest + def_bone_e.parent = eb[def_chain[i-1]] + ctrl_bone_e.parent = eb[ctrl_chain[i-1]] + + bpy.ops.object.mode_set(mode='OBJECT') + pb = self.obj.pose.bones + + # Constraint org and def to the control bones + for org, ctrl, defrm in zip(self.org_bones, ctrl_chain, def_chain): + con = pb[org].constraints.new('COPY_TRANSFORMS') + con.name = "copy_transforms" + con.target = self.obj + con.subtarget = ctrl + + con = pb[defrm].constraints.new('COPY_TRANSFORMS') + con.name = "copy_transforms" + con.target = self.obj + con.subtarget = ctrl + + # Create control widgets + for bone in ctrl_chain: + create_bone_widget(self.obj, bone) + + @classmethod + def create_sample(self, obj): + """ Create a sample metarig for this rig type. + + """ + # generated by rigify.utils.write_metarig + bpy.ops.object.mode_set(mode='EDIT') + arm = obj.data + + bones = {} + + bone = arm.edit_bones.new('bone.01') + bone.head[:] = 0.0000, 0.0000, 0.0000 + bone.tail[:] = 0.0000, 0.0000, 0.3333 + bone.roll = 0.0000 + bone.use_connect = False + bones['bone.01'] = bone.name + bone = arm.edit_bones.new('bone.02') + bone.head[:] = 0.0000, 0.0000, 0.3333 + bone.tail[:] = 0.0000, 0.0000, 0.6667 + bone.roll = 3.1416 + bone.use_connect = True + bone.parent = arm.edit_bones[bones['bone.01']] + bones['bone.02'] = bone.name + bone = arm.edit_bones.new('bone.03') + bone.head[:] = 0.0000, 0.0000, 0.6667 + bone.tail[:] = 0.0000, 0.0000, 1.0000 + bone.roll = 3.1416 + bone.use_connect = True + bone.parent = arm.edit_bones[bones['bone.02']] + bones['bone.03'] = bone.name + + bpy.ops.object.mode_set(mode='OBJECT') + pbone = obj.pose.bones[bones['bone.01']] + pbone.rigify_type = 'basic.copy_chain' + pbone.lock_location = (False, False, False) + pbone.lock_rotation = (False, False, False) + pbone.lock_rotation_w = False + pbone.lock_scale = (False, False, False) + pbone.rotation_mode = 'QUATERNION' + pbone.rigify_parameters.add() + pbone = obj.pose.bones[bones['bone.02']] + pbone.rigify_type = '' + pbone.lock_location = (False, False, False) + pbone.lock_rotation = (False, False, False) + pbone.lock_rotation_w = False + pbone.lock_scale = (False, False, False) + pbone.rotation_mode = 'QUATERNION' + pbone = obj.pose.bones[bones['bone.03']] + pbone.rigify_type = '' + pbone.lock_location = (False, False, False) + pbone.lock_rotation = (False, False, False) + pbone.lock_rotation_w = False + pbone.lock_scale = (False, False, False) + pbone.rotation_mode = 'QUATERNION' + + bpy.ops.object.mode_set(mode='EDIT') + for bone in arm.edit_bones: + bone.select = False + bone.select_head = False + bone.select_tail = False + for b in bones: + bone = arm.edit_bones[bones[b]] + bone.select = True + bone.select_head = True + bone.select_tail = True + arm.edit_bones.active = bone + + diff --git a/rigify/rigs/copy.py b/rigify/rigs/copy.py deleted file mode 100644 index 61c4cc99..00000000 --- a/rigify/rigs/copy.py +++ /dev/null @@ -1,114 +0,0 @@ -#====================== 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 ======================== - -import bpy -from rigify.utils import MetarigError -from rigify.utils import copy_bone -from rigify.utils import strip_org, make_deformer_name -from rigify.utils import create_bone_widget - - -class Rig: - """ A "copy" rig. All it does is duplicate the original bone and - constrain it. - This is a control and deformation rig. - - """ - def __init__(self, obj, bone, params): - """ Gather and validate data about the rig. - """ - self.obj = obj - self.org_bone = bone - self.org_name = strip_org(bone) - self.params = params - - def generate(self): - """ Generate the rig. - Do NOT modify any of the original bones, except for adding constraints. - The main armature should be selected and active before this is called. - - """ - bpy.ops.object.mode_set(mode='EDIT') - - # Make a control bone (copy of original). - bone = copy_bone(self.obj, self.org_bone, self.org_name) - - # Make a deformation bone (copy of original, child of original). - def_bone = copy_bone(self.obj, self.org_bone, make_deformer_name(self.org_name)) - - # Get edit bones - eb = self.obj.data.edit_bones - bone_e = eb[bone] - def_bone_e = eb[def_bone] - - # Parent - def_bone_e.use_connect = False - def_bone_e.parent = eb[self.org_bone] - - bpy.ops.object.mode_set(mode='OBJECT') - pb = self.obj.pose.bones - - # Constrain the original bone. - con = pb[self.org_bone].constraints.new('COPY_TRANSFORMS') - con.name = "copy_loc" - con.target = self.obj - con.subtarget = bone - - # Create control widget - create_bone_widget(self.obj, bone) - - @classmethod - def create_sample(self, obj): - """ Create a sample metarig for this rig type. - - """ - # generated by rigify.utils.write_metarig - bpy.ops.object.mode_set(mode='EDIT') - arm = obj.data - - bones = {} - - bone = arm.edit_bones.new('Bone') - bone.head[:] = 0.0000, 0.0000, 0.0000 - bone.tail[:] = 0.0000, 0.0000, 0.2000 - bone.roll = 0.0000 - bone.use_connect = False - bones['Bone'] = bone.name - - bpy.ops.object.mode_set(mode='OBJECT') - pbone = obj.pose.bones[bones['Bone']] - pbone.rigify_type = 'copy' - pbone.lock_location = (False, False, False) - pbone.lock_rotation = (False, False, False) - pbone.lock_rotation_w = False - pbone.lock_scale = (False, False, False) - pbone.rotation_mode = 'QUATERNION' - pbone.rigify_parameters.add() - - bpy.ops.object.mode_set(mode='EDIT') - for bone in arm.edit_bones: - bone.select = False - bone.select_head = False - bone.select_tail = False - for b in bones: - bone = arm.edit_bones[bones[b]] - bone.select = True - bone.select_head = True - bone.select_tail = True - arm.edit_bones.active = bone - -- cgit v1.2.3