Setting the rotation of an object

So I have an object in the game engine. I want to set the rotation value of the x axis of this object to a specific number. I know for a fact that applyRotation will not do it because I have tried. applyRotation simply adds onto the current rotation of the object. I want to completely replace that number used as the rotation.

The only possible method for doing what I am asking is by using the object.orientation(3x3 matrix). I read about using the orientation matrix and I don’t have the time to figure out how to use it to my needs. Solutions?

You can convert the 3x3 matrix of the object.localOrientation to a euler:


#Convert the matrix
rotation = object.localOrientation.to_euler()

#Make the changes that you want
rotation.x=...
rotation.y=...
rotation.z=...

#Set the rotation
object.localOrientation = rotation #You can put directly a euler to the localOrientation


The last example convert the actual matrix to a euler and modify it(usefull when you need to set only a axis, and not all them), if you want to change all the axis, a more easier method is just create a euler an set it as the rotation:


#Create the euler
rotation = mathutils.Euler((0.0, 0.0, 0.0), 'XYZ') #Create a euler with x=0, y=0, z=0

#Set the rotation
object.localOrientation = rotation


A euler is more easy to manage. The euler needs to set with radians, you need to convert it to degress(when you are setting) and later convert it again to radians(before to pur the auler to the rotation), but i don’t how to do it, I never needed to set the rotation directly :no:.

Read the mathutils module for usefull information.

EDIT: remember import the mathutils module!!!

Thank you for your code snippets! Now, in the line that converts the orientation matrix to eulers and stores it into the rotation variable, the console keeps saying that ‘Matrix’ object is not callable.

Remove the parenthesis after localOrientation:

rotation = object.localOrientation.to_euler()