How to append bones to a list in a branching manner I'm looking to append bones in a specific manner. Currently I'm doing this

https://github.com/General-101/H2V-Blender-JMSv2-Exporter/blob/master/Blend2Halo2-JMSv2.py#L209

nodeslist = list(obj.data.bones)


but it's not accurate for what I need. I basically need to add all of the children of the first bone and then move on to their children and append them all before moving on to the next. Basically appending bones by their layer.



Something like this would be appended in this order with the above code
0 1 2 3 4 5

but I need the list in this order

0 1 3 5 2 4

Any ideas on how I could accomplish this?
Use recursion

import bpy


def walk_bones(bone):
    yield(bone)
    for b in bone.children:
        yield from walk_bones(b)

ob = bpy.context.object
arm = ob.data
rootbones = [b for b in arm.bones if not b.parent]

for rb in rootbones:
    for b in walk_bones(rb):
        print(b.name)


Adjusting slightly to 

def walk_bones(bone, level=0, sp="  "):
    yield(f"{sp * level}{bone.name}")
    for b in bone.children:
        yield from walk_bones(b, level + 1)


Can print out the hierarchy, as an example the rigify shark meta-rig 

spine
  spine.001
    spine.002
      spine.003
        spine.008
          spine.004
            spine.005
              spine.006
                spine.007
                  eye.L
                  eye.R
                  jaw.master
                    jaw
                      jaw.001
                    jaw.002.L
                      jaw.003.L
                    jaw.002.R
                      jaw.003.R
              shoulder.L
                side_fin.L
                  side_fin.L.001
              shoulder.R
                side_fin.R
                  side_fin.R.001
            top_fin
              top_fin.001
          chest_fin.Bot.L
          chest_fin.Bot.R
    mid_fin.Top
    mid_fin.Bot
  back_fin.T.Bk
    back_fin.T.001.Bk
      back_fin.T.002.Bk
  back_fin.B.Bk
    back_fin.B.001.Bk