This question can be difficult, but I’ll do it anyway.
In Blender, the call “bpy.context.region_data.view_matrix” returns a matrix identical to Matrix obtained with the “bgl.GL_MODELVIEW_MATRIX” when transposed.
ex:
def get_modelview_matrix():
model_matrix = bgl.Buffer(bgl.GL_DOUBLE, [4, 4])
bgl.glGetDoublev(bgl.GL_MODELVIEW_MATRIX, model_matrix)
return model_matrix
print(Matrix(get_modelview_matrix().to_list()).transposed())
<Matrix 4x4 (-0.9935, -0.0763, 0.0844, 0.0000)
( 0.1137, -0.6555, 0.7466, 0.0000)
(-0.0016, 0.7513, 0.6599, 0.0000)
( 0.5504, 0.9845, -19.2413, 1.0000)>
print(bpy.context.region_data.view_matrix)
<Matrix 4x4 (-0.9935, -0.0763, 0.0844, 0.0000)
( 0.1137, -0.6555, 0.7466, 0.0000)
(-0.0016, 0.7513, 0.6599, 0.0000)
( 0.5504, 0.9845, -19.2413, 1.0000)>
That’s good :). Reduces modules. Reduces calculations
But now let’s try the same with the projection_matrix (or perspective_matrix).
In bgl: “bgl.GL_PROJECTION_MATRIX”,
in bpy: “bpy.context.region_data.perspective_matrix”
def get_projection_matrix():
proj_matrix = bgl.Buffer(bgl.GL_DOUBLE, [4, 4])
bgl.glGetDoublev(bgl.GL_PROJECTION_MATRIX, proj_matrix)
return proj_matrix
print(Matrix(get_projection_matrix().to_list()))
<Matrix 4x4 (1.9111, 0.0000, 0.0000, 0.0000)
(0.0000, 1.0938, 0.0000, 0.0000)
(0.0000, 0.0000, -1.0000, -1.0000)
(0.0000, 0.0000, -0.0200, 0.0000)>
print(bpy.context.region_data.perspective_matrix)
<Matrix 4x4 (-1.8711, 0.3886, -0.0034, 1.0136)
(-0.1210, -0.5746, 0.9227, 1.1949)
(-0.1706, -0.8263, -0.5369, 19.4218)
(-0.1706, -0.8262, -0.5369, 19.4410)>
The values are different now. And that is bad :(.
I’m doing something wrong? How do I get the projection_matrix in the bpy module?
Why the values of perspective_matrix in Blender are different from the projection_matrix in OpenGL?