Get an objects rotation in python

I was wondering if there was some way to get on objects rotation. What im trying isnt working.

Currently im using

object.getAxisVect

When I rotate it returns some decimals. I was wanting something simple like getRotation, but saw nothing in the API.

Any help is great.

The rotation of an object is described by default as a matrix

GameObject.worldOrientation

the mathutils module, from which subclasses such as Vector are derived, shows how you can convert between them

Assuming that you want the X, Y and Z rotations of each axis vector in degrees, you can do this…
Realise that angles are taken anticlockwise as positive.
sample.blend (608 KB)

Thanks for the help Agoose but its a little complicated and over my head, anything slightly simpler?. I havent gotten into functions and modules. When I print object.worldOrientation i get like 9 numbers. What I dont understand is when i rotate on 1 axis, 3 of the values move. Im basically just trying to define the y rotation of my object as I roll it.

I did a tutorial on Matrices / Portals that explains the basics of the orientation matrix, and how it works: http://www.youtube.com/watch?v=hyxS4RyWxpk

I must say after tinkering with Agoose’ little script I got this i set up in my script:


import math
matrix_rotation = owner.worldOrientation
euler_rotation = matrix_rotation.to_euler()
degrees_rotation = [math.degrees(a) for a in euler_rotation]
 
degrees = int(degrees_rotation[1])
 
print(degrees)

works great and I can get a whole number. Thanks Agoose

It’s actually a simple example, but i bet the module scared you off :wink:
Here’s how to use it:

add ‘import math’ to the top of your script (Without the ‘’)
now, copy the code for get_rotation into the top of you python file, below all the import statements:


def get_rotation(object):
    matrix_rotation = object.worldOrientation
    euler_rotation = matrix_rotation.to_euler()
    degrees_rotation = [math.degrees(a) for a in euler_rotation]
    return degrees_rotation

now, wherever you want the rotation of an object calling the function will return the x, y, z rotations.
If you just want the rotation about the Z axis, then do something like this:


z_rotation = get_rotation(object)[2]

or, if you want all 3:


x_rotation, y_rotation, z_rotation = get_rotation(object)

Ah, I was a little late replying :wink:
I’m glad you found it useful. If you get into this stuff, feel free to PM me, for I’ll happily explain why that is the case.
The code you’ve extracted is simply in the form of a function above, as you would use a function in maths. It means that if you get the rotation of an object more than once it may prove useful to use a function rather than copying and pasting code each time.

This is very helpful!

Next question is, how do you then SET the rotational position of an object? Let’s say for example that I simply want to rotate an object around the z-axis from 0 deg. to 180 deg. using the keyboard? i.e. I hit the A-key and it goes to 0 deg., I hit the S-key and it rotates to 180 deg. - If I hit the A-key again, back to 0 deg. - toggle, rinse, repeat.

Thanks!
V

I have updated my blend for you. You should be able to see how to use it :slight_smile:

sample (1).blend (613 KB)

Simply assign object’s worldOrientation property as follows:

object.worldOrientation = [angleX, angleY, angleZ] # ANGLES ARE IN RADIANS!!!

In fact, worldOrientation always returns a matrix, but also accepts three euler angles as inputs.

This i didn’t know! But yes, my example still works, and it may help you to keep consistent and use it :wink:

I would do something like:

if keypress.positive and degrees < 90:
…object.applyRotation

Will rotate till it hits 90.
Something similar. on phone so cant break it down til I get home