1

Is there a new command for selecting bones when in pose mode?

myPoseBone = bpy.data.objects['metarig'].pose.bones["thigh.R"]
myPoseBone.select = True

does not seem to work (AttributeError: 'PoseBone' obejct has no attribute 'select').

However, everybody seems to do it that way. Does it still work for you? Thanks in advance,

Matthias

Mantikator
  • 71
  • 10

2 Answers2

5

Select the armature bone.

Further to the answer by @yhoho a pose bone has a bpy.types.PoseBone.bone property pointing to its associated armature bone.

Example from python console (where C = bpy.context) , bone of interest has name "Bone", armature object has context.

When unsure of things the python console is a handy way to interactively see what's what

>>> C.object
bpy.data.objects['Armature']

>>> pb = C.object.pose.bones.get("Bone") >>> pb bpy.data.objects['Armature'].pose.bones["Bone"]

its associated armature bone, from its bone property

>>> pb.bone
bpy.data.armatures['Armature'].bones["Bone"]

Which tells us could also get this via the armature, ie the data part of the armature object.

>>> arm = C.object.data
>>> arm
bpy.data.armatures['Armature']

>>> bone = arm.bones.get("Bone") >>> bone bpy.data.armatures['Armature'].bones["Bone"]

The two bones referenced in different ways are equal

>>> bone == pb.bone
True

but are not an instance of same object,

>>> bone is pb.bone
False

A bone from the armature bones collection of type bpy.types.Bone has a select property, whereas a pose bone bpy.types.PoseBone from an armature objects pose bone collection has not (AFAIK never has).

Hence as shown to select, can use either of

pb.bone.select = True
bone.select = True

The idea of using collection.get(name) is that it returns None if the named item does not exist and can be checked against None (boolean False) and handled.

>>> pb = C.object.pose.bones.get("NonExistentBone")
>>> pb is None
True

Related

Set active bone in pose mode from Python script

batFINGER
  • 84,216
  • 10
  • 108
  • 233
0

you need specify the "bone" method

myPoseBone = bpy.data.objects['metarig'].pose.bones["thigh.R"].bone
myPoseBone.select = True
yhoyo
  • 2,285
  • 13
  • 32
  • 1
    Please add more information to your answer to prevent it from getting flagged as "low quality", and deleted. – Nate_Sycro27 Sep 23 '20 at 00:22
  • @Nate_Sycro27 actually I thought the same but I don't know what other info add. – yhoyo Sep 23 '20 at 01:04
  • 1
    Consider changing up to myPoseBone.bone.select = True altering line above such that "My Pose Bone" is actually a pose bone, not an armature bone. – batFINGER Sep 23 '20 at 08:47