1
# ##### BEGIN GPL LICENSE BLOCK #####
3
# This program is free software; you can redistribute it and/or
4
# modify it under the terms of the GNU General Public License
5
# as published by the Free Software Foundation; either version 2
6
# of the License, or (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software Foundation,
15
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
# ##### END GPL LICENSE BLOCK #####
22
narrowui = bpy.context.user_preferences.view.properties_width_check
25
class PoseTemplateSettings(bpy.types.IDPropertyGroup):
29
class PoseTemplate(bpy.types.IDPropertyGroup):
33
def metarig_templates():
35
return rigify.get_submodule_types()
38
class DATA_PT_template(bpy.types.Panel):
39
bl_label = "Meta-Rig Templates"
40
bl_space_type = 'PROPERTIES'
41
bl_region_type = 'WINDOW'
43
bl_default_closed = True
47
def poll(self, context):
48
if not context.armature:
52
return (obj.mode in ('POSE', 'OBJECT'))
55
def draw(self, context):
58
active_type = context.active_pose_bone["type"]
63
pose_templates = scene.pose_templates
65
if pose_templates.active_template_index == -1:
66
pose_templates.active_template_index = 0
68
if not self.templates:
69
self.templates[:] = metarig_templates()
71
while(len(pose_templates.templates) < len(self.templates)):
72
pose_templates.templates.add()
73
while(len(pose_templates.templates) > len(self.templates)):
74
pose_templates.templates.remove(0)
76
for i, template_name in enumerate(self.templates):
77
template = pose_templates.templates[i]
78
if active_type == template_name:
79
template.name = "<%s>" % template_name.replace("_", " ")
81
template.name = " %s " % template_name.replace("_", " ")
84
row.operator("pose.metarig_generate", text="Generate")
85
row.operator("pose.metarig_validate", text="Check")
86
row.operator("pose.metarig_graph", text="Graph")
88
row.prop(pose_templates, "generate_def_rig")
92
col.template_list(pose_templates, "templates", pose_templates, "active_template_index", rows=1)
94
subrow = col.split(percentage=0.5, align=True)
95
subsubrow = subrow.row(align=True)
96
subsubrow.operator("pose.metarig_assign", text="Assign")
97
subsubrow.operator("pose.metarig_clear", text="Clear")
99
subsubrow = subrow.split(percentage=0.8)
100
subsubrow.operator("pose.metarig_sample_add", text="Sample").metarig_type = self.templates[pose_templates.active_template_index]
101
subsubrow.operator("pose.metarig_sample_add", text="All").metarig_type = "" # self.templates[pose_templates.active_template_index]
103
sub = row.column(align=True)
104
sub.operator("pose.metarig_reload", icon="FILE_REFRESH", text="")
108
from bpy.props import StringProperty
111
class Reload(bpy.types.Operator):
112
'''Re-Scan the metarig package directory for scripts'''
114
bl_idname = "pose.metarig_reload"
115
bl_label = "Re-Scan the list of metarig types"
117
def execute(self, context):
118
DATA_PT_template.templates[:] = metarig_templates()
122
def rigify_report_exception(operator, exception):
126
# find the module name where the error happened
127
# hint, this is the metarig type!
128
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
129
fn = traceback.extract_tb(exceptionTraceback)[-1][0]
130
fn = os.path.basename(fn)
131
fn = os.path.splitext(fn)[0]
133
if fn.startswith("__"):
134
message.append("Incorrect armature...")
136
message.append("Incorrect armature for type '%s'" % fn)
137
message.append(exception.message)
139
message.reverse() # XXX - stupid! menu's are upside down!
141
operator.report(set(['INFO']), '\n'.join(message))
144
class Generate(bpy.types.Operator):
145
'''Generates a metarig from the active armature'''
147
bl_idname = "pose.metarig_generate"
148
bl_label = "Generate Metarig"
150
def execute(self, context):
154
meta_def = context.scene.pose_templates.generate_def_rig
157
rigify.generate_rig(context, context.object, META_DEF=meta_def)
158
except rigify.RigifyError as rig_exception:
159
rigify_report_exception(self, rig_exception)
164
class Validate(bpy.types.Operator):
165
'''Validate a metarig from the active armature'''
167
bl_idname = "pose.metarig_validate"
168
bl_label = "Validate Metarig"
170
def execute(self, context):
174
rigify.validate_rig(context, context.object)
175
except rigify.RigifyError as rig_exception:
176
rigify_report_exception(self, rig_exception)
180
class Sample(bpy.types.Operator):
181
'''Create a sample metarig to be modified before generating the final rig.'''
183
bl_idname = "pose.metarig_sample_add"
184
bl_label = "Re-Scan Metarig Scripts"
186
metarig_type = StringProperty(name="Type", description="Name of the rig type to generate a sample of, a blank string for all", maxlen=128, default="")
188
def execute(self, context):
191
final = (self.properties.metarig_type == "")
192
objects = rigify.generate_test(context, metarig_type=self.properties.metarig_type, GENERATE_FINAL=final)
195
for i, (obj_meta, obj_gen) in enumerate(objects):
196
obj_meta.location.x = i * 1.0
198
obj_gen.location.x = i * 1.0
203
class Graph(bpy.types.Operator):
204
'''Create a graph from the active armature through graphviz'''
206
bl_idname = "pose.metarig_graph"
207
bl_label = "Pose Graph"
209
def execute(self, context):
211
import graphviz_export
213
reload(graphviz_export)
214
obj = bpy.context.object
215
path = os.path.splitext(bpy.data.filepath)[0] + "-" + bpy.utils.clean_name(obj.name)
216
path_dot = path + ".dot"
217
path_png = path + ".png"
218
saved = graphviz_export.graph_armature(bpy.context.object, path_dot, CONSTRAINTS=False, DRIVERS=False)
221
# if we seriously want this working everywhere we'll need some new approach
222
os.system("dot -Tpng %s > %s; gnome-open %s &" % (path_dot, path_png, path_png))
223
#os.system("python /b/xdot.py '%s' &" % path_dot)
228
class AsScript(bpy.types.Operator):
229
'''Write the edit armature out as a python script'''
231
bl_idname = "pose.metarig_to_script"
232
bl_label = "Write Metarig to Script"
233
bl_options = {'REGISTER', 'UNDO'}
235
filepath = StringProperty(name="File Path", description="File path used for exporting the Armature file", maxlen=1024, default="")
237
def execute(self, context):
241
code = rigify_utils.write_meta_rig(obj)
242
path = self.properties.filepath
243
file = open(path, "w")
249
def invoke(self, context, event):
252
self.properties.filepath = os.path.splitext(bpy.data.filepath)[0] + "-" + bpy.utils.clean_name(obj.name) + ".py"
254
wm.add_fileselect(self)
255
return {'RUNNING_MODAL'}
258
# operators that use the GUI
259
class ActiveAssign(bpy.types.Operator):
260
'''Assign to the active posebone'''
262
bl_idname = "pose.metarig_assign"
263
bl_label = "Assign to the active posebone"
265
def poll(self, context):
266
bone = context.active_pose_bone
267
return bool(bone and bone.id_data.mode == 'POSE')
269
def execute(self, context):
270
scene = context.scene
271
pose_templates = scene.pose_templates
272
template_name = DATA_PT_template.templates[pose_templates.active_template_index]
273
context.active_pose_bone["type"] = template_name
277
class ActiveClear(bpy.types.Operator):
278
'''Clear type from the active posebone'''
280
bl_idname = "pose.metarig_clear"
281
bl_label = "Metarig Clear Type"
283
def poll(self, context):
284
bone = context.active_pose_bone
285
return bool(bone and bone.id_data.mode == 'POSE')
287
def execute(self, context):
288
scene = context.scene
290
del context.active_pose_bone["type"]
296
class INFO_MT_armature_metarig_add(bpy.types.Menu):
297
bl_idname = "INFO_MT_armature_metarig_add"
298
bl_label = "Meta-Rig"
300
def draw(self, context):
304
layout.operator_context = 'INVOKE_REGION_WIN'
306
for submodule_type in rigify.get_submodule_types():
307
text = bpy.utils.display_name(submodule_type)
308
layout.operator("pose.metarig_sample_add", text=text, icon='OUTLINER_OB_ARMATURE').metarig_type = submodule_type
313
PoseTemplateSettings,
326
INFO_MT_armature_metarig_add]
328
menu_func = (lambda self, context: self.layout.menu("INFO_MT_armature_metarig_add", icon='OUTLINER_OB_ARMATURE'))
329
import space_info # ensure the menu is loaded first
333
register = bpy.types.register
337
PoseTemplate.StringProperty(attr="name",
338
name="Name of the slave",
344
PoseTemplateSettings.CollectionProperty(attr="templates", type=PoseTemplate, name="Templates", description="")
345
PoseTemplateSettings.IntProperty(attr="active_template_index",
346
name="Index of the active slave",
352
PoseTemplateSettings.BoolProperty(attr="generate_def_rig",
353
name="Create Deform Rig",
354
description="Create a copy of the metarig, constrainted by the generated rig",
357
bpy.types.Scene.PointerProperty(attr="pose_templates", type=PoseTemplateSettings, name="Pose Templates", description="Pose Template Settings")
359
space_info.INFO_MT_armature_add.append(menu_func)
363
unregister = bpy.types.unregister
367
bpy.types.INFO_MT_armature_add.remove(menu_func)
369
if __name__ == "__main__":