Knowing the names of the bone and the armature, I would like to animate the bone by setting the location and/or rotation of the bone starting from say frame 24 to 48. I'm an absolute beginner and I googled but I'm still confused.

I use this code to translate and rotate:

bpy.ops.transform.translate(value=(0, 1, 0), orient_type='GLOBAL')

bpy.ops.transform.rotate(value=1.5708, orient_axis='X', orient_type='GLOBAL')

They work only when I manually select the bone (by clicking it). so I tried to use this code to set the active bone but it didn't work:

bpy.data.objects["Armature"].data.bones["Bone"].select = True

Would you help me, please?

I enter Pose mode using this code:


Pose bones belong to the object

If ob is our armature object, ob.type == 'ARMATURE' the pose bones belong to ob.pose.bones. The edit bones are in the armature ob.data.bones.

To set active pose bone(s) to use operators see Set active bone in pose mode from Python script

To get a specific pose bone knowing its name

name = "Bone"
pb = ob.pose.bones.get(name) # None if no bone named name

Once you have the pose bone can set its properties or matrix.

pb.location = (0, 0, 1)

How to get world-space matrix of any pose bone?

Add keyframe for all locations x, y, z at frame 23

pb.keyframe_insert("location", frame=23)

Note if a bone is connected then translating will have no effect.

| improve this answer | |

Your Answer

