[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [4292] trunk/py/scripts/addons/rigify: In rigify dev tools added a button to generate widget-creation code.

Campbell Barton ideasman42 at gmail.com
Mon Feb 18 09:41:25 CET 2013


When building a string, using +=isn't so efficient. (ok, realize this
isn't a bottleneck)

Suggest building a list, then at the end doing 'string = "".join(data)'
http://www.blender.org/documentation/blender_python_api_2_65_release/info_best_practice.html#writing-strings-to-a-file-python-general

Alternatively you could also use StringIO (a file like object to build a string)
http://docs.python.org/3.3/library/io.html?highlight=io#io.StringIO

Also noticed mesh.update() is called twice, probably not intended.

On Mon, Feb 18, 2013 at 12:35 AM, Nathan Vegdahl <cessen at cessen.com> wrote:
> Revision: 4292
>           http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=4292
> Author:   cessen
> Date:     2013-02-17 13:35:39 +0000 (Sun, 17 Feb 2013)
> Log Message:
> -----------
> In rigify dev tools added a button to generate widget-creation code.
>
> Modified Paths:
> --------------
>     trunk/py/scripts/addons/rigify/ui.py
>     trunk/py/scripts/addons/rigify/utils.py
>
> Modified: trunk/py/scripts/addons/rigify/ui.py
> ===================================================================
> --- trunk/py/scripts/addons/rigify/ui.py        2013-02-17 06:50:48 UTC (rev 4291)
> +++ trunk/py/scripts/addons/rigify/ui.py        2013-02-17 13:35:39 UTC (rev 4292)
> @@ -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 @@
>      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 @@
>          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 @@
>      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 @@
>      bpy.utils.unregister_class(Sample)
>      bpy.utils.unregister_class(EncodeMetarig)
>      bpy.utils.unregister_class(EncodeMetarigSample)
> +    bpy.utils.register_class(EncodeWidget)
>
> Modified: trunk/py/scripts/addons/rigify/utils.py
> ===================================================================
> --- trunk/py/scripts/addons/rigify/utils.py     2013-02-17 06:50:48 UTC (rev 4291)
> +++ trunk/py/scripts/addons/rigify/utils.py     2013-02-17 13:35:39 UTC (rev 4292)
> @@ -611,6 +611,50 @@
>      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.
>      """
>
> _______________________________________________
> Bf-extensions-cvs mailing list
> Bf-extensions-cvs at blender.org
> http://lists.blender.org/mailman/listinfo/bf-extensions-cvs



-- 
- Campbell


More information about the Bf-extensions-cvs mailing list