We can access vertex groups for the active mesh through its “vertex_groups” structure.
1 |
bpy.context.active_object.vertex_groups |
To create a new vertex group, we need to execute the following:
1 |
new_vertex_group = bpy.context.active_object.vertex_groups.new(name='_GROUP_NAME_') |
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:
1 2 |
vertex_group_data = [1, 3, 5, 7] new_vertex_group.add(vertex_group_data, 1.0, 'ADD') |
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:
1 2 3 4 5 |
import bpy new_vertex_group = bpy.context.active_object.vertex_groups.new(name='_GROUP_NAME_') vertex_group_data = [1, 3, 5, 7] new_vertex_group.add(vertex_group_data, 1.0, 'ADD') |
A new vertex group will be created for the active mesh and vertices with the indices from the list will be added to it.
I have a problem that the vertices that i select doesnt appear in my group of vertex i dont understand..
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
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?