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.