Sub topic 14
Inspecting and Assigning Weights with Python
This chapter assumes you know Blender’s Object, Edit, and Pose modes, and that your mesh is already bound to an armature. It also assumes basic Python syntax. The examples use a mesh object named Body and an armature with a bone named Bone.
Weight data lives in vertex groups. Each group name must match a deforming bone name. The group stores a value from 0.0 to 1.0 for each assigned vertex. The Armature modifier reads these groups while it deforms the mesh.
Inspecting existing weights
Select the mesh and run this script in Blender’s Scripting workspace or the Python Console. It prints every nonzero group membership for every vertex.
import bpy
obj = bpy.data.objects["Body"]
if obj.type != "MESH":
raise TypeError("Body must be a mesh object")
for vertex in obj.data.vertices:
memberships = []
for membership in vertex.groups:
group = obj.vertex_groups[membership.group]
if membership.weight > 0.0:
memberships.append(
f"{group.name}: {membership.weight:.3f}"
)
if memberships:
print(f"Vertex {vertex.index}: " + ", ".join(memberships))A vertex can belong to several groups. For example, a vertex near an elbow might have weight 0.6 in UpperArm and 0.4 in Forearm. When the armature moves, both bones contribute to that vertex’s position.
The membership.group value is an internal group index. The script converts it to a readable name through obj.vertex_groups.
Creating a group and assigning weights
The following script creates a group if needed, then assigns a weight of 1.0 to a chosen set of vertices. The REPLACE mode overwrites the previous value in that group.
import bpy
obj = bpy.data.objects["Body"]
group_name = "Bone"
vertex_indices = [0, 1, 2, 3]
weight = 1.0
if obj.type != "MESH":
raise TypeError("Body must be a mesh object")
group = obj.vertex_groups.get(group_name)
if group is None:
group = obj.vertex_groups.new(name=group_name)
valid_indices = [
index for index in vertex_indices
if 0 <= index < len(obj.data.vertices)
]
group.add(valid_indices, weight, "REPLACE")
print(f"Assigned {weight} to {len(valid_indices)} vertices in {group_name}")The group name must match the armature bone exactly, including capitalization and suffixes such as .L or .R. Creating a group with an unrelated name does not make that group influence the mesh.
Normalizing weights with Python
Normalization makes the weights on a vertex add up to 1.0. This prevents unintended scaling or weak deformation when several bones influence the same vertex.
import bpy
obj = bpy.data.objects["Body"]
if obj.type != "MESH":
raise TypeError("Body must be a mesh object")
for vertex in obj.data.vertices:
memberships = list(vertex.groups)
total = sum(item.weight for item in memberships)
if total > 0.0:
for item in memberships:
group = obj.vertex_groups[item.group]
group.add(
[vertex.index],
item.weight / total,
"REPLACE",
)This script normalizes every existing group membership. It does not create missing groups, remove tiny values, or enforce a maximum number of influences. Those tasks are often easier to perform with Weight Paint mode or Blender’s vertex-group operators.
> callout note > Run inspection scripts before changing weights. Save a copy of the blend file before assigning large sets of values, because a script can modify thousands of vertices immediately.
Common checks
- Confirm the mesh has an Armature modifier whose object is the intended armature.
- Confirm each group name matches a bone with Deform enabled.
- Check that weights are not all zero.
- Inspect vertices near joints, where several groups should usually blend.
- Test the result in Pose Mode after scripting changes.
Exercise
Why does assigning weight to a group named "arm" fail to deform the mesh when the bone is named "Arm.L"?
Show answer
The Armature modifier matches vertex groups to bone names exactly. The group must be renamed to "Arm.L", or the bone must be renamed to match the group.