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?


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() 
  • $\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$ – alpha bravo Jan 7 '17 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$ – alpha bravo Jan 8 '17 at 22:35

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.