1
$\begingroup$

I tried to get a vertex belongs to a vertex group like below. But nothing is returned.

import bpy

obj = boy.data.objects['Cube']
obj.select_set(True)
obj.vertex_groups.new(name='Group')

vertexgroup = obj.vertex_group.get('Group')
for v in obj.data.vertices:
    for g in v.groups:
         if g.group == vertexgroup.index:
             print(v)

to check if it's working I called print(), but nothing happened.What is wrong?? can anyone help me??

$\endgroup$
1
$\begingroup$

No verts are assigned to the group

The trivial error in question code (bpy not boy) aside, you have not assigned any vertices to the new group.

Test script, assigns verts indices 0, 1 and 4 to the new vertex group of object named "Cube" if it exists.

import bpy

obj = bpy.data.objects.get("Cube")

if obj:
    vg = obj.vertex_groups.new(name='Group')
    # need to assign some verts to group
    vg.add([0, 1, 4], 1.0, 'REPLACE')
    for v in obj.data.vertices:
        for g in v.groups:
             if g.group == vg.index:
                 print(v.index)

If you already have a vg named "Group" and wish to use it, or create if non existent

vg = (obj.vertex_groups.get('Group')
     or obj.vertex_groups.new(name='Group'))

Feel it is important to note that question code

obj.vertex_groups.new(name='Group')
vertexgroup = obj.vertex_group.get('Group')

will work "as expected" only when there is no prior vg named "Group". Other wise the newly added group will be named "Group.001", "Group.002" etc etc, yet you are only ever getting the one originally named "Group".

$\endgroup$
1
  • $\begingroup$ Thank you!! I got it!! $\endgroup$ – ktro2828 Aug 25 '20 at 18:04

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.