This script re-parents any selected bones in an armature to the active bone, provided they are not descendants of the active bone. It operates by traversing the bone hierarchy and changing the parent of each selected bone that isn’t the active bone. If a bone’s parent is changed, the script does not process its descendants.
## Directions
1. Open Blender and select the armature you want to modify.
2. Switch to Edit Mode.
3. Select the bones you want to re-parent, then lastly select the bone you want to be the new parent. This bone will become the active bone.
4. Run the script in Blender’s Python console or text editor.
## Source
```python
import bpy
# Ensure we're in edit mode
if bpy.context.object.mode != 'EDIT':
bpy.ops.object.mode_set(mode='EDIT')
armature = bpy.context.object
active_bone = armature.data.edit_bones.active
if not active_bone:
raise Exception("No active bone selected")
# Recursive function to traverse bone hierarchy
def traverse_bone_hierarchy(bone):
if bone == active_bone:
return # Stop traversal for this branch
# If the bone is selected, set its parent to the active bone
if bone.select:
bone.parent = active_bone
return # Stop traversal for this branch
for child in bone.children:
traverse_bone_hierarchy(child)
# Start traversal from root bones
for bone in armature.data.edit_bones:
if not bone.parent:
traverse_bone_hierarchy(bone)
```