# orient module

I don’t know if anyone will find this useful, but I thought I would post it. It’s from Spike and Siegal(I think) post and also from Orientation tutorial posted a while back, which was an excellent read, btw. Anyway, if you put this file in your text cache and call it Orient.py.

``````
#Orient.py
#use like this
#mat1 = Orient.degrees(0.0,0.0,45.0)
#object.setPosition(mat1) for global orientation by degrees
#mat2 = owner.getOrientation()
#object.setPosition(Orient.mult(mat1,mat2)) for local rotation

import math

def degrees(alpha,beta,gamma):
alpha = -alpha * math.pi/180
beta = beta * math.pi/180
gamma = -gamma * math.pi/180
a = math.cos(alpha)
b = math.sin(alpha)
c = math.cos(beta)
d = math.sin(beta)
e = math.cos(gamma)
f = math.sin(gamma)
bd = b*d
mat = [[c*e, -a*f+b*d*e, b*f+a*d*e],
[c*f, a*e+b*d*f, -b*e+a*d*f],[-d, b*c, a*c]]
return mat

def mult(mat1,mat2):
return [[(mat1[0][0]*mat2[0][0])+(mat1[0][1]*mat2[1][0])+(mat1[0][2]*mat2[2][0]),
(mat1[0][0]*mat2[0][1])+(mat1[0][1]*mat2[1][1])+(mat1[0][2]*mat2[2][1]),
(mat1[0][0]*mat2[0][2])+(mat1[0][1]*mat2[1][2])+(mat1[0][2]*mat2[2][2])],
[(mat1[1][0]*mat2[0][0])+(mat1[1][1]*mat2[1][0])+(mat1[1][2]*mat2[2][0]),
(mat1[1][0]*mat2[0][1])+(mat1[1][1]*mat2[1][1])+(mat1[1][2]*mat2[2][1]),
(mat1[1][0]*mat2[0][2])+(mat1[1][1]*mat2[1][2])+(mat1[1][2]*mat2[2][2])],
[(mat1[2][0]*mat2[0][0])+(mat1[2][1]*mat2[1][0])+(mat1[2][2]*mat2[2][0]),
(mat1[2][0]*mat2[0][1])+(mat1[2][1]*mat2[1][1])+(mat1[2][2]*mat2[2][1]),
(mat1[2][0]*mat2[0][2])+(mat1[2][1]*mat2[1][2])+(mat1[2][2]*mat2[2][2])]]

``````

You can then do this in a python script:

``````

import GameLogic
import Orient

cont = GameLogic.getCurrentController()
owner = cont.getOwner()
owner.setOrientation(Orient.degrees(0.0,0.0,45.0))
#set the absolute orientation to 45 degrees clockwise on the z axis
mat1 = Orient.degrees(0.0,0.0,45.0)
mat2 = owner.getOrientation()
owner.setOrientation(Orient.mult(mat1,mat2))
#set the local rotation of the object so it turns 45 degrees clockwise
#on the z axis

``````

If you do both those things as in this script you will end up with the object rotating 90 degrees.
Anyway, most times this is all I need to do, so it’s handy just loading Orient.py and doing an import. It’s just rehashed in a way that I can use it easily and remember it. You may also just prefer using Spikes and Siegal code the way it was, so do a search. Sorry if I miscredited anyone. The rotations are all clockwise in the 1,3,and 7 views.

i am not sure if this should belong here or python and plugins.

It normally get’s posted here. It’s strictly game engine.

useful code though.

Thanks guys although i must admit Spike coded it all and i just tweaked it to work the way we wanted it to.

Here is the original post: http://blenderartists.org/forum/showthread.php?t=47149, and
if you scroll to the bottom you will see a new and improved script by Spike who now has tons more knowledge about those types of things.

Later

good useful code
hew can reversal that
EX:
if object orient (z or x) = 45:
do that or stab or …
thanks