0
$\begingroup$

I have a mesh with particular vertex groups that form a circle around different sections of the mesh. I want to be able to call those vertex groups using a single variable name (radius 1, radius 2). I've been using bpy.ops.object.vertex_group_select() to select vertex group (but vertex group name must already be highlighted under "Vertex Groups" header. Is there a way to code a variable, that will select an individual Vertex Group when called?

$\endgroup$
1
$\begingroup$

This is a python script which will select all the vertices in a vertex group with weight>0.5

import bpy
import bmesh

def selvg(obj, name):
    bm = bmesh.from_edit_mesh(obj.data)
    for i in range(len(obj.data.vertices)):
        try:
            s = obj.vertex_groups[name].weight(i) >0.5
            s=True # or maybe you don't care what the weight is, and even weight 0 is acceptable
        except:
            s=False
        bm.verts[i].select = s
    bmesh.update_edit_mesh(obj.data)

selvg(bpy.context.active_object, "Group")
$\endgroup$
  • 1
    $\begingroup$ Nice one. Though I would suggest to replace the try-except clause with a condition obj.vertex_groups[name].index in [ g.group for g in obj.data.vertices[i].groups ]. $\endgroup$ – TLousky Aug 9 '17 at 21:58
  • $\begingroup$ That alteration might not be effective. When I print out [ v.groups[:] for v in obj.data.vertices ] I get [[], [], [], [], [], [], [], []] . Since vertex groups are per-object (not per-mesh) I am not surprised. $\endgroup$ – Mutant Bob Aug 10 '17 at 14:17
  • $\begingroup$ Yeah, verts that are not part of any group will have an empty list, though that should work to differentiate between those included in the vgroup of interest and those that are not. But hey, the try-catch solution seems fine, I'm just biased against using try-catch when it's not absolutely necessary :) $\endgroup$ – TLousky Aug 10 '17 at 14:47
  • $\begingroup$ Thank you for the help! I don't receive an error message when I run the script in edit mode, however it only seems to move the 3D cursor to the location of the vertex group, it isn't selecting all of the vertices within that group so I can create a variable out of it that can later be called for further operations. You'll have to forgive me for not fully understanding as I've only learned Python within the past week (the joys of getting my MSc), but would you mind explaining the code a bit more, maybe I'm missing something? $\endgroup$ – user33993 Aug 10 '17 at 15:00
  • $\begingroup$ Perhaps it is because some of the vertices in the group have a weight <=0.5 . I have modified the script with an s=True so you can try that variation. $\endgroup$ – Mutant Bob Aug 10 '17 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.