# Assign variable selecting vertex group in blender

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?

## 1 Answer

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")

• 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 ]. Aug 9, 2017 at 21:58
• 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. Aug 10, 2017 at 14:17
• 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 :) Aug 10, 2017 at 14:47
• 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? Aug 10, 2017 at 15:00
• 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. Aug 10, 2017 at 15:31