0
$\begingroup$

I try and not undertand if i am on the right way, that code i want to set the select
faces, make a vertex group, this is a piece of code:

    ob = bpy.context.object
    me = ob.data
    bm = bmesh.from_edit_mesh(me)


    vertex = [v for v in bm.verts if (v.select==True and not v.hide)]       
    ver_grp = ob.vertex_groups.new(name = "My vertex group")

    ver_grp.add(???,0,'Replace')###not sure if replace can make ex novo
$\endgroup$
1
$\begingroup$

You are on the way

Let's talk about the vertex list line first

You can use v.index for list: vertex = [v.index for v in bm.verts if (v.select==True and not v.hide)], so the vertex will be a list contains your vertices indices.

Next, vertex_group.add() method:

It need 3 arguments (index, weight, type):

  • index: should be a list contain indices
  • weight: should be a float value in 0.0~1.0
  • type: should be a string in {'REPLACE', 'ADD', 'SUBTRACT'}

So you can use ver_grp.add(vertex,1.0,'REPLACE') for it

Sum up your code:

ob = bpy.context.object
me = ob.data
bpy.ops.object.mode_set(mode='EDIT') #You need to enter edit mode for bmesh
bm = bmesh.from_edit_mesh(me)

# not a good practice to use vertex for vertices indices list
v_indices = [v.index for v in bm.verts if (v.select==True and not v.hide)]      
ver_grp = ob.vertex_groups.new(name = "My vertex group")

bpy.ops.object.mode_set(mode='OBJECT') #You need to enter object mode for add()
# Add . in float number help people know it is a float instead of int
ver_grp.add(v_indices, 1.0, 'REPLACE') # most of the blender string enums use upper case

$\endgroup$
  • $\begingroup$ Ty , work , in my script this is performed by a button that appears only in edit mode, is there a way not to exit edit mode for apply ? $\endgroup$ – Pastrokkio May 21 at 22:39
  • 1
    $\begingroup$ @Andrea I don't know, I always set the mode. You can swap back your mode by bpy.ops.object.mode_set(mode='EDIT') or bpy.ops.object.editmode_toggle() in the end of this script. Operator should stay in place while the @poll() still work $\endgroup$ – Hikariztw May 22 at 2:18

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.