I'm trying to check whether or not a vertex belongs to a particular group, I've seen a couple methods, I'm wondering why the following does not move any vertices.

for v in ob.data.vertices:
  if v.groups.find("Group") >= 0:
     v.co[0]=5 + offset

Just to be clear, There is a vertex group with the given name with vertices assigned, but nothing is moved. Am I using the find method wrong? Or totally misunderstanding what it's supposed to do?


What you basically want is something like this:

import bpy
ob = bpy.data.objects["Cube"]
gi = ob.vertex_groups["Group"].index # get group index
for v in ob.data.vertices:
  for g in v.groups:
    if g.group == gi: # compare with index in VertexGroupElement
      v.co[0] = 5

v.groups is a collection of VertexGroupElements. VertexGroupElement contains group (as index) and weight (as float [0,1]).

find can not find anything, because v.groups does not contain any keys.


There is a quick yet counter-intuitive way to check whether a vertex with a certain index belongs to a group or not. Whenever you call your_object.vertex_groups[GROUP_NAME].weight(i) to check the weight of a vertex, if that vertex is not present in the mentioned group, this method raises a RuntimeError. You can use it. Here's the code:

import bpy



for vert in verts:
        #if we get past this line, then the vertex is present in the group
        vert.co.x = 5 + offset # or whatever value you want            
    except RuntimeError:
       # vertex is not in the group

Basically, you iterate over all the vertices in your mesh, trying to get their weights (but here we don't save them to a variable, because for your particular task you don't need the values of weights). If a vertex is present in your group, the weight() returns its weight value, which is discarded immediately, and then it moves to the next line where it translates the vertex along the x axis. But, if a vertex is not present in the group, it raises RuntimeError exception, thus skipping the vertex translation part. pass means "do nothing", so it simply moves along to the next vertex.

  • $\begingroup$ Nice ad-hoc solution with try / except $\endgroup$ – okk Jan 26 at 9:33

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.