Tuesday, February 1, 2011

[Scripting 2.5 FAQ] Setting the active bone

Just a quick tip for riggers/scripters working in Blender 2.5.

Many operators for pose bones (including those for constraints) rely on having an active bone to refer to. However, how do we set this?

The answer is simple, though perhaps non-obvious:
arm = bpy.data.armatures["MyArmature"]
arm.bones.active = posebone.bone

where posebone is a pointer to some bone you've obtained from "object.pose.bones"

That's it. Simple :)

3 comments:

  1. Simple, sure... working, not quite...

    When you try to use a posebone you get a;
    TypeError: BPy_PropertyRNA - Attribute (setattr): ArmatureBones.active expected a Bone type

    since armature.bones are Bone types.

    And object.pose.bones doesn't have an 'active' attribute either.

    What does work is setting the active bone using arm.bones.active = arm.bones['Bone'] (but doesn't update the 3dview properly).

    ReplyDelete
  2. Right, well I didn't say to use a posebone directly! Rather, I said posebone 'DOT' bone (posebone.bone), which gets the bone type as required.

    The version you give also works, but is a bit more verbose.

    I guess that you could also do:
    arm.bones.active = arm.bones[posebone.name] # :)

    ReplyDelete
  3. hey aligorth,

    I'm able to select up and down in an armature of bones with
    bpy.ops.armature.select_hierarchy(direction='PARENT')

    But i can't figure out how to select the tip or tail of a bone.

    any suggestions?

    kesten

    ReplyDelete