Remove bone parent

Hi,

I try to remove parenting on all bones of an armature and it does not work

bpy.ops.object.mode_set(mode='POSE')
for poseBone in context.object.pose.bones:
    bone = armature.data.bones[poseBone.name]
    bpy.context.object.data.bones.active = bone
    bpy.ops.object.parent_clear(type="CLEAR_KEEP_TRANSFORM")

what am I doing wrong ?
thanks for your help

try something like

bone.parent = None

Instead of the bpy.ops parent_clear
I don’t think you need to set the active bone either in that case.
That should give :

bpy.ops.object.mode_set(mode='POSE')
for poseBone in context.object.pose.bones:
    bone = armature.data.bones[poseBone.name]
    bone.parent = None
1 Like

actually it has to be done in edit mode like this

bpy.ops.object.mode_set(mode='EDIT')

for editableBone in context.editable_bones:
    armature.data.edit_bones.active = editableBone
    bpy.ops.armature.parent_clear(type='CLEAR')

Ok ! To be honest I didn’t take the time to try the solution I provided.
I you find something that works for you in the end it’s great !

But I’m pretty sure it’s possible to remove the parenting without using bpy.ops.
As a rule of thumb, if you can do something without using bpy.ops it’s always better.

If you have time to experiment, you may try this :

bpy.ops.object.mode_set(mode='EDIT')

for bone in context.editable_bones:
    bone.parent = None

All that said, if your solution using bpy.ops is working then it’s ok to keep it !

1 Like

thx for helping anyway
I posted another one, maybe you have the anwser :wink: