1
$\begingroup$

I'm writing a piece of code where I want to assign the selected grease pencil vertices to a vertex group. So far, with the vertices selected, this is what I have tried:

#create vertex group:
obj.vertex_groups.new(name = 'TEST')
#assign selected vertices to group:
bpy.ops.gpencil.vertex_group_assign()

However, this doesn't work. It creates the vertex group no problem, but doesn't assign anything to it. Any ideas?

$\endgroup$
2
  • $\begingroup$ Do you have vertices selected at that point? $\endgroup$
    – pevinkinel
    Oct 5, 2021 at 11:00
  • $\begingroup$ Yes, vertices are selected $\endgroup$
    – Lipizzaner
    Oct 6, 2021 at 0:39

1 Answer 1

1
$\begingroup$

Okay, so it turns out that you need to override the context for this operator.

#create vertex group:
obj.vertex_groups.new(name = 'TEST')

#assign selected vertices to group:
for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        for region in area.regions:
            if region.type == 'WINDOW':
                override = {'area': area, 'region': region, 'edit_object':bpy.context.edit_object}

bpy.ops.gpencil.vertex_group_assign(override)
$\endgroup$

You must log in to answer this question.

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