0
$\begingroup$

I am trying to find the coordinates of Object1 in the local coordinates of Object2. Neither Object1 nor Object2 are the object that the python code is attached to.

I can find the location of the 3D cursor relative to my active object using: co_find = context_act.matrix_world.inverted() * context.sce.objects['Cube001']

But I do not know how to find the location of Object1 in the local coordinate system of Object2.

Does anyone know how to do this?

$\endgroup$

1 Answer 1

1
$\begingroup$

To get the matrix of ob1 in the local space of ob2

mwi = ob2.matrix_world.inverted()
m_local2ob2 = mwi * ob1.matrix_world

loc = m_local2ob2.to_translation() # local loc
loc, rot, scale = m_local2ob2.decompose() 
$\endgroup$
2
  • $\begingroup$ batFINGER, Thanks very much for your help. I'm running my script in the game engine. When I move Obj2, then I want to calculate the loc, rot, scale (using your code). Your code calculates the initial loc, rot, scale, but when I move my Obj2, the loc, rot, scale do not update with the new values. Obviously there is nothing wrong with your code - I just don;t know how to update the values when my character (ob2) moves. Do you have any suggestions? Thanks so much. $\endgroup$ Jan 7, 2017 at 4:09
  • $\begingroup$ I've found a solution to this second problem. I've used camera_to_world, which is a bge function. Seems like bpy functions don't update in the game engine. $\endgroup$ Jan 8, 2017 at 22:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .