Selecting bones on script

I made a little helper script for selecting bones. One of the buttons selects all selected bones’ children.

It’s confusing a lot, because:

-the BONE_SELECTED constant can only be added to the bone.options when makeEditable() was called for the armature.
-but, if makeEditable() was called, the armature returns Editbones and not Bones. Editbones have no ‘children’ or ‘getAllChildren’ functions.

-in either case, the BONE_SELECTED attribute works in pose mode. Even the Editbones return the BONE_SELECTED status they get in pose mode, not in Edit mode.

I ended up with this code that works nice in Pose Mode, but I found no way to select bones (or detect if they are selected!) in Edit mode. Please help.

Sorry if some of the code is not optimized. It’s my first time with python. I have to clean up.


########################################
# selects all children of selected bones

def selectchildren():
    print ''
    print '###### select children #####'
    
    sce = bpy.data.scenes.active
    arm_ob = sce.objects.active
    
    if not arm_ob or arm_ob.getType() != 'Armature':
        PupMenu('Please select an armature bone')
        return
        
    arm_data = arm_ob.getData()

    bones = arm_data.bones.values()
    
    
    selectlist = []
    optionlist = []
    
    print '#bonelist#'
    print bones
        
    #List bones to select (only works from pose mode - not edit mode)

    for bone in bones:
                
        if (Armature.BONE_SELECTED in bone.options):
            selectlist += bone.getAllChildren()
                 
    #Flag bones SELECTED
    
    arm_data.makeEditable()
    
        
    for selectbone in selectlist:
    
        optionlist = arm_data.bones[selectbone.name].options
        optionlist.append(Armature.BONE_SELECTED)
        arm_data.bones[selectbone.name].options = optionlist
        
        
    arm_data.update()
                    
    Blender.Redraw()