[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [3199] contrib/py/scripts/addons/ mesh_show_vgroup_weights.py: Initial commit of the Show Vertex Groups/ Weights addon to contrib.

Jason van Gumster jason at handturkeystudios.com
Mon Apr 2 01:17:30 CEST 2012


Revision: 3199
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=3199
Author:   thefallenweeble
Date:     2012-04-01 23:17:20 +0000 (Sun, 01 Apr 2012)
Log Message:
-----------
Initial commit of the Show Vertex Groups/Weights addon to contrib.

Details on the script can be found on its wiki [1] and bf-extensions page [2].

[1] http://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Modeling/Show_Vertex_Group_Weights
[2] http://projects.blender.org/tracker/?func=detail&aid=30609&group_id=153&atid=467

Added Paths:
-----------
    contrib/py/scripts/addons/mesh_show_vgroup_weights.py

Added: contrib/py/scripts/addons/mesh_show_vgroup_weights.py
===================================================================
--- contrib/py/scripts/addons/mesh_show_vgroup_weights.py	                        (rev 0)
+++ contrib/py/scripts/addons/mesh_show_vgroup_weights.py	2012-04-01 23:17:20 UTC (rev 3199)
@@ -0,0 +1,468 @@
+# ***** BEGIN GPL LICENSE BLOCK *****
+#
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ***** END GPL LICENCE BLOCK *****
+
+# <pep8 compliant> (Thanks to CodemanX on IRC)
+
+bl_info = {
+    "name": "Show Vertex Groups/Weights",
+    "author": "Jason van Gumster (Fweeb), Bartius Crouch",
+    "version": (0, 7, 1),
+    "blender": (2, 62, 3),
+    "location": "3D View > Properties Region > Show Weights",
+    "description": "Finds the vertex groups of a selected vertex and displays the corresponding weights",
+    "warning": "Requires bmesh",
+    "wiki_url": "http://wiki.blender.org/index.php?title=Extensions:2.6/Py/Scripts/Modeling/Show_Vertex_Group_Weights",
+    "tracker_url": "http://projects.blender.org/tracker/index.php?func=detail&aid=30609&group_id=153&atid=467",
+    "category": "Mesh"}
+
+#TODO - Add button for selecting vertices with no groups
+
+
+import bpy, bmesh, bgl, blf, mathutils
+
+
+# Borrowed/Modified from Bart Crouch's old Index Visualiser add-on
+def calc_callback(self, context):
+    #polling
+    if context.mode != "EDIT_MESH" or len(context.active_object.vertex_groups) == 0:
+        return
+
+    # get color info from theme
+    acol = context.user_preferences.themes[0].view_3d.editmesh_active
+    tcol = (acol[0] * 0.85, acol[1] * 0.85, acol[2] * 0.85)
+    
+    # get screen information
+    mid_x = context.region.width / 2.0
+    mid_y = context.region.height / 2.0
+    width = context.region.width
+    height = context.region.height
+    
+    # get matrices
+    view_mat = context.space_data.region_3d.perspective_matrix
+    ob_mat = context.active_object.matrix_world
+    total_mat = view_mat * ob_mat
+    
+    # calculate location info
+    texts = []
+    locs = []
+    weights  = []
+    me = context.active_object.data
+    bm = bmesh.from_edit_mesh(me)
+    dvert_lay = bm.verts.layers.deform.active
+
+    for v in bm.verts:
+        if v.select: #XXX Should check v.hide here, but it doesn't work
+            if bm.select_mode == {'VERT'} and bm.select_history.active is not None and bm.select_history.active.index == v.index:
+                locs.append([acol[0], acol[1], acol[2], v.index, v.co.to_4d()])
+            else:
+                locs.append([tcol[0], tcol[1], tcol[2], v.index, v.co.to_4d()])
+            dvert = v[dvert_lay]
+            for vgroup in context.active_object.vertex_groups:
+                if vgroup.index in dvert.keys():
+                    weights += [v.index, vgroup.index, dvert[vgroup.index]]
+
+    for loc in locs:
+        vec = total_mat * loc[4] # order is important
+        # dehomogenise
+        vec = mathutils.Vector((vec[0] / vec[3], vec[1] / vec[3], vec[2] / vec[3]))
+        x = int(mid_x + vec[0] * width / 2.0)
+        y = int(mid_y + vec[1] * height / 2.0)
+        texts += [loc[0], loc[1], loc[2], loc[3], x, y, 0]
+
+    # store as ID property in mesh
+    context.active_object.data["show_vgroup_verts"] = texts
+    context.active_object.data["show_vgroup_weights"] = weights
+
+
+# draw in 3d-view
+def draw_callback(self, context):
+    # polling
+    if context.mode != "EDIT_MESH" or len(context.active_object.vertex_groups) == 0:
+        return
+    # retrieving ID property data
+    try:
+        texts = context.active_object.data["show_vgroup_verts"]
+        weights = context.active_object.data["show_vgroup_weights"]
+    except:
+        return
+    if not texts:
+        return
+
+    bm = bmesh.from_edit_mesh(context.active_object.data)
+
+    if bm.select_mode == {'VERT'} and bm.select_history.active is not None:
+        active_vert = bm.select_history.active
+    else:
+        active_vert = None
+
+    # draw
+    blf.size(0, 13, 72)
+    blf.enable(0, blf.SHADOW)
+    blf.shadow(0, 3, 0.0, 0.0, 0.0, 1.0)
+    blf.shadow_offset(0, 2, -2)
+    for i in range(0, len(texts), 7):
+        bgl.glColor3f(texts[i], texts[i+1], texts[i+2])
+        blf.position(0, texts[i+4], texts[i+5], texts[i+6])
+        blf.draw(0, "Vertex " + str(int(texts[i+3])) + ":")
+        font_y = texts[i+5]
+        group_name = ""
+        for j in range(0, len(weights), 3):
+            if int(weights[j]) == int(texts[i+3]):
+                font_y -= 13
+                blf.position(0, texts[i+4] + 10, font_y, texts[i+6])
+                for group in context.active_object.vertex_groups:
+                    if group.index == int(weights[j+1]):
+                        group_name = group.name
+                        break
+                blf.draw(0, group_name + ": %.3f" % weights[j+2])
+        if group_name == "":
+            font_y -= 13
+            blf.position(0, texts[i+4] + 10, font_y, texts[i+6])
+            blf.draw(0, "No Groups")
+
+    # restore defaults
+    blf.disable(0, blf.SHADOW)
+
+
+# operator
+class ShowVGroupWeights(bpy.types.Operator):
+    bl_idname = "view3d.show_vgroup_weights"
+    bl_label = "Show Vertex Group Weights"
+    bl_description = "Toggle the display of the vertex groups and weights for selected vertices"
+    
+    @classmethod
+    def poll(cls, context):
+        return context.mode == 'EDIT_MESH'
+    
+    def __del__(self):
+        bpy.context.scene.display_indices = -1
+        clear_properties(full=False)
+    
+    def modal(self, context, event):
+        if context.area:
+            context.area.tag_redraw()
+
+        # removal of callbacks when operator is called again
+        if context.scene.display_indices == -1:
+            context.region.callback_remove(self.handle1)
+            context.region.callback_remove(self.handle2)
+            context.scene.display_indices = 0
+            return {'CANCELLED'}
+        
+        return {'PASS_THROUGH'}
+    
+    def invoke(self, context, event):
+        if context.area.type == 'VIEW_3D':
+            if context.scene.display_indices < 1:
+                # operator is called for the first time, start everything
+                context.scene.display_indices = 1
+                context.window_manager.modal_handler_add(self)
+                self.handle1 = context.region.callback_add(calc_callback,
+                    (self, context), 'POST_VIEW')
+                self.handle2 = context.region.callback_add(draw_callback,
+                    (self, context), 'POST_PIXEL')
+                return {'RUNNING_MODAL'}
+            else:
+                # operator is called again, stop displaying
+                context.scene.display_indices = -1
+                clear_properties(full=False)
+                return {'RUNNING_MODAL'}
+        else:
+            self.report({"WARNING"}, "View3D not found, can't run operator")
+            return {'CANCELLED'}
+
+
+# properties used by the script
+class InitProperties(bpy.types.Operator):
+    bl_idname = "view3d.init_find_weights"
+    bl_label = "Initialize properties for vgroup weights finder"
+    
+    def execute(self, context):
+        bpy.types.Scene.display_indices = bpy.props.IntProperty(
+            name="Display indices",
+            default=0)
+        context.scene.display_indices = 0
+        return {'FINISHED'}
+
+
+# removal of ID-properties when script is disabled
+def clear_properties(full=True):
+    # can happen on reload
+    if bpy.context.scene is None:
+        return
+    
+    if "show_vgroup_verts" in bpy.context.active_object.data.keys():
+        del bpy.context.active_object.data["show_vgroup_verts"]
+    if "show_vgroup_weights" in bpy.context.active_object.data.keys():
+        del bpy.context.active_object.data["show_vgroup_weights"]
+    if full:
+        props = ["display_indices"]
+        for p in props:
+            if p in bpy.types.Scene.bl_rna.properties:
+                exec("del bpy.types.Scene." + p)
+            if p in bpy.context.scene.keys():
+                del bpy.context.scene[p]
+
+
+class AssignVertexWeight(bpy.types.Operator):
+    bl_idname = "mesh.vertex_group_assign"
+    bl_label = "Assign Weights"
+    bl_description = "Assign weights for all of the groups on a specific vertex"
+
+    vgroup_weights = bpy.props.StringProperty(name = "Vertex Group Weights")
+
+    @classmethod
+    def poll(cls, context):
+        return context.mode == 'EDIT_MESH'
+
+    def execute(self, context):
+        me = context.active_object.data
+        bm = bmesh.from_edit_mesh(me)
+        dvert_lay = bm.verts.layers.deform.active
+        weights = eval(self.vgroup_weights) #XXX Would be nice if I didn't have to use an eval
+
+        for v in bm.verts:
+            if v.index == weights["__index__"]:
+                del weights["__index__"]
+                dvert = v[dvert_lay]
+                for vgroup in dvert.keys():
+                    dvert[vgroup] = weights[vgroup]
+                break
+
+        return {'FINISHED'}
+
+
+class RemoveFromVertexGroup(bpy.types.Operator):
+    bl_idname = "mesh.vertex_group_remove"
+    bl_label = "Remove Vertex from Group"
+    bl_description = "Remove a specific vertex from a specific vertex group"
+
+    #XXX abusing vector props here a bit; the first element is the vert index and the second is the group index
+    vert_and_group = bpy.props.IntVectorProperty(name = "Vertex and Group to remove", size = 2)
+
+    @classmethod
+    def poll(cls, context):
+        return context.mode == 'EDIT_MESH'
+
+    def execute(self, context):
+        ob = context.active_object
+        me = ob.data
+        bm = bmesh.from_edit_mesh(me)
+
+        # Save current selection
+        selected_verts = []

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list