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
GROUP_NAME="Group"
obj=bpy.context.active_object
data=obj.data
verts=data.vertices
for vert in verts:
i=vert.index
try:
obj.vertex_groups[GROUP_NAME].weight(i)
#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
pass
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.