0
$\begingroup$

I have a script that selects some random faces and splits them. I would like to add the selection to a vertex group. I can't get my script to assign the selection to a vertex group. Thank you for the help.

            sub2 = 0
            bpy.ops.mesh.select_all(action='SELECT')
            bpy.ops.mesh.select_random(percent=55, seed=10, action='DESELECT')
            bpy.ops.mesh.split()    
            group = context.object.vertex_groups.new()
            group.name = ("Island" + str(sub2))
            #group.add(bpy.data.objects.data.verts, 1.0, 'ADD')
            bpy.ops.object.vertex_group_assign()
$\endgroup$

1 Answer 1

3
$\begingroup$

You have missing "bpy" on line 4, so it should be:

group = bpy.context.object.vertex_groups.new()

Don't forget to import bpy:

import bpy

Whole script:

import bpy
sub2 = 0
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.select_random(percent=55, seed=10, action='DESELECT')
bpy.ops.mesh.split()    
group = bpy.context.object.vertex_groups.new()
group.name = ("Island" + str(sub2))
#group.add(bpy.data.objects.data.verts, 1.0, 'ADD')
bpy.ops.object.vertex_group_assign()
$\endgroup$
2
  • $\begingroup$ Oh man, something so simple. It was throwing me off because it would still create the group without the bpy if front of context. Thank you very much for your help! $\endgroup$
    – admbro
    Commented Oct 17, 2017 at 15:27
  • 1
    $\begingroup$ Use Window -> Toggle System Console to see the actual error messages and the line number where your script fails. In this case, you would get "context is incorrect" error. $\endgroup$ Commented Oct 17, 2017 at 16:59

You must log in to answer this question.

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