1
$\begingroup$

I'm trying to use the python API to get the transformation matrix from world space to camera space. I want to do this by using convert_space() but I don't manage to get the correct transformation matrix and the documentation is also not helpful. Shouldn't it be as simple as that?

m_world_to_camera = camera.convert_space(matrix=mathutils.Matrix.Identity(4),from_space='WORLD',to_space='LOCAL')

The transformation somehow neglects the camera's translation. On the other hand

m_world_to_camera = camera.matrix_world.inverted()

gives me the correct transformation. I could use this instead but I want to figure out what's wrong with my understanding of convert_space().

$\endgroup$

1 Answer 1

1
$\begingroup$

I think the issue with your method is that the convert_space method only converts the rotations, not the translations. To get the complete transformation matrix, you can apply both rotation and translation by concatenating the rotation matrix obtained from convert_space with the camera's world position. Try this:

import bpy
import mathutils

camera = bpy.data.objects['Camera']

rotation_matrix = camera.convert_space(matrix=mathutils.Matrix.Identity(4), from_space='WORLD', to_space='LOCAL')
translation_vector = camera.matrix_world.to_translation()
m_world_to_camera = rotation_matrix @ mathutils.Matrix.Translation(translation_vector)
$\endgroup$
1
  • 1
    $\begingroup$ Yes, that seems like it. Would be nice if the documentation would be more clear about this and state that convert_space neglects the translation. $\endgroup$
    – Marc
    Feb 8, 2023 at 16:28

You must log in to answer this question.

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