Python: set 2 world orientations to 0

Hi! I have an object and a script that should reset object’s world orientation x and y to 0.0, but keep z orientation as it is. How to do it?

You can create an Euler object from mathutils and set the x and y entire to 0, leaving the Z. To leave the Z, just obtain your Euler object from the object’s orientation obj.worldOrientation.to_euler()
Set the world orientation as this new Euler.

OK! I found the first part already, but I wanna know the next code!

To convert your euler back to a matrix (worldOrientation is a matrix) use .to_matrix()


euler_ori = own.worldOrientation.to_euler()
#do stuff to euler_ori
own.worldOrientation = euler_ori.to_matrix()

How should euler_ori look like? Maybe a scheme? And wont it affect also Z axis? As I understand, I need 3x3 matrix([-,-,-],[-,-,-],[-,-,-]), but what to type in so the rotation x & rotation y is 0, but rotation Z doesn’t get affected?

You can set directly the object.worldOrientation with an euler, it’s not necessary turn it back to a matrix.

Well, maybe someone could just tell me code? I don’t know much about orientation matrix. I more need it to be done quick than to train my brain in figuring this out.


#Get the euler of the orientation, eulers are easier to edit than matrices (they are like vectors)
orientation = object.worldOrientation.to_euler()

#Set the x and y values to 0.0
orientation.x = 0.0
orientation.y = 0.0

#Se the object's orientation as the edited euler
object.worldOrientation = orientation

Thanks, that works!:slight_smile: