# Finding vertices in a Vertex Group using Blender's Python API

Is there an easy way to get the vertices in a vertex group? The only way I've found is pretty brute-force - e.g., to find the vertices of object obs[i] in group "e":

e_idx = obs[i].vertex_groups['e'].index
vs=[v for v in obs[i].data.vertices if (v.groups)]
for v in vs:
for g in v.groups:
if (g.group == e_idx):
es[i].append(v)
break


Isn't there an easier way? I just found almost identical code in the "Similar Questions", so maybe this is the best there is.

# Methods

### 1. List comprehension

As mentioned by Pycoder in the comments below, using operators is often slow. So a quicker and simpler (and X2 faster) method to get all the verts that belong to a certain vertex group index:

vg_idx = 0
o = bpy.context.object
vs = [ v for v in o.data.vertices if vg_idx in [ vg.group for vg in v.groups ] ]


### 2. bpy.ops

You can also use the bpy.ops.object.vertex_group_select operator in edit mode:

import bpy

o = bpy.context.object

bpy.ops.object.mode_set( mode = 'EDIT' )

# Set the first vertex group as active:
o.vertex_groups.active = o.vertex_groups[0]

# Deselect all verts and select only current VG
bpy.ops.mesh.select_all( action = 'DESELECT' )
bpy.ops.object.vertex_group_select()

# Now the selected vertices are the ones that belong to this VG
vgVerts = [ v for v in o.data.vertices if v.select ]


### 3. Optimization if only one group

If there is only one group, this is a more optimized way to get the verts that belong to it. Note that this only works if there is only one vertex group.

vertices = [v for v in obj.data.vertices if v.groups]


# Performance testing

### 1. List comprehension

With the default cube (8 vertices), this was timed 1,000,000 times, and had a minimum value of:

0.000003528 seconds

With the default cube subdivided 8 times (393,218 vertices), this was timed 1,000 times, and had a minimum value of:

0.1528 seconds

### 2. bpy.ops

With the default cube (8 vertices), this was timed 10,000 times, and had a minimum value of:

0.0003289 seconds

With the default cube subdivided 8 times (393,218 vertices), this was timed 100 times, and had a minimum value of:

0.8849 seconds

### 3. Optimization if only one group

With the default cube (8 vertices), this was timed 1,000,000 times, and had a minimum value of:

0.00000222 seconds

With the default cube subdivided 8 times (393,218 vertices), this was timed 1,000 times, and had a minimum value of:

0.0659 seconds

Here is a graphical (but less accurate) model if the performance. Each point is the min of 100 iterations.

Another representation of it:

Because the higher subdivision values make the lower end almost meaningless, we can use a logarithmic scale to see everything a bit more clearly:

Checking if a vertex belongs to a vertex group in python

How can i get the weight for all vertices in a vertex group?

vertex groups and bone weights programmatically

How can I get the names of the vertex groups these vertices are in?

• bpy.ops are relatively slow; the scene is updated every time. This solution is not optimal, although it does work. – JakeD Mar 9 '17 at 13:17
• Beautiful list comprehension example...I was going to post the same thing, but you beat me to it, so I added the optimized technique to your answer rather than make another answer. – JakeD Mar 9 '17 at 20:16
• I was just asking if you wanted to make it a community wiki...the comments were getting lengthy, so I made the chat... – JakeD Mar 9 '17 at 22:57
• Great idea, done. – TLousky Mar 9 '17 at 22:58
• @VSB you should ask this as a new question, if I had to guess I might try to use vertex groups for that, especially if you want to assign identical weight to all members, but you'll likely get plenty of cool solutions. – TLousky Dec 6 '20 at 8:45