I'm trying to run the below script. But for some reason I receive this error saying

Traceback (most recent call last):
File "<blender_console>", line 1, in <module>
File "Text", line 18, in <module>
File "Text", line 18, in <listcomp>
indexerror: bpy_prop_collection[index]: index 0 out of range, size 0

Below is the script I'm using and in the output it prints 1 only, so this means the error is in this statement labels = [ v.groups[0].group for v in mesh.vertices ]

So if anyone could please advise

import bpy

scn = bpy.context.scene
mesh_obj = scn.objects.active
mesh = mesh_obj.data

vertex_groups = mesh_obj.vertex_groups

labels = [  v.groups[0].group for v in mesh.vertices ]

1 Answer 1


Your assumption is that there is at least one group (groups[0]) that can be accessed, but it's not the case if the vertex isn't assigned to any group. It's independent of the existing vertex groups of the object.

If you want a list of all vertex group names, use this instead:

labels = [vgroup.name for vgroup in mesh_obj.vertex_groups]

For a list of all vertex groups the current vertex is part of, use:

# Create a lookup dictionary once
vgroup_mapping = {i: vgroup.name for i, vgroup in enumerate(mesh_obj.vertex_groups)}

# Look up the vgroup names for a certain vertex (could also iterate over all vertices here)
vert = mesh.vertices[0]
labels = [vgroup_mapping[group.group] for group in vert.groups]
  • $\begingroup$ How to search and select a certain vertex? I checked and this is because vertex number 26572 is not assigned to a vertex group. So I want to select vertex 26572 and assign it to an existing group. Can you advise me how to do this? $\endgroup$
    – Tak
    Aug 29, 2014 at 13:31
  • $\begingroup$ Not sure what you want, you don't need to change a vertex's .select attribute to assign it to a vertex group. Use mesh_obj.vertex_groups['Group Name'].add((vert_index,), 1.0, 'REPLACE') instead. And why do you want to search for vertex number 26572? Do you actually want to assign the active vertex of an editmesh? Or find vertices matching certain criteria (as in location)? $\endgroup$
    – CodeManX
    Aug 29, 2014 at 16:44
  • $\begingroup$ how to map or connect these vertex groups with the bones in the armature? and when I do ctrl+p (automatic weighting it creates different vertex groups with different names) $\endgroup$
    – Tak
    Aug 30, 2014 at 10:26
  • $\begingroup$ Simply name the vertex groups after the bone names, that's it. The rest is done by the Armature modifier. $\endgroup$
    – CodeManX
    Aug 31, 2014 at 8:31
  • $\begingroup$ Thanks a lot! could you check my question in this link blender.stackexchange.com/questions/15410/… as I don't want to change the vertexgroups names, I want them to be 26 as I already did in the blend file I attached in the question, but I don't know how, so I'd be very thankful if you could please have a look. As for example, you will see that I have four vertex groups for the head, but the armature has one bone. So if you could please have a look and let me know, I'd appreciate your advice $\endgroup$
    – Tak
    Aug 31, 2014 at 13:23

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.