How to get local position and rotation?

I need to get position and rotation of a object relatively to it’s parent. First oddity, I faced to, was that in blender local coordinates is something diferent, than coordinates from parent’s center, but seems like coordinates from world at the moment of linking. Why is it so?

Anyway. I wrote this script to get local rotation. It works, but maybe there is a better way to do it?


import Mathutils

def getworldaxis(obj):
	ornt = obj.worldOrientation

	clmn0 = ornt[0]
	clmn1 = ornt[1]
	clmn2 = ornt[2]

	matrix1 = Mathutils.Matrix([clmn0[0],clmn1[0],clmn2[0]],[clmn0[1],clmn1[1],clmn2[1]],[clmn0[2],clmn1[2],clmn2[2]])

	matrixEuler= matrix1.toEuler()
	
	return matrixEuler

def getlocalaxis(obj):
	parent = obj.parent
	
	objaxis = getworldaxis(obj)
	parentaxis = getworldaxis(parent)
	
	axx = objaxis[0] - parentaxis[0]
	axy = objaxis[1] - parentaxis[1]
	axz = objaxis[2] - parentaxis[2]
	
	localaxis = Mathutils.Euler( axx , axy, axz)
		
	return localaxis

And now i also need local position, considering parent’s rotation. How to get it? Maybe someone was faced with a similar task?

localPosition and localOrientation

refer to http://www.blender.org/documentation/249PythonDoc/GE/toc-GameLogic-module.html

As I said, that’s brings global position and orientation at the moment of linking.

ok, now I see what you mean.
I had the same problem with scaling in my SaveLoader Library.

My solution was to unparent and reparent the objects. You should avoid this for armature parenting, vertex parenting and bone parenting.

Here is a small test script:


def reparent(cont):
 parent = cont.owner.parent
 if parent != None:
  cont.owner.removeParent()
  cont.owner.setParent(parent)
 
def printLocalPosition(cont):
 print "before", cont.owner, cont.owner.localPosition
 reparent(cont)
 print "after ", cont.owner, cont.owner.localPosition

Apply printLocalPosition to the module controller of a child and you will see the difference (as long as the parent is not at [0,0,0]).

I think this is a bug in the BGE.

Looks like it works, thank you, Monster!