8
$\begingroup$

I have a bone, which i need to rotate. It has to be done on a server all the time, which means, that it would be nice to avoid clicking on the bone.

I understand that i need to use:

bpy.data.objects['Armature'].pose.bones['Bone'].matrix_basic[0]

allthough i might be wrong.

After the bone is rotated, I need to Keyframe the rotation, how would i go about this?

$\endgroup$

1 Answer 1

12
$\begingroup$

I assume you are rotating the bone in pose mode, for this you can use the following lines:

import math
import bpy

ob = bpy.data.objects['Armature']
bpy.context.scene.objects.active = ob
bpy.ops.object.mode_set(mode='POSE')

pbone = ob.pose.bones[bname]
# Set rotation mode to Euler XYZ, easier to understand
# than default quaternions
pbone.rotation_mode = 'XYZ'
# select axis in ['X','Y','Z']  <--bone local
axis = 'Z'
angle = 120
pbone.rotation_euler.rotate_axis(axis, math.radians(angle))
bpy.ops.object.mode_set(mode='OBJECT')
#insert a keyframe
pbone.keyframe_insert(data_path="rotation_euler" ,frame=1)

the code from this wiki page , it contains useful scripts for batch job

$\endgroup$
2
  • $\begingroup$ AttributeError: 'Bone' object has no attribute 'rotation_euler' $\endgroup$ Commented Sep 16, 2022 at 7:19
  • $\begingroup$ this rotates bone around unknown axis :) $\endgroup$
    – Hope
    Commented Feb 23, 2023 at 2:48

You must log in to answer this question.

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