Skip to content

Commit b0c13d6

Browse files
committed
Convert four simplest rig types to the new base class.
These types demonstrate the use of all callbacks, bone creation and parenting utilities, constraint & driver creation tools, and python script generation.
1 parent 9bc46d0 commit b0c13d6

File tree

5 files changed

+609
-713
lines changed

5 files changed

+609
-713
lines changed

rigs/basic/copy_chain.py

Lines changed: 58 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -20,124 +20,83 @@
2020

2121
import bpy
2222

23-
from ...utils import MetarigError
24-
from ...utils import copy_bone
25-
from ...utils import connected_children_names
26-
from ...utils import strip_org, make_deformer_name
27-
from ...utils import create_bone_widget
23+
from ..chain_rigs import SimpleChainRig
2824

25+
from ...utils.errors import MetarigError
26+
from ...utils.rig import connected_children_names
27+
from ...utils.naming import strip_org, make_deformer_name
28+
from ...utils.widgets_basic import create_bone_widget
29+
from ...utils.misc import map_list, map_apply
2930

30-
class Rig:
31+
32+
class Rig(SimpleChainRig):
3133
""" A "copy_chain" rig. All it does is duplicate the original bone chain
3234
and constrain it.
3335
This is a control and deformation rig.
34-
3536
"""
36-
def __init__(self, obj, bone_name, params):
37+
def initialize(self):
38+
super(Rig,self).initialize()
39+
3740
""" Gather and validate data about the rig.
3841
"""
39-
self.obj = obj
40-
self.org_bones = [bone_name] + connected_children_names(obj, bone_name)
41-
self.params = params
42-
self.make_controls = params.make_controls
43-
self.make_deforms = params.make_deforms
42+
self.make_controls = self.params.make_controls
43+
self.make_deforms = self.params.make_deforms
4444

45-
if len(self.org_bones) <= 1:
46-
raise MetarigError("RIGIFY ERROR: Bone '%s': input to rig type must be a chain of 2 or more bones" % (strip_org(bone_name)))
4745

48-
def generate(self):
49-
""" Generate the rig.
50-
Do NOT modify any of the original bones, except for adding constraints.
51-
The main armature should be selected and active before this is called.
46+
def generate_bones(self):
47+
# Create the deformation and control bone chains.
48+
if self.make_controls:
49+
self.make_control_chain()
5250

53-
"""
54-
bpy.ops.object.mode_set(mode='EDIT')
51+
if self.make_deforms:
52+
self.make_deform_chain()
53+
54+
55+
def parent_bones(self):
56+
if self.make_controls:
57+
self.parent_control_chain()
58+
59+
if self.make_deforms:
60+
self.parent_deform_chain()
5561

56-
# Create the deformation and control bone chains.
57-
# Just copies of the original chain.
58-
def_chain = []
59-
ctrl_chain = []
60-
for i in range(len(self.org_bones)):
61-
name = self.org_bones[i]
62-
63-
# Control bone
64-
if self.make_controls:
65-
# Copy
66-
ctrl_bone = copy_bone(self.obj, name)
67-
eb = self.obj.data.edit_bones
68-
ctrl_bone_e = eb[ctrl_bone]
69-
# Name
70-
ctrl_bone_e.name = strip_org(name)
71-
# Parenting
72-
if i == 0:
73-
# First bone
74-
ctrl_bone_e.parent = eb[self.org_bones[0]].parent
75-
else:
76-
# The rest
77-
ctrl_bone_e.parent = eb[ctrl_chain[-1]]
78-
# Add to list
79-
ctrl_chain += [ctrl_bone_e.name]
80-
else:
81-
ctrl_chain += [None]
82-
83-
# Deformation bone
84-
if self.make_deforms:
85-
# Copy
86-
def_bone = copy_bone(self.obj, name)
87-
eb = self.obj.data.edit_bones
88-
def_bone_e = eb[def_bone]
89-
# Name
90-
def_bone_e.name = make_deformer_name(strip_org(name))
91-
# Parenting
92-
if i == 0:
93-
# First bone
94-
def_bone_e.parent = eb[self.org_bones[0]].parent
95-
else:
96-
# The rest
97-
def_bone_e.parent = eb[def_chain[-1]]
98-
# Add to list
99-
def_chain += [def_bone_e.name]
100-
else:
101-
def_chain += [None]
102-
103-
bpy.ops.object.mode_set(mode='OBJECT')
104-
pb = self.obj.pose.bones
105-
106-
# Constraints for org and def
107-
for org, ctrl, defrm in zip(self.org_bones, ctrl_chain, def_chain):
108-
if self.make_controls:
109-
con = pb[org].constraints.new('COPY_TRANSFORMS')
110-
con.name = "copy_transforms"
111-
con.target = self.obj
112-
con.subtarget = ctrl
113-
114-
if self.make_deforms:
115-
con = pb[defrm].constraints.new('COPY_TRANSFORMS')
116-
con.name = "copy_transforms"
117-
con.target = self.obj
118-
con.subtarget = org
11962

63+
def configure_bones(self):
64+
if self.make_controls:
65+
self.configure_control_chain()
66+
67+
68+
def rig_bones(self):
69+
# Create the deformation and control bone chain constraints.
70+
if self.make_controls:
71+
self.rig_org_chain()
72+
73+
if self.make_deforms:
74+
self.rig_deform_chain()
75+
76+
77+
def generate_widgets(self):
12078
# Create control widgets
12179
if self.make_controls:
122-
for bone in ctrl_chain:
123-
create_bone_widget(self.obj, bone)
80+
self.make_control_widgets()
12481

12582

126-
def add_parameters(params):
127-
""" Add the parameters of this rig type to the
128-
RigifyParameters PropertyGroup
129-
"""
130-
params.make_controls = bpy.props.BoolProperty(name="Controls", default=True, description="Create control bones for the copy")
131-
params.make_deforms = bpy.props.BoolProperty(name="Deform", default=True, description="Create deform bones for the copy")
83+
@classmethod
84+
def add_parameters(self, params):
85+
""" Add the parameters of this rig type to the
86+
RigifyParameters PropertyGroup
87+
"""
88+
params.make_controls = bpy.props.BoolProperty(name="Controls", default=True, description="Create control bones for the copy")
89+
params.make_deforms = bpy.props.BoolProperty(name="Deform", default=True, description="Create deform bones for the copy")
13290

13391

134-
def parameters_ui(layout, params):
135-
""" Create the ui for the rig parameters.
136-
"""
137-
r = layout.row()
138-
r.prop(params, "make_controls")
139-
r = layout.row()
140-
r.prop(params, "make_deforms")
92+
@classmethod
93+
def parameters_ui(self, layout, params):
94+
""" Create the ui for the rig parameters.
95+
"""
96+
r = layout.row()
97+
r.prop(params, "make_controls")
98+
r = layout.row()
99+
r.prop(params, "make_deforms")
141100

142101

143102
def create_sample(obj):

rigs/basic/super_copy.py

Lines changed: 77 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -20,107 +20,112 @@
2020

2121
import bpy
2222

23-
from ...utils import copy_bone
24-
from ...utils import strip_org, make_deformer_name
25-
from ...utils import create_bone_widget, create_circle_widget
23+
from ...base_rig import BaseRig
2624

25+
from ...utils.naming import strip_org, make_deformer_name
26+
from ...utils.widgets_basic import create_bone_widget, create_circle_widget
2727

28-
class Rig:
28+
29+
class Rig(BaseRig):
2930
""" A "copy" rig. All it does is duplicate the original bone and
3031
constrain it.
3132
This is a control and deformation rig.
3233
3334
"""
34-
def __init__(self, obj, bone, params):
35+
def find_org_bones(self, pose_bone):
36+
return pose_bone.name
37+
38+
39+
def initialize(self):
3540
""" Gather and validate data about the rig.
3641
"""
37-
self.obj = obj
38-
self.org_bone = bone
39-
self.org_name = strip_org(bone)
40-
self.params = params
41-
self.make_control = params.make_control
42-
self.make_widget = params.make_widget
43-
self.make_deform = params.make_deform
44-
45-
def generate(self):
46-
""" Generate the rig.
47-
Do NOT modify any of the original bones, except for adding constraints.
48-
The main armature should be selected and active before this is called.
42+
self.org_name = strip_org(self.bones.org)
43+
44+
self.make_control = self.params.make_control
45+
self.make_widget = self.params.make_widget
46+
self.make_deform = self.params.make_deform
4947

50-
"""
51-
bpy.ops.object.mode_set(mode='EDIT')
48+
49+
def generate_bones(self):
50+
bones = self.bones
5251

5352
# Make a control bone (copy of original).
5453
if self.make_control:
55-
bone = copy_bone(self.obj, self.org_bone, self.org_name)
54+
bones.ctrl = self.copy_bone(bones.org, self.org_name, parent=True)
5655

5756
# Make a deformation bone (copy of original, child of original).
5857
if self.make_deform:
59-
def_bone = copy_bone(self.obj, self.org_bone, make_deformer_name(self.org_name))
58+
bones.deform = self.copy_bone(bones.org, make_deformer_name(self.org_name), bbone=True)
6059

61-
# Get edit bones
62-
eb = self.obj.data.edit_bones
63-
# UNUSED
64-
# if self.make_control:
65-
# bone_e = eb[bone]
66-
if self.make_deform:
67-
def_bone_e = eb[def_bone]
6860

69-
# Parent
61+
def parent_bones(self):
62+
bones = self.bones
63+
7064
if self.make_deform:
71-
def_bone_e.use_connect = False
72-
def_bone_e.parent = eb[self.org_bone]
65+
self.set_bone_parent(bones.deform, bones.org, use_connect=False)
66+
67+
68+
def configure_bones(self):
69+
bones = self.bones
7370

74-
bpy.ops.object.mode_set(mode='OBJECT')
75-
pb = self.obj.pose.bones
71+
if self.make_control:
72+
self.copy_bone_properties(bones.org, bones.ctrl)
73+
74+
75+
def rig_bones(self):
76+
bones = self.bones
7677

7778
if self.make_control:
7879
# Constrain the original bone.
79-
con = pb[self.org_bone].constraints.new('COPY_TRANSFORMS')
80-
con.name = "copy_transforms"
81-
con.target = self.obj
82-
con.subtarget = bone
80+
self.make_constraint(bones.org, 'COPY_TRANSFORMS', bones.ctrl)
81+
82+
83+
def generate_widgets(self):
84+
bones = self.bones
8385

86+
if self.make_control:
8487
# Create control widget
8588
if self.make_widget:
86-
create_circle_widget(self.obj, bone, radius=0.5)
89+
create_circle_widget(self.obj, bones.ctrl, radius=0.5)
8790
else:
88-
create_bone_widget(self.obj, bone)
91+
create_bone_widget(self.obj, bones.ctrl)
8992

9093

91-
def add_parameters(params):
92-
""" Add the parameters of this rig type to the
93-
RigifyParameters PropertyGroup
94-
"""
95-
params.make_control = bpy.props.BoolProperty(
96-
name = "Control",
97-
default = True,
98-
description = "Create a control bone for the copy"
99-
)
100-
101-
params.make_widget = bpy.props.BoolProperty(
102-
name = "Widget",
103-
default = True,
104-
description = "Choose a widget for the bone control"
105-
)
106-
107-
params.make_deform = bpy.props.BoolProperty(
108-
name = "Deform",
109-
default = True,
110-
description = "Create a deform bone for the copy"
111-
)
112-
113-
114-
def parameters_ui(layout, params):
115-
""" Create the ui for the rig parameters.
116-
"""
117-
r = layout.row()
118-
r.prop(params, "make_control")
119-
r = layout.row()
120-
r.prop(params, "make_widget")
121-
r.enabled = params.make_control
122-
r = layout.row()
123-
r.prop(params, "make_deform")
94+
@classmethod
95+
def add_parameters(self, params):
96+
""" Add the parameters of this rig type to the
97+
RigifyParameters PropertyGroup
98+
"""
99+
params.make_control = bpy.props.BoolProperty(
100+
name = "Control",
101+
default = True,
102+
description = "Create a control bone for the copy"
103+
)
104+
105+
params.make_widget = bpy.props.BoolProperty(
106+
name = "Widget",
107+
default = True,
108+
description = "Choose a widget for the bone control"
109+
)
110+
111+
params.make_deform = bpy.props.BoolProperty(
112+
name = "Deform",
113+
default = True,
114+
description = "Create a deform bone for the copy"
115+
)
116+
117+
118+
@classmethod
119+
def parameters_ui(self, layout, params):
120+
""" Create the ui for the rig parameters.
121+
"""
122+
r = layout.row()
123+
r.prop(params, "make_control")
124+
r = layout.row()
125+
r.prop(params, "make_widget")
126+
r.enabled = params.make_control
127+
r = layout.row()
128+
r.prop(params, "make_deform")
124129

125130

126131
def create_sample(obj):

0 commit comments

Comments
 (0)