14
$\begingroup$

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.

$\endgroup$
25
$\begingroup$

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?

vertex groups and bone weights programmatically

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

$\endgroup$
  • 1
    $\begingroup$ bpy.ops are relatively slow; the scene is updated every time. This solution is not optimal, although it does work. $\endgroup$ – JakeD Mar 9 '17 at 13:17
  • $\begingroup$ @pycoder yeah that's true. However he did ask for a simple solution, and I'm not all sure ops are slower than low level iterations if you have a really high poly mesh. $\endgroup$ – TLousky Mar 9 '17 at 13:18
  • 1
    $\begingroup$ 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. $\endgroup$ – JakeD Mar 9 '17 at 20:16
  • 2
    $\begingroup$ I was just asking if you wanted to make it a community wiki...the comments were getting lengthy, so I made the chat... $\endgroup$ – JakeD Mar 9 '17 at 22:57
  • 1
    $\begingroup$ Great idea, done. $\endgroup$ – TLousky Mar 9 '17 at 22:58

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.