diff options
author | Nathan Vegdahl <cessen@cessen.com> | 2013-02-17 17:35:39 +0400 |
---|---|---|
committer | Nathan Vegdahl <cessen@cessen.com> | 2013-02-17 17:35:39 +0400 |
commit | 40f30e6de5a3267c033884e54c851bd37711c113 (patch) | |
tree | 1d11646d18a37bc89c76600aa0f32a97c5207612 /rigify | |
parent | 1227e12cc555d1830a6344e5f86d83464000beb3 (diff) |
In rigify dev tools added a button to generate widget-creation code.
Diffstat (limited to 'rigify')
-rw-r--r-- | rigify/ui.py | 52 | ||||
-rw-r--r-- | rigify/utils.py | 44 |
2 files changed, 86 insertions, 10 deletions
diff --git a/rigify/ui.py b/rigify/ui.py index c2e1e6c6..3ff46b67 100644 --- a/rigify/ui.py +++ b/rigify/ui.py @@ -21,7 +21,8 @@ import bpy from bpy.props import StringProperty -from .utils import get_rig_type, write_metarig, MetarigError +from .utils import get_rig_type, MetarigError +from .utils import write_metarig, write_widget from . import rig_lists from . import generate @@ -201,16 +202,18 @@ class VIEW3D_PT_tools_rigify_dev(bpy.types.Panel): bl_space_type = 'VIEW_3D' bl_region_type = 'TOOLS' - @classmethod - def poll(cls, context): - return (context.mode == 'EDIT_ARMATURE') - def draw(self, context): - r = self.layout.row() - r.operator("armature.rigify_encode_metarig", text="Encode Metarig to Python") - r = self.layout.row() - r.operator("armature.rigify_encode_metarig_sample", text="Encode Sample to Python") - + obj = context.active_object + if obj != None: + if context.mode == 'EDIT_ARMATURE': + r = self.layout.row() + r.operator("armature.rigify_encode_metarig", text="Encode Metarig to Python") + r = self.layout.row() + r.operator("armature.rigify_encode_metarig_sample", text="Encode Sample to Python") + + if context.mode == 'EDIT_MESH': + r = self.layout.row() + r.operator("mesh.rigify_encode_mesh_widget", text="Encode Mesh Widget to Python") #~ class INFO_MT_armature_metarig_add(bpy.types.Menu): #~ bl_idname = "INFO_MT_armature_metarig_add" @@ -374,6 +377,33 @@ class EncodeMetarigSample(bpy.types.Operator): return {'FINISHED'} +class EncodeWidget(bpy.types.Operator): + """ Creates Python code that will generate the selected metarig. + """ + bl_idname = "mesh.rigify_encode_mesh_widget" + bl_label = "Rigify Encode Widget" + bl_options = {'UNDO'} + + @classmethod + def poll(self, context): + return context.mode == 'EDIT_MESH' + + def execute(self, context): + name = "widget.py" + + if name in bpy.data.texts: + text_block = bpy.data.texts[name] + text_block.clear() + else: + text_block = bpy.data.texts.new(name) + + text = write_widget(context.active_object) + text_block.write(text) + bpy.ops.object.mode_set(mode='EDIT') + + return {'FINISHED'} + + #menu_func = (lambda self, context: self.layout.menu("INFO_MT_armature_metarig_add", icon='OUTLINER_OB_ARMATURE')) #from bl_ui import space_info # ensure the menu is loaded first @@ -388,6 +418,7 @@ def register(): bpy.utils.register_class(Sample) bpy.utils.register_class(EncodeMetarig) bpy.utils.register_class(EncodeMetarigSample) + bpy.utils.register_class(EncodeWidget) #space_info.INFO_MT_armature_add.append(ui.menu_func) @@ -402,3 +433,4 @@ def unregister(): bpy.utils.unregister_class(Sample) bpy.utils.unregister_class(EncodeMetarig) bpy.utils.unregister_class(EncodeMetarigSample) + bpy.utils.register_class(EncodeWidget) diff --git a/rigify/utils.py b/rigify/utils.py index 9a956689..ec4c9e76 100644 --- a/rigify/utils.py +++ b/rigify/utils.py @@ -611,6 +611,50 @@ def write_metarig(obj, layers=False, func_name="create"): return "\n".join(code) +def write_widget(obj): + """ Write a mesh object as a python script for widget use. + """ + script = "" + script += "def create_thing_widget(rig, bone_name, size=1.0, bone_transform_name=None):\n" + script += " obj = create_widget(rig, bone_name, bone_transform_name)\n" + script += " if obj != None:\n" + + # Vertices + if len(obj.data.vertices) > 0: + script += " verts = [" + for v in obj.data.vertices: + script += "(" + str(v.co[0]) + "*size, " + str(v.co[1]) + "*size, " + str(v.co[2]) + "*size), " + script += "]\n" + + # Edges + if len(obj.data.edges) > 0: + script += " edges = [" + for e in obj.data.edges: + script += "(" + str(e.vertices[0]) + ", " + str(e.vertices[1]) + "), " + script += "]\n" + + # Faces + if len(obj.data.polygons) > 0: + script += " faces = [" + for f in obj.data.polygons: + script += "(" + for v in f.vertices: + script += str(v) + ", " + script += "), " + script += "]\n" + + # Build mesh + script += "\n mesh = obj.data\n" + script += " mesh.from_pydata(verts, edges, faces)\n" + script += " mesh.update()\n" + script += " mesh.update()\n" + script += " return obj\n" + script += " else:\n" + script += " return None\n" + + return script + + def random_id(length=8): """ Generates a random alphanumeric id string. """ |