20
$\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$
0

1 Answer 1

35
$\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$
12
  • 1
    $\begingroup$ bpy.ops are relatively slow; the scene is updated every time. This solution is not optimal, although it does work. $\endgroup$
    – Jake Dube
    Commented Mar 9, 2017 at 13:17
  • 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$
    – Jake Dube
    Commented Mar 9, 2017 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$
    – Jake Dube
    Commented Mar 9, 2017 at 22:57
  • 1
    $\begingroup$ Great idea, done. $\endgroup$
    – TLousky
    Commented Mar 9, 2017 at 22:58
  • 1
    $\begingroup$ @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. $\endgroup$
    – TLousky
    Commented Dec 6, 2020 at 8:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .