6
$\begingroup$

In my mesh I have few vertices which are in more then 4 vertex groups. I know the indices of those vertices, is there a way to get names of those vertex groups? Many tnx

$\endgroup$

2 Answers 2

5
$\begingroup$

You can use bmesh module and vertex group operators to get the names of the vertex groups:

import bpy
import bmesh

# get active mesh
obj = bpy.context.object
me = obj.data

# get bMesh representation
bm = bmesh.from_edit_mesh(me)

# set your vertex index value
v_index = 0 

# iterate through the vertex group 
for group in obj.vertex_groups:

    # set current vertex group to active
    bpy.ops.object.vertex_group_set_active(group=str(group.name))

    # select the active vertex group
    bpy.ops.object.vertex_group_select()

    # get all currently selected verts
    vertices = [v for v in bm.verts if (v.select and not v.hide)]

    # vertex iteration
    for vertex in vertices:
        if vertex.index ==  v_index:
            print("Vertex ", v_index, " is in ", group.name)        

    # deselect the current vertex group             
    bpy.ops.object.vertex_group_deselect()

# show the updates in the viewport
bmesh.update_edit_mesh(me, True)

Maybe there's another way without selecting all the vertex groups, but it works so far.

$\endgroup$
1
  • $\begingroup$ I didnt get you without selecting it, the script is awesome tnx $\endgroup$ Commented Apr 8, 2015 at 15:42
13
$\begingroup$

There's no need for operators, nor the bmesh module:

import bpy

ob = bpy.context.object
assert ob is not None and ob.type == 'MESH', "active object invalid"

# ensure we got the latest assignments and weights
ob.update_from_editmode()
me = ob.data

# create vertex group lookup dictionary for names
vgroup_names = {vgroup.index: vgroup.name for vgroup in ob.vertex_groups}

# create dictionary of vertex group assignments per vertex
vgroups = {v.index: [vgroup_names[g.group] for g in v.groups] for v in me.vertices}

# test: print list of vertex group names vertex 0 is in
print(vgroups[0])
$\endgroup$

You must log in to answer this question.

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