4
$\begingroup$

Here is my setup:

  • I have the default cube
  • I make a sphere and move it y+4 (now at 0,4,0)
  • I parent the sphere (child) to the cube (parent)
  • the sphere location is [0,4,0] in local coordinates. It's the same for global coordinates. (This is a simplified example.)

I want to programmatically get the sphere's coordinates in the global space.

Scouring the web, I have found the following general formula for getting global coordinates from local ones:

sphere.matrix_world * sphere.location

In the above example, if I plug that into the python console, it produces [0,8,0]. Why 8? It should be 4, no? Is this not the correct formula? If I make the example more complex (by rotating the cube, for example) I still get this 'doubling' effect. [0,3,0] becomes [0,6,0]. What is the correct python expression to get the correct global coordinates of the parented sphere?

$\endgroup$
4
$\begingroup$

The formula is

default_cube.matrix_world * sphere.matrix_basis

You can directly get the global coordinates (including constraints) of the sphere from

sphere.matrix_world.translation

See Object.matrix_basis, mathutils.Matrix

$\endgroup$
  • 2
    $\begingroup$ There's also a more or less equal alternative to Object.matrix_world.translation: to_translation(). In contrast to translation, there's always a copy made and modifications to the Vector object won't affect the original matrix. $\endgroup$ – CoDEmanX Jan 7 '15 at 9:04

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.