How to create a Vertex Group and add vertices to it with the Blender Python API

We can access vertex groups for the active mesh through its “vertex_groups” structure.

To create a new vertex group, we need to execute the following:

specifying the desired name for the group in the “name” parameter.

A vertex group stores a list of the vertex indices that are included in that group.

To add the required vertices to the group, we need to execute the following command:

Where the “vertex_group_data” is a list of vertex indices to include in the group.

The second parameter is the default weight assigned to a vertex in this group.

The third parameter can have three options:

  • ‘ADD’ – points are simply added to the group
  • ‘SUBTRACT’ – subtraction mode. If the vertex has already been added to the group earlier, it will be removed from it.
  • ‘REPLACE’ – replacement mode. If the vertex has already been added to the group earlier, the default weight for it will be replaced. The vertex itself will remain in the group.

The final code:

A new vertex group will be created for the active mesh and vertices with the indices from the list will be added to it.

Nikita

Author: Nikita

Blender add-ons developer and articles writer.

0 0 votes
Article Rating
Subscribe
Notify of
guest
3 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Pedro Antonio Reca Perales
Pedro Antonio Reca Perales
1 month ago

I have a problem that the vertices that i select doesnt appear in my group of vertex i dont understand..

Pedro Antonio Reca Perales
Pedro Antonio Reca Perales
1 month ago

i have this vertices:

and i put this code:
bufanda = bpy.context.active_object
new_vertex_group = bufanda.vertex_groups.new(name=’Peloo’)
vertex_group_data = [373,131,130,347,346,212,58,204,59,126,85,123,124,128,127,206,205,209,54,342,13,228,51,339,340,230,229,344,343,221,222,52,225,56,213,55]
new_vertex_group.add(vertex_group_data, 1.0, ‘ADD’)

bufanda.particle_systems[“hair”].vertex_group_density = “Peloo”

and my group of vertex appear with nothing

NikitaD
Admin
1 month ago

I don’t see the vertices indices from your vertex_group_data on the screenshot. Are you sure that you are adding valid vertices to the group?