8
$\begingroup$

How would I get an object's rotation and set that as another's velocity? Like if I had Object A, that has the rotation data, and Object B, the object to be moved. How would I get that working in python?

Because of some confusion, here's some of the code:

Gun = bpy.data.objects["Gun"]
rotationOfGun = Gun.globalOrientation
$\endgroup$
  • 1
    $\begingroup$ So you want to get an object's rotation vector and use that to set another object's velocity? $\endgroup$ – Daniel Pendergast May 30 '13 at 0:30
  • $\begingroup$ That's exactly what I need to do. $\endgroup$ – V360 May 30 '13 at 0:32
  • $\begingroup$ I assume that you're using a rigidbody? $\endgroup$ – Daniel Pendergast May 30 '13 at 0:34
  • $\begingroup$ no, just a Dynamic $\endgroup$ – V360 May 30 '13 at 0:42
6
$\begingroup$

The method for accessing an object's rotation varies depending upon which rendering engine you are using.

In either Blender-Internal or Cycles, you can access an object's rotation by using the Python reference bpy.context.object.rotation_euler or just object.rotation_euler. In your script, just call that reference and set your other object's velocity based on that.

In the Blender Game Engine, you need to instead use object.worldOrientation.to_euler() to retrieve the object's rotation. For example:

from bge import logic
cont = logic.getCurrentController()
obj = cont.owner
rotation = obj.worldOrientation.to_euler()
print (rotation.z)
$\endgroup$
  • $\begingroup$ It says this when I try to run it: Traceback (most recent call last): File "<blender_console>", line 1, in <module> TypeError: 'bpy_prop_collection' object is not callable $\endgroup$ – V360 May 30 '13 at 1:22
  • $\begingroup$ Did you type ...euler or ...euler() ? $\endgroup$ – Gwen May 30 '13 at 1:35
  • 2
    $\begingroup$ This is game-engine you should not use bpy! $\endgroup$ – dfelinto May 30 '13 at 2:40
  • 1
    $\begingroup$ @V360 I caught my mistake. I had assumed you were using Cycles, but the commands are different in the Game Engine. Try using obj.worldOrientation and let me know if that works. $\endgroup$ – Gwen May 30 '13 at 4:47
  • 1
    $\begingroup$ @V360 Check the code snippet I added in. It works for me in Blender 2.66a. $\endgroup$ – Gwen May 30 '13 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.