Posted by Chris Plush on Jul 25, 2014

This one’s for the Python dabblers that haven’t been able to figure out `localOrientation`

. Using the function `localPosition`

is easy enough because it returns a list of x, y, and z values. Using `localOrientation`

for rotation values, however, returns a 3×3 matrix. So unless you’re brave enough to handle matrices what we’re going to do in this quick tutorial is convert our object’s matrix to degrees for easier reading and changing. For an intro to python scripting in the BGE, check out my beginner’s python tutorial.

Open the text editor in Blender and add a new file. Name this “rotation”. Paste in the following code:

```
import bge
import math
cont = bge.logic.getCurrentController()
own = cont.owner
xyz = own.localOrientation.to_euler()
rotz = math.degrees(xyz[2])
print (rotz)
```

Now select your object and in the Logic editor add an Always sensor(set to true pulse) and connect it to a Python controller(set to script “rotation”). Add logic bricks for turning the object left and right. Press P over the 3D View and turn the object around a bit so you can see the rotation values change. Then check the system console. It’ll print out the object’s Z rotation value in degrees.

**Logic setup:**

**Z rotation values printed in the console:**

This is a simple script. In the first line we import the math module because we’ll need special functions for angle conversions. Then the `cont`

and `own`

lines are standard lines for getting information on the object running the python script(the owner).

The `xyz`

variable is the object’s orientation matrix, except we added `to_euler()`

at the end of it to convert the matrix to radians. Radians are a different angle measurement that ranges from 0 to 2π. So now instead of being a matrix, `localOrientation`

is converted to a list with x, y, and z values in radians. Now we can take each one of these values and convert them into degrees. So the last variable, `rotz`

, takes the third item in the `xyz`

list and converts it to degrees.

Then we print `rotz`

. Now you can see what direction an object is facing. Make a note that the degree values will range from -180 to 180, not 0 to 360(unless you want to add a little extra math to make that happen). Also make a note that to get the x value, it would be `xyz[0]`

, and the y value would be `xyz[1]`

. The first item in python lists is 0 in case you were confused.

Now reading the rotation value is one thing, but how about changing it? It’s pretty much the same process, except in reverse. Erase your script and paste in the following script:

```
import bge
import math
cont = bge.logic.getCurrentController()
own = cont.owner
xyz = own.localOrientation.to_euler()
xyz[2] = math.radians(45)
own.localOrientation = xyz.to_matrix()
```

In this script we still convert `localOrientation`

to euler first. Then we change `xyz[2]`

, which is the Z value, to the radian equivalent of 45 degrees(you can put any number or variable you want in there). Then we convert it back to a matrix in the last line and assign it to `own.localOrientation`

. When you press P in the 3D View, the cube will instantly rotate to 45 degrees.

Easy as pi.

Check out a little demo I made of aiming a catapult at a castle. You can even type in your own angles to set the catapult to for precise aiming. There are a couple different short scripts with notes in each one telling you what they do.