Accessing the Rotation of a blender Object
I'm just starting out with Blender and Python and I've been trying to access the rotation properties of a cube using the blender game engine with Python 2.5.1
I have this python script attached to a cube in my scene:
cont = GameLogic.getCurrentController()
own = cont.owner
print own.RotX, own.RotY, own.RotZ
All I get is this error:
Python script error from controller "cont#CONTR#1": Traceback (most recent call last): File "starter", line 4, in AttributeError: 'KX_GameObject' object has no attribute 'RotX'Can anybody tell me how I can access the rotation properties? feel li开发者_如何学JAVAke I'm going crazy!
Thanks,
WillYou can use the property localOrientation, which seems to be the only way to access rotation in the game engine without using the motion actuator which doesn't allow the printing of the current rotation.
localOrientation consists of a list of lists, or a 3x3 matrix. each row of the matrix is a point that the corresponding axis will point to. For a default cube:
import GameLogic
cont = GameLogic.getCurrentController()
own = cont.owner
print(own.localOrientation)
will yield Matrix((1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)) because the x axis of the object points to the point (1, 0, 0), and y points to (0, 1, 0), the z to (0, 0, 1)
hope this helps!
EDIT: just played around with this, and I would like to say that if things are acting weird remember that this is LOCAL orientation. Check the local location of the object if things aren't working right! I just got severely confused because I didn't realize my object had a local position of (0,0,0) even though it looked like it was at (9,-10,0)
As far as I remember you can access the rotation properties by the getDRot() function, where getDRot()[0] = rotX, getDRot()[1] = rotY, getDRot()[2] = rotZ. But I am not sure if you can call it on the owner object. From some snippets that I have written a long time ago I call this function on an Actuator. So your ball must have an Actuator, and then you can
import GameLogic
cont = GameLogic.getCurrentController()
moveAct = cont.getActuator("move") # or the name you gave it
rotX = moveAct.getDRot()[0]
#etc
精彩评论