Hello all,
Having spent quite some time trying to find out how to get camera's projection matrix from a script, I resorted to post the question on this forum.
Is it the only option to built myself, taking necessary data from camera parameters?
I see that one part of the information needed for this matrix  screen resolution  is in scene properties. Is it the cause why camera doesn't have such a property?
Using Blender 2.57.1
thanks in advance,
Maxym
Camera's projection matrix
Moderators: jesterKing, stiv

 Posts: 0
 Joined: Wed Jul 06, 2011 9:42 am
Recently I had the same problem. Here is a scratch code that produces such a matrix in Blender 2.58:
Obviously it can be optimized but for now it was sufficient for me.
Notice that v is a 3d vector and RT is a 4x4 matrix. However the operation is carried out because it is treated as an affine transformation.
Code: Select all
import bpy
from mathutils import *
from math import *
# Getting width, height and the camera
scn = bpy.data.scenes['Scene']
w = scn.render.resolution_x*scn.render.resolution_percentage/100.
h = scn.render.resolution_y*scn.render.resolution_percentage/100.
cam = bpy.data.cameras['Camera']
camobj = bpy.data.objects['Camera']
# Some point in 3D you want to project
v = Vector((1,2,3))
# Getting camera parameters
# Extrinsic
RT = camobj.matrix_world.inverted()
# Intrinsic
C = Matrix().to_3x3()
C[0][0] = w/2 / tan(cam.angle/2)
ratio = w/h
C[1][1] = h/2. / tan(cam.angle/2) * ratio
C[0][2] = w / 2.
C[1][2] = h / 2.
C[2][2] = 1.
C.transpose()
# Projecting v with the camera
p = (v * RT) * C
p /= p[2]
Notice that v is a 3d vector and RT is a 4x4 matrix. However the operation is carried out because it is treated as an affine transformation.