Skip to main content
8 of 8
replaced http://stackoverflow.com/ with https://stackoverflow.com/

#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.

enter image description here

Another representation of it:

enter image description here

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

enter image description here

#Additional links

https://blenderartists.org/forum/showthread.php?304054-Getting-vertices-of-a-vertex-group-from-within-Python

Checking if a vertex belongs to a vertex group in python

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

How to assign vertex groups and bone weights through Blender's Python API?

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

TLousky
  • 16.1k
  • 1
  • 42
  • 73