## Script Description
This Blender script allows you to join multiple vertex groups associated with selected bones into a single vertex group. The script is specifically designed to work in Edit mode when you have an armature with multiple bones selected. The vertex group corresponding to the active bone will be used as the destination group, and the weights of the other selected bone vertex groups will be summed and applied to this group. After the operation, the other selected bone vertex groups will be deleted. If the destination vertex group ends up with a weight of 0 for all vertices, it will also be deleted.
## How to Use the Script
1. Open Blender and select an armature object.
2. Enter Edit mode and select multiple bones, including an active bone, for which you want to join the associated vertex groups.
3. Copy and paste the script into Blender’s Text Editor.
4. Click the “Run Script” button in the Text Editor to execute the script.
5. The script will perform the operation on every mesh object in the scene that has an Armature modifier set to the selected armature.
Please note that the script must be run while in Edit mode with the desired bones selected, and it requires an active bone to be set among the selected bones.
## Source
```python
import bpy
# Get the active object
armature_obj = bpy.context.active_object
if not armature_obj or armature_obj.type != 'ARMATURE':
raise Exception("Please select bones from an armature.")
# Check current mode and ensure it is Edit mode
current_mode = bpy.context.mode
if current_mode != 'EDIT_ARMATURE':
raise Exception("Please switch to Edit mode.")
# Access Edit mode bones
armature = armature_obj.data
selected_bones = [ebone.name for ebone in armature.edit_bones if ebone.select]
active_bone = armature.edit_bones.active.name if armature.edit_bones.active else None
# Verify that active bone is one of the selected bones
if active_bone not in selected_bones:
raise Exception("Active bone must be one of the selected bones.")
# Switch to Object mode to modify vertex groups
bpy.ops.object.mode_set(mode='OBJECT')
# Iterate over all objects in the scene
for obj in bpy.context.scene.objects:
# Check if the object is a mesh and has an armature modifier set to the selected armature
if obj.type == 'MESH' and any(mod.type == 'ARMATURE' and mod.object == armature_obj for mod in obj.modifiers):
# Get vertex groups with the same names as selected bones
bone_vgroups = [vg for vg in obj.vertex_groups if vg.name in selected_bones]
active_vgroup = obj.vertex_groups.get(active_bone)
# If the active bone vertex group does not exist, create it
if not active_vgroup:
active_vgroup = obj.vertex_groups.new(name=active_bone)
# Iterate over each vertex in the mesh
for vertex in obj.data.vertices:
# Set the active bone vertex group weights to the sum of the bone vertex group weights
total_weight = 0.0
for vg in bone_vgroups:
try:
total_weight += vg.weight(vertex.index)
except RuntimeError:
pass # Vertex is not in this vertex group, skip
active_vgroup.add([vertex.index], total_weight, 'REPLACE')
# Delete all of the bone vertex groups, except for the active bone vertex group
for vg in bone_vgroups:
if vg != active_vgroup:
obj.vertex_groups.remove(vg)
# If the active bone vertex group has a weight of 0 for all vertices, delete it as well
if all(active_vgroup.weight(vertex.index) == 0 for vertex in obj.data.vertices):
obj.vertex_groups.remove(active_vgroup)
# Switch back to Edit mode
bpy.ops.object.mode_set(mode='EDIT')
```