Setting rotation with python doesn't work?

Hello.

I’m having a problem with setting the rotation of an object with python.

I used:
obj[‘rot’] = [0, 0, 0]

But no matter what I set the 3 rotation values to, nothing seems to happen. The object has nothing else that would affect it’s rotation, and it’s not parented(though it is vertex parented, but that doesn’t affect rotation). What am I doing wrong?

Where do you have that Command from? It is none that I have ever seen and it looks like nothing that would in any Way suit the Blender Game Engine Python Language.


from bge import logic as g

c = g.getCurrentController()
o = c.owner
o.applyRotation([0.0,0.0,1.0],True)

C.A.ligari:
I did a google search, and that came up. Oddly, Blender never said there was an error with the script.

ManOfSteel:
Thanks for replying, but that’s not quite what I was looking for. That seems to change the rotation relative to the current rotation(and running the script repeatedly rotates the object non stop). I want a way to set the absolute rotation. I guess I could set the orientation, but that would require me to convert the rotation to matrix form, which I have no clue how to do.

Basically, I have a script that retrieves the angle of a surface in radians, and I want to apply that angle to a character. And I want the character to only align with the surface on one axis. Does anyone know of a solution?

The mathutils module would tend a help a bit here because it allows you to avoid direct fiddling with matrices (which can be quite hard to grasp)


import mathutils

#convert the orientation to a euler so we can easily set it#
myOrient = myObject.worldOrientation.to_euler()

#set the new orientation (in radians)#
myOrient[0] = x_orientation
myOrient[1] = y_orientation
myOrient[2] = z_orientation

#build our new matrix so it can be applied to the object#
myMatrix = myOrient.to_matrix()

#change the orientation according to our matrix#
myObject.worldOrientation = myMatrix

@laser - The BGE didn’t throw an error because you just set a local variable in the object, ‘rot’, to that list. There’s nothing wrong with it, but it didn’t do anything other than set that variable.
@Ace - I don’t think you have to convert the orientation to a matrix again - just setting the orientation to the Euler value should work.

EDIT: Actually, look into the object’s alignAxisToVect() function - that should be what you want. If not, try using the Euler (in this case, my_orient) variable’s rotate_axis function. I believe it’s local rotation, allowing you to use simple rotations and still rotate on the correct axis toward the surface.

Have you tried using obj.localAngularVelocity(), obj.worldAngularVelocity() or obj.setAngularVelocity()?

Edit: Sorry, my bad. I was thinking rotation speed instead of orientation. Should have read more carefully.

Here’s what i would use.
Simply change the variable “object” to the reference to your object, and the variable “angle” to the angle you want, and the variable “axis” to the axis you want (x=0,y=1,z=2)

For an angle in degrees:


from math import*
from mathutils import*
axis = 2
angle = 90
local = object.localOrientation[axis]
object.worldOrientation=Matrix.Rotation(radians(angle), 3, local)  

For an angle in radians:


from mathutils import*
axis = 2
angle = 90
local = object.localOrientation[axis]
object.worldOrientation=Matrix.Rotation(angle, 3, local)  

Thanks for your help everyone.

I’m getting an error: no module named mathutils.

Though, I managed to get this to work:

myOrient = [0,0,0]
myOrient[0] = x_orientation myOrient[1] = y_orientation myOrient[2] = z_orientation #change the orientation according to our matrix# myObject.worldOrientation = myOrient

I didn’t know I could set orientation using euler angles.

I think you have an older version.
change
mathutils in to Mathutils

you can use directly the Euler
radians (0 / 360)>> euler (0/6.28)
0 = 0
90 = 1.57
180 = 3.14
360 = 6.28

(is-bit rounded)

to convert radians to euler can multiply
radians*0.0174533=euler

i use this(2.57)

from bge import logic as BG

c=BG.getCurrentController()
o=c.owner
sce=BG.getCurrentScene()
sens=c.sensors["Keyboard"]



if sens.positive:
    
    rot=o.worldOrientation.to_euler() 

    R=0.0174533
    rot.x = 00 *R
    rot.y = 00 *R
    rot.z = 45 *R
    o.worldOrientation=rot


agree, 90% of problem solved by couple alignAxisToVect() and getVectTo()
but need to learn well;)