Changeset View
Changeset View
Standalone View
Standalone View
rigify/rigs/basic/copy_chain.py
| Show All 14 Lines | |||||
| # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
| # | # | ||||
| #======================= END GPL LICENSE BLOCK ======================== | #======================= END GPL LICENSE BLOCK ======================== | ||||
| # <pep8 compliant> | # <pep8 compliant> | ||||
| import bpy | import bpy | ||||
| from ...utils import MetarigError | from ..chain_rigs import SimpleChainRig | ||||
| from ...utils import copy_bone | |||||
| from ...utils import connected_children_names | |||||
| from ...utils import strip_org, make_deformer_name | |||||
| from ...utils import create_bone_widget | |||||
| from ...utils.errors import MetarigError | |||||
| from ...utils.rig import connected_children_names | |||||
| from ...utils.naming import strip_org, make_deformer_name | |||||
| from ...utils.widgets_basic import create_bone_widget | |||||
| class Rig: | from ...base_rig import BaseRig, stage | ||||
| class Rig(SimpleChainRig): | |||||
| """ A "copy_chain" rig. All it does is duplicate the original bone chain | """ A "copy_chain" rig. All it does is duplicate the original bone chain | ||||
| and constrain it. | and constrain it. | ||||
| This is a control and deformation rig. | This is a control and deformation rig. | ||||
| """ | """ | ||||
| def __init__(self, obj, bone_name, params): | def initialize(self): | ||||
| super().initialize() | |||||
| """ Gather and validate data about the rig. | """ Gather and validate data about the rig. | ||||
| """ | """ | ||||
| self.obj = obj | self.make_controls = self.params.make_controls | ||||
| self.org_bones = [bone_name] + connected_children_names(obj, bone_name) | self.make_deforms = self.params.make_deforms | ||||
| self.params = params | |||||
| self.make_controls = params.make_controls | |||||
| self.make_deforms = params.make_deforms | |||||
| 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_name))) | |||||
| 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') | # Control chain | ||||
| # Create the deformation and control bone chains. | @stage.generate_bones | ||||
| # Just copies of the original chain. | def make_control_chain(self): | ||||
| def_chain = [] | if self.make_controls: | ||||
| ctrl_chain = [] | super().make_control_chain() | ||||
| for i in range(len(self.org_bones)): | |||||
| name = self.org_bones[i] | |||||
| # Control bone | @stage.parent_bones | ||||
| def parent_control_chain(self): | |||||
| if self.make_controls: | if self.make_controls: | ||||
| # Copy | super().parent_control_chain() | ||||
| ctrl_bone = copy_bone(self.obj, name) | |||||
| eb = self.obj.data.edit_bones | |||||
| ctrl_bone_e = eb[ctrl_bone] | |||||
| # Name | |||||
| ctrl_bone_e.name = strip_org(name) | |||||
| # Parenting | |||||
| if i == 0: | |||||
| # First bone | |||||
| ctrl_bone_e.parent = eb[self.org_bones[0]].parent | |||||
| else: | |||||
| # The rest | |||||
| ctrl_bone_e.parent = eb[ctrl_chain[-1]] | |||||
| # Add to list | |||||
| ctrl_chain += [ctrl_bone_e.name] | |||||
| else: | |||||
| ctrl_chain += [None] | |||||
| # Deformation bone | @stage.configure_bones | ||||
| if self.make_deforms: | def configure_control_chain(self): | ||||
| # Copy | if self.make_controls: | ||||
| def_bone = copy_bone(self.obj, name) | super().configure_control_chain() | ||||
| eb = self.obj.data.edit_bones | |||||
| def_bone_e = eb[def_bone] | |||||
| # Name | |||||
| def_bone_e.name = make_deformer_name(strip_org(name)) | |||||
| # Parenting | |||||
| if i == 0: | |||||
| # First bone | |||||
| def_bone_e.parent = eb[self.org_bones[0]].parent | |||||
| else: | |||||
| # The rest | |||||
| def_bone_e.parent = eb[def_chain[-1]] | |||||
| # Add to list | |||||
| def_chain += [def_bone_e.name] | |||||
| else: | |||||
| def_chain += [None] | |||||
| bpy.ops.object.mode_set(mode='OBJECT') | @stage.generate_widgets | ||||
| pb = self.obj.pose.bones | def make_control_widgets(self): | ||||
| if self.make_controls: | |||||
| super().make_control_widgets() | |||||
| # Constraints for org and def | ############################## | ||||
| for org, ctrl, defrm in zip(self.org_bones, ctrl_chain, def_chain): | # ORG chain | ||||
| @stage.rig_bones | |||||
| def rig_org_chain(self): | |||||
| if self.make_controls: | if self.make_controls: | ||||
| con = pb[org].constraints.new('COPY_TRANSFORMS') | super().rig_org_chain() | ||||
| con.name = "copy_transforms" | |||||
| con.target = self.obj | ############################## | ||||
| con.subtarget = ctrl | # Deform chain | ||||
| @stage.generate_bones | |||||
| def make_deform_chain(self): | |||||
| if self.make_deforms: | if self.make_deforms: | ||||
| con = pb[defrm].constraints.new('COPY_TRANSFORMS') | super().make_deform_chain() | ||||
| con.name = "copy_transforms" | |||||
| con.target = self.obj | |||||
| con.subtarget = org | |||||
| # Create control widgets | @stage.parent_bones | ||||
| if self.make_controls: | def parent_deform_chain(self): | ||||
| for bone in ctrl_chain: | if self.make_deforms: | ||||
| create_bone_widget(self.obj, bone) | super().parent_deform_chain() | ||||
| @stage.rig_bones | |||||
| def rig_deform_chain(self): | |||||
| if self.make_deforms: | |||||
| super().rig_deform_chain() | |||||
| def add_parameters(params): | @classmethod | ||||
| def add_parameters(self, params): | |||||
| """ Add the parameters of this rig type to the | """ Add the parameters of this rig type to the | ||||
| RigifyParameters PropertyGroup | RigifyParameters PropertyGroup | ||||
| """ | """ | ||||
| params.make_controls = bpy.props.BoolProperty(name="Controls", default=True, description="Create control bones for the copy") | params.make_controls = bpy.props.BoolProperty(name="Controls", default=True, description="Create control bones for the copy") | ||||
| params.make_deforms = bpy.props.BoolProperty(name="Deform", default=True, description="Create deform bones for the copy") | params.make_deforms = bpy.props.BoolProperty(name="Deform", default=True, description="Create deform bones for the copy") | ||||
| def parameters_ui(layout, params): | @classmethod | ||||
| def parameters_ui(self, layout, params): | |||||
| """ Create the ui for the rig parameters. | """ Create the ui for the rig parameters. | ||||
| """ | """ | ||||
| r = layout.row() | r = layout.row() | ||||
| r.prop(params, "make_controls") | r.prop(params, "make_controls") | ||||
| r = layout.row() | r = layout.row() | ||||
| r.prop(params, "make_deforms") | r.prop(params, "make_deforms") | ||||
| def create_sample(obj): | def create_sample(obj): | ||||
| """ Create a sample metarig for this rig type. | """ Create a sample metarig for this rig type. | ||||
| """ | """ | ||||
| # generated by rigify.utils.write_metarig | # generated by rigify.utils.write_metarig | ||||
| bpy.ops.object.mode_set(mode='EDIT') | bpy.ops.object.mode_set(mode='EDIT') | ||||
| arm = obj.data | arm = obj.data | ||||
| ▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines | |||||