> For the complete documentation index, see [llms.txt](https://aconite0101s-works.gitbook.io/aconite0101s-works-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aconite0101s-works.gitbook.io/aconite0101s-works-docs/blender/script/bone-remover.md).

# Bone Remover

Blender py script use to remove bone from amature

<details>

<summary><strong>Remove Zero Weight bone (All)</strong></summary>

<pre class="language-python" data-overflow="wrap"><code class="lang-python"><strong>import bpy
</strong>
# Collect all selected meshes
selected_objects = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']

# Make sure there's at least one selected mesh
if not selected_objects:
    print("No mesh objects selected.")
else:
    # Assume all meshes use the same armature (via Armature Modifier)
    armature = None
    for mesh in selected_objects:
        for mod in mesh.modifiers:
            if mod.type == 'ARMATURE':
                armature = mod.object
                break
        if armature:
            break

    if not armature or armature.type != 'ARMATURE':
        print("No armature found in selected meshes.")
    else:
        # Get all vertex group names that have non-zero weights across all selected meshes
        valid_groups = set()

        for mesh in selected_objects:
            for v in mesh.data.vertices:
                for g in v.groups:
                    if g.weight > 0:
                        group_name = mesh.vertex_groups[g.group].name
                        valid_groups.add(group_name)

        # Switch to edit mode on the armature to remove bones
        bpy.ops.object.mode_set(mode='OBJECT')
        bpy.context.view_layer.objects.active = armature
        bpy.ops.object.mode_set(mode='EDIT')

        edit_bones = armature.data.edit_bones
        bones_to_remove = [bone.name for bone in edit_bones if bone.name not in valid_groups]

        for bone_name in bones_to_remove:
            edit_bones.remove(edit_bones[bone_name])
            print(f"Removed bone: {bone_name}")

        bpy.ops.object.mode_set(mode='OBJECT')
        print(f"Removed {len(bones_to_remove)} bones with zero weight from all selected meshes.")

</code></pre>

</details>

<details>

<summary><strong>Remove Zero Weight bone (</strong>Skip "Breast" Bones<strong>)</strong></summary>

This script will prevent you from deleting some bones. Certain avatars might have their "Breast" bone set to zero weight, and the script could delete that

```python
import bpy

# Collect all selected meshes
selected_objects = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']

if not selected_objects:
    print("No mesh objects selected.")
else:
    armature = None
    for mesh in selected_objects:
        for mod in mesh.modifiers:
            if mod.type == 'ARMATURE':
                armature = mod.object
                break
        if armature:
            break

    if not armature or armature.type != 'ARMATURE':
        print("No armature found in selected meshes.")
    else:
        # Get vertex groups that are actually used
        valid_groups = set()

        for mesh in selected_objects:
            for v in mesh.data.vertices:
                for g in v.groups:
                    if g.weight > 0:
                        group_name = mesh.vertex_groups[g.group].name
                        valid_groups.add(group_name)

        # Switch to armature edit mode
        bpy.ops.object.mode_set(mode='OBJECT')
        bpy.context.view_layer.objects.active = armature
        bpy.ops.object.mode_set(mode='EDIT')

        edit_bones = armature.data.edit_bones
        bones_to_remove = []

        for bone in edit_bones:
            if bone.name.startswith("Breast"):
                continue  # Skip bones like Breast_L, Breast_R, etc.
            if bone.name not in valid_groups:
                bones_to_remove.append(bone.name)

        for bone_name in bones_to_remove:
            edit_bones.remove(edit_bones[bone_name])
            print(f"Removed bone: {bone_name}")

        bpy.ops.object.mode_set(mode='OBJECT')
        print(f"Removed {len(bones_to_remove)} unused bones (excluding 'Breast' bones).")

```

</details>
