9
$\begingroup$

I want to retrieve an armature by name and access its bones. However, this doesn't work:

armature = bpy.data.objects["rig"]
bones = armature.bones

It gives the error

'Object' object has no attribute 'bones'`

The "rig" object is found, and the armature variable holds a bpy_struct after the first line executes.

So how do I access the bone data?

$\endgroup$

1 Answer 1

21
$\begingroup$

"rig" is an Object datablock, bone data however is part of an Armature datablock.

What you are looking for is bpy.data.objects["rig"].data, which is the armature:

# Active object
ob = bpy.context.object

if ob.type == 'ARMATURE':
    armature = ob.data

    for bone in armature.bones:
        print(bone.name)

An armature has two bone collections:

  • .bones
  • .edit_bones

Note that .edit_bones are only accessible if the armature is in Edit Mode.
See EditBones, PoseBones, Bone... Bones

There is a third kind of bone data structure for pose bones, which is referenced by the object and not the armature:

bpy.data.objects["rig"].pose.bones
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .