Set object's orientation equal to another object's orientation

Hello, I’m just starting with Blender’s python scripting. I have tried making a script to do the previously mentioned but it doesn’t seem to work:


import bge
import GameLogic
cont = bge.logic.getCurrentController()
own = cont.owner

rot=own.worldOrientation

scn = GameLogic.getCurrentScene()
obl = scn.getObjectList()
object = obl['OBCube'] #object to orient
object.worldOrientaion=rot

I tried messing around with “getOrientation” and “SetOrientation” but I can’t seem to get the syntax right.

Be aware, the rotations and positions of gameobjects are “references” - meaning they don’t copy the value, but instead access the value every time they’re used. This means that you should copy the positions:
Also, you’re using deprecated (2.46) code (GameLogic, getObjectList() and OB prefix before object names), this should work;


import bge

cont = bge.logic.getCurrentController()
own = cont.owner

scene = bge.logic.getCurrentScene()

other = scene.objects['Cube']

rot = own.worldOrientation.copy()

other.worldOrientation = rot


Thanks man! now it works

By the way, is there a way to convert from quaternions to eulers, and then back to quaternions?

Have a look at Rigify. After generating a rig from the metarig, go into the Text Editor and click on the scripts list. You’ll find rig_ui.py and in there is an example of what you’re talking about. It uses bones, yes, but the principals are the same.

You’re looking for the section called Visual Transform helper functions. It shows how to match translation, scale and rotation.

In most cases, the mathutils classes (Vectors, Eulers …) have methods like “to_matrix()” and so forth. Reading the documentation will show you these methods:
http://www.blender.org/documentation/blender_python_api_2_63_13/

As a side note, mxmstr, in your original code you misspelled ‘worldOrientation’ as ‘worldOrientaion’. That’s probably why it didn’t work. Be sure to check the console to help you narrow down errors. You also don’t need the GameLogic module anymore - the bge.logic module replaces it for Blender 2.6x.

Thanks dudes. This was very helpful.