How do you get an object's rotation via python?

I would like to get a float number of a specific rotation of an object.
For example the Z rotation of an object.
I have tried this:

rZ = obj.localOrientation[2]

But this gives me this:

 Vector((0.0, 0.0, 1.0))

How do I get just the 1.0?

obj.localOrientation.z doesn’t work. It does work with the position.
Is there a way how to do it?

[2][2] (ten characters)

rZ[2] would also give the same result as obj.localOrientation[2][2]

Also, object.rotation_euler[n] gives rotational values. [0] is X, [1] is Y, and [2] is Z. (If you have XYZ Euler as the rotation, anyway)

1 Like

You’re probably looking for the to_euler method:

rZ = obj.localOrientation.to_euler().z

This way you can access the rotation as XYZ axis instead of a matrix.

3 Likes

In addition to @joelgomes1994

to_euler takes radians if you want to change the z value, so you need to import math module and use the math.radians(degrees) or the other way math.degrees(radians) in order to set the right amount.

also orientation.z or orientation[2] gives false coords once a while, it’s better to use orientation.col[2] better be safe then wondering why something isn’t working properly.

2 Likes

Yes, this works exactly as wanted, thank you.

1 Like