2
$\begingroup$

I want to rotate object in blender, for which I am using following operations.

ob = bpy.context.active_object
ob.rotation_euler = (x,y,z)

It is working well. Now if reaped the this definition for same object, it doesn't consider the last updated orientation. It considers the original orientation.

What can I do?

$\endgroup$
1
  • 1
    $\begingroup$ Have you tried ob.rotation_euler.rotate( Euler( (x, y, z) ) ) ? $\endgroup$
    – lemon
    Feb 23, 2020 at 16:04

1 Answer 1

4
$\begingroup$

You are setting the rotation.

The code in question you are setting the Euler rotation. The resulting value will always be (x, y, z)

Instead recommend make a transform (rotation) matrix. Python console example. x, y, and z are 30, 45 and 60 degrees respectively.

>>> x, y, z = (radians(30), radians(45), radians(60))
>>> Euler((x, y, z))
Euler((0.5235987901687622, 0.7853981852531433, 1.0471975803375244), 'XYZ')

The context object, has Euler rotation set to zero.

>>> ob = C.object
>>> ob.rotation_euler
Euler((0.0, -0.0, 0.0), 'XYZ')

Create a transform matrix from our Euler. Make it 4x4 so we can multiply it by the 4x4 object matrix. Notice after the first multiplication we get the result as above.

>>> R = Euler((x, y, z)).to_matrix().to_4x4()
>>> ob.matrix_world = R @ ob.matrix_world
>>> ob.rotation_euler
Euler((0.5235987901687622, 0.7853982448577881, 1.0471975803375244), 'XYZ')

and again

>>> ob.matrix_world = R @ ob.matrix_world
>>> ob.rotation_euler
Euler((1.6257474422454834, 0.4853370189666748, 2.580118417739868), 'XYZ')

Please note this is global rotation. For local use ob.matrix_local instead.

Note: As pointed out by @lemon for Eulers could also use

ob.rotation_euler.rotate(Euler((x, y, z)))

to transform the local euler rotation from another.

For a lot of cases, the axis and angle is defined. Eg make a 4x4 rotation matrix of 30 degrees around the axis (1, 1, 1)

R = Matrix.Rotation(radians(30), 4, (1, 1, 1))

Our R from above as an axis / angle rotation

>>> R.to_quaternion().to_axis_angle()
(Vector((0.03912381827831268, 0.7727739214897156, 0.6334742903709412)), 1.2104885578155518)

is the equivalent of rotating 69.356 degrees around axis (0.03912381827831268, 0.7727739214897156, 0.6334742903709412)

$\endgroup$

You must log in to answer this question.

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