~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to release/scripts/ui/properties_data_armature_rigify.py

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# ##### BEGIN GPL LICENSE BLOCK #####
2
 
#
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.
7
 
#
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.
12
 
#
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.
16
 
#
17
 
# ##### END GPL LICENSE BLOCK #####
18
 
 
19
 
# <pep8 compliant>
20
 
import bpy
21
 
 
22
 
narrowui = bpy.context.user_preferences.view.properties_width_check
23
 
 
24
 
 
25
 
class PoseTemplateSettings(bpy.types.IDPropertyGroup):
26
 
    pass
27
 
 
28
 
 
29
 
class PoseTemplate(bpy.types.IDPropertyGroup):
30
 
    pass
31
 
 
32
 
 
33
 
def metarig_templates():
34
 
    import rigify
35
 
    return rigify.get_submodule_types()
36
 
 
37
 
 
38
 
class DATA_PT_template(bpy.types.Panel):
39
 
    bl_label = "Meta-Rig Templates"
40
 
    bl_space_type = 'PROPERTIES'
41
 
    bl_region_type = 'WINDOW'
42
 
    bl_context = "data"
43
 
    bl_default_closed = True
44
 
 
45
 
    templates = []
46
 
 
47
 
    def poll(self, context):
48
 
        if not context.armature:
49
 
            return False
50
 
        obj = context.object
51
 
        if obj:
52
 
            return (obj.mode in ('POSE', 'OBJECT'))
53
 
        return False
54
 
 
55
 
    def draw(self, context):
56
 
        layout = self.layout
57
 
        try:
58
 
            active_type = context.active_pose_bone["type"]
59
 
        except:
60
 
            active_type = None
61
 
 
62
 
        scene = context.scene
63
 
        pose_templates = scene.pose_templates
64
 
 
65
 
        if pose_templates.active_template_index == -1:
66
 
            pose_templates.active_template_index = 0
67
 
 
68
 
        if not self.templates:
69
 
            self.templates[:] = metarig_templates()
70
 
 
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)
75
 
 
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("_", " ")
80
 
            else:
81
 
                template.name = " %s " % template_name.replace("_", " ")
82
 
 
83
 
        row = layout.row()
84
 
        row.operator("pose.metarig_generate", text="Generate")
85
 
        row.operator("pose.metarig_validate", text="Check")
86
 
        row.operator("pose.metarig_graph", text="Graph")
87
 
        row = layout.row()
88
 
        row.prop(pose_templates, "generate_def_rig")
89
 
 
90
 
        row = layout.row()
91
 
        col = row.column()
92
 
        col.template_list(pose_templates, "templates", pose_templates, "active_template_index", rows=1)
93
 
 
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")
98
 
 
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]
102
 
 
103
 
        sub = row.column(align=True)
104
 
        sub.operator("pose.metarig_reload", icon="FILE_REFRESH", text="")
105
 
 
106
 
 
107
 
# operators
108
 
from bpy.props import StringProperty
109
 
 
110
 
 
111
 
class Reload(bpy.types.Operator):
112
 
    '''Re-Scan the metarig package directory for scripts'''
113
 
 
114
 
    bl_idname = "pose.metarig_reload"
115
 
    bl_label = "Re-Scan the list of metarig types"
116
 
 
117
 
    def execute(self, context):
118
 
        DATA_PT_template.templates[:] = metarig_templates()
119
 
        return {'FINISHED'}
120
 
 
121
 
 
122
 
def rigify_report_exception(operator, exception):
123
 
    import traceback
124
 
    import sys
125
 
    import os
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]
132
 
    message = []
133
 
    if fn.startswith("__"):
134
 
        message.append("Incorrect armature...")
135
 
    else:
136
 
        message.append("Incorrect armature for type '%s'" % fn)
137
 
    message.append(exception.message)
138
 
 
139
 
    message.reverse() # XXX - stupid! menu's are upside down!
140
 
 
141
 
    operator.report(set(['INFO']), '\n'.join(message))
142
 
 
143
 
 
144
 
class Generate(bpy.types.Operator):
145
 
    '''Generates a metarig from the active armature'''
146
 
 
147
 
    bl_idname = "pose.metarig_generate"
148
 
    bl_label = "Generate Metarig"
149
 
 
150
 
    def execute(self, context):
151
 
        import rigify
152
 
        reload(rigify)
153
 
 
154
 
        meta_def = context.scene.pose_templates.generate_def_rig
155
 
 
156
 
        try:
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)
160
 
 
161
 
        return {'FINISHED'}
162
 
 
163
 
 
164
 
class Validate(bpy.types.Operator):
165
 
    '''Validate a metarig from the active armature'''
166
 
 
167
 
    bl_idname = "pose.metarig_validate"
168
 
    bl_label = "Validate Metarig"
169
 
 
170
 
    def execute(self, context):
171
 
        import rigify
172
 
        reload(rigify)
173
 
        try:
174
 
            rigify.validate_rig(context, context.object)
175
 
        except rigify.RigifyError as rig_exception:
176
 
            rigify_report_exception(self, rig_exception)
177
 
        return {'FINISHED'}
178
 
 
179
 
 
180
 
class Sample(bpy.types.Operator):
181
 
    '''Create a sample metarig to be modified before generating the final rig.'''
182
 
 
183
 
    bl_idname = "pose.metarig_sample_add"
184
 
    bl_label = "Re-Scan Metarig Scripts"
185
 
 
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="")
187
 
 
188
 
    def execute(self, context):
189
 
        import rigify
190
 
        reload(rigify)
191
 
        final = (self.properties.metarig_type == "")
192
 
        objects = rigify.generate_test(context, metarig_type=self.properties.metarig_type, GENERATE_FINAL=final)
193
 
 
194
 
        if len(objects) > 1:
195
 
            for i, (obj_meta, obj_gen) in enumerate(objects):
196
 
                obj_meta.location.x = i * 1.0
197
 
                if obj_gen:
198
 
                    obj_gen.location.x = i * 1.0
199
 
 
200
 
        return {'FINISHED'}
201
 
 
202
 
 
203
 
class Graph(bpy.types.Operator):
204
 
    '''Create a graph from the active armature through graphviz'''
205
 
 
206
 
    bl_idname = "pose.metarig_graph"
207
 
    bl_label = "Pose Graph"
208
 
 
209
 
    def execute(self, context):
210
 
        import os
211
 
        import graphviz_export
212
 
        import bpy
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)
219
 
 
220
 
        if saved:
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)
224
 
 
225
 
        return {'FINISHED'}
226
 
 
227
 
 
228
 
class AsScript(bpy.types.Operator):
229
 
    '''Write the edit armature out as a python script'''
230
 
 
231
 
    bl_idname = "pose.metarig_to_script"
232
 
    bl_label = "Write Metarig to Script"
233
 
    bl_options = {'REGISTER', 'UNDO'}
234
 
 
235
 
    filepath = StringProperty(name="File Path", description="File path used for exporting the Armature file", maxlen=1024, default="")
236
 
 
237
 
    def execute(self, context):
238
 
        import rigify_utils
239
 
        reload(rigify_utils)
240
 
        obj = context.object
241
 
        code = rigify_utils.write_meta_rig(obj)
242
 
        path = self.properties.filepath
243
 
        file = open(path, "w")
244
 
        file.write(code)
245
 
        file.close()
246
 
 
247
 
        return {'FINISHED'}
248
 
 
249
 
    def invoke(self, context, event):
250
 
        import os
251
 
        obj = context.object
252
 
        self.properties.filepath = os.path.splitext(bpy.data.filepath)[0] + "-" + bpy.utils.clean_name(obj.name) + ".py"
253
 
        wm = context.manager
254
 
        wm.add_fileselect(self)
255
 
        return {'RUNNING_MODAL'}
256
 
 
257
 
 
258
 
# operators that use the GUI
259
 
class ActiveAssign(bpy.types.Operator):
260
 
    '''Assign to the active posebone'''
261
 
 
262
 
    bl_idname = "pose.metarig_assign"
263
 
    bl_label = "Assign to the active posebone"
264
 
 
265
 
    def poll(self, context):
266
 
        bone = context.active_pose_bone
267
 
        return bool(bone and bone.id_data.mode == 'POSE')
268
 
 
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
274
 
        return {'FINISHED'}
275
 
 
276
 
 
277
 
class ActiveClear(bpy.types.Operator):
278
 
    '''Clear type from the active posebone'''
279
 
 
280
 
    bl_idname = "pose.metarig_clear"
281
 
    bl_label = "Metarig Clear Type"
282
 
 
283
 
    def poll(self, context):
284
 
        bone = context.active_pose_bone
285
 
        return bool(bone and bone.id_data.mode == 'POSE')
286
 
 
287
 
    def execute(self, context):
288
 
        scene = context.scene
289
 
        try:
290
 
            del context.active_pose_bone["type"]
291
 
        except:
292
 
            return {'CANCELLED'}
293
 
        return {'FINISHED'}
294
 
 
295
 
 
296
 
class INFO_MT_armature_metarig_add(bpy.types.Menu):
297
 
    bl_idname = "INFO_MT_armature_metarig_add"
298
 
    bl_label = "Meta-Rig"
299
 
 
300
 
    def draw(self, context):
301
 
        import rigify
302
 
 
303
 
        layout = self.layout
304
 
        layout.operator_context = 'INVOKE_REGION_WIN'
305
 
 
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
309
 
 
310
 
classes = [
311
 
    DATA_PT_template,
312
 
 
313
 
    PoseTemplateSettings,
314
 
    PoseTemplate,
315
 
 
316
 
    Reload,
317
 
    Generate,
318
 
    Validate,
319
 
    Sample,
320
 
    Graph,
321
 
    AsScript,
322
 
 
323
 
    ActiveAssign,
324
 
    ActiveClear,
325
 
 
326
 
    INFO_MT_armature_metarig_add]
327
 
 
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
330
 
 
331
 
 
332
 
def register():
333
 
    register = bpy.types.register
334
 
    for cls in classes:
335
 
        register(cls)
336
 
 
337
 
    PoseTemplate.StringProperty(attr="name",
338
 
                    name="Name of the slave",
339
 
                    description="",
340
 
                    maxlen=64,
341
 
                    default="")
342
 
 
343
 
 
344
 
    PoseTemplateSettings.CollectionProperty(attr="templates", type=PoseTemplate, name="Templates", description="")
345
 
    PoseTemplateSettings.IntProperty(attr="active_template_index",
346
 
                    name="Index of the active slave",
347
 
                    description="",
348
 
                    default=-1,
349
 
                    min=-1,
350
 
                    max=65535)
351
 
 
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",
355
 
                    default=False)
356
 
 
357
 
    bpy.types.Scene.PointerProperty(attr="pose_templates", type=PoseTemplateSettings, name="Pose Templates", description="Pose Template Settings")
358
 
 
359
 
    space_info.INFO_MT_armature_add.append(menu_func)
360
 
 
361
 
 
362
 
def unregister():
363
 
    unregister = bpy.types.unregister
364
 
    for cls in classes:
365
 
        unregister(cls)
366
 
 
367
 
    bpy.types.INFO_MT_armature_add.remove(menu_func)
368
 
 
369
 
if __name__ == "__main__":
370
 
    register()