3
$\begingroup$

I create an armature an all the bones through a script, then parent them. But the bones change the order in the list after setting the parents.

This script shows that each bone index changes after the modifying the parents

import bpy
import mathutils

print('HELLO')
#Clear all
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()

#create armature
armature_da = bpy.data.armatures.new("Armature")
armature_ob = bpy.data.objects.new("Armature", armature_da)
bpy.context.scene.objects.link(armature_ob)
bpy.context.scene.objects.active = armature_ob


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

for i in range(10):
    bone = bpy.context.active_object.data.edit_bones.new(str(i))
    bone.head = mathutils.Vector((.01,0,0)) * i
    bone.tail = bone.head + mathutils.Vector((0,0,0.01))
bpy.ops.object.mode_set(mode='OBJECT')
print('creation',bpy.context.active_object.data.bones.keys())

print('first parenting')

armature = bpy.context.scene.objects.active 
bpy.ops.object.mode_set(mode='EDIT')
bones = armature.data.edit_bones
for i in range(9):
    editBone = bones[9-i]
#    editBone.parent = bones[9-i-1]   

#    editBone = bones[i]
    editBone.parent = bones[i+1]   

print('pre object',bpy.context.active_object.data.edit_bones.keys())
bpy.ops.object.mode_set(mode='OBJECT')
print('pos object',bpy.context.active_object.data.bones.keys())

print('second parenting')

armature = bpy.context.scene.objects.active 
bpy.ops.object.mode_set(mode='EDIT')
bones = armature.data.edit_bones
for i in range(9):
#    editBone = bones[9-i]
    editBone.parent = bones[9-i-1]   

    editBone = bones[i]
#    editBone.parent = bones[i+1]   

print('pre object',bpy.context.active_object.data.edit_bones.keys())
bpy.ops.object.mode_set(mode='OBJECT')
print('pos object',bpy.context.active_object.data.bones.keys())

Am I using a incorrect way to set parents? Is there a way to preserve the bone indexes?

I am aware that I could use the names to reference the bones from the array, but I prefer to stick to indexes.

$\endgroup$
2
$\begingroup$

No, you can't preserve bone indices, outside of edit-mode, bones are order by their hierarchy.

Its best to reference bones by name rather then index, (Or at least get their index once, outside of edit-mode)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.