1
$\begingroup$

I've been trying for a while now to rotate a rig around its local axis.

Given 3 angles I would like to rotate the bone around its axis. I saw that in the UI method that is used bpy.ops.transform.rotate method, however this method has an argument called orientation matrix which I'm not sure how to calculate.

Does anyone know how I am supposed to calculate this arg?

$\endgroup$
1
  • $\begingroup$ Are you trying to rotate an object or a bone in pose mode? $\endgroup$ Commented Jul 4, 2022 at 19:55

1 Answer 1

1
$\begingroup$

This argument is optional ; but if you really want to deal with linear transformation matrices, you have to understand how it works.

You could use matrices in order to compute translation, scale and rotation.

Here, the matrix is a rotational matrix, which expect some value according to the axis you choose given an angle t (aka theta) :

  • for the x axis, the matrix will be (( 1, 0, 0), (0, cos(t), sin(t), (0, -sin(t), cos(t))

  • for the y axis, the matrix will be (( cos(t), 0, -sin(t) ), (0, 1, 0), (sin(t), 0, cos(t))

  • and for the z axis, you will have ((cos(t), sin(t), 0), (-sin(t), cos(t), 0 ), (0, 0, 1))

If you select the default cube, you can see its transformation matrix:

>>> obj = bpy.context.active_object
>>> obj.matrix_world
Matrix(((1.0, 0.0, 0.0, 0.0),
        (0.0, 1.0, 0.0, 0.0),
        (0.0, 0.0, 1.0, 0.0),
        (0.0, 0.0, 0.0, 1.0)))

Now, you can use a mathutils.Matrix in order to rotate the object to 20° on the X axis:

>>> rotation_mat = mathutils.Matrix.Rotation(math.radians(20), 4, 'X')
>>> obj.matrix_world @= rotation_mat

I use the @= python operator that provide a matrix multiplication

Does anyone know how I am supposed to calculate this arg ?

Now you know how to calculate this arg ; but not how to use it ! The way bpy.ops.transform.rotate use the orient_matrix argument is not clear ; it seems to have no effect here, because you have to provide the mandatory 1st argument which set the angle value and do the same thing. With the Blender Python API, Some parameters are historical, some others for future use, and some others are not implemented at all but still documented. You could ask the team because I tried many way to use it without any effect.

Last but not least, playing with Maths is really easy if you read the right book: 3D Math Primer for Game Dev - Fletcher Dunn - 2cd Ed.- 2011 is definitively the best book for anybody having to play with math without any skills. You will find 3 chapters on Matrices, very well explained.

$\endgroup$

You must log in to answer this question.

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