Getting different extrinsic matrix values when running blender through the terminal versus when running using blender-provided console

I am setting the camera position in a scene and rendering an image. The problem was, the image generated using the blender from the terminal (>>blender --background --python myfile.py) and blender-gui were slightly off. I was able to narrow it down to the extrinsic matrix, it has a slight difference in the two for the same input code. How is that possible?

def set_camera( bpy_cam,  angle=pi / 3, W=600, H=500):
    bpy_cam.angle = angle
    bpy_scene = bpy.context.scene
    bpy_scene.render.resolution_x = W
    bpy_scene.render.resolution_y = H

def look_at(obj_camera, point):
    loc_camera = obj_camera.matrix_world.to_translation()
    direction = point - loc_camera
    # point the cameras '-Z' and use its 'Y' as up
    rot_quat = direction.to_track_quat('-Z', 'Y')
    obj_camera.rotation_euler = rot_quat.to_euler()

H = 500
W = 600
bpy_camera = D.objects['Camera']
bpy_camera.location, look_at_point = Vector ((2,-2,2)), Vector((0,0,1))
look_at(bpy_camera, look_at_point)
set_camera(bpy_camera.data, angle=pi /3, W=W, H=H)
bpy.context.view_layer.update() #update camera params
print(bpy_camera.matrix_world)

The intrinsic matrix matches perfectly while the extrinsic does not. The extrinsic matrix when run from blender-python through the terminal command “blender --background --python myfile.py”

<Matrix 4x4 (0.6854, -0.2656,  0.6780,  2.0000)
            (0.7282,  0.2500, -0.6381, -2.0000)
            (0.0000,  0.9311,  0.3647,  2.0000)
            (0.0000,  0.0000,  0.0000,  1.0000)>

I ran the same commands in the blender provided console through the GUI, the extrinsic matrix:

Matrix(((0.7071067094802856, -0.23570232093334198, 0.6666667461395264, 2.0),
        (0.70710688829422, 0.23570220172405243, -0.666666567325592, -2.0),
        (3.3049932568474105e-08, 0.9428090453147888, 0.3333333432674408, 2.0),
        (0.0, 0.0, 0.0, 1.0)))

The values are different. Any possible reason for this? Any known bugs or such?
In case it is useful, blender version 3.3.1 .The python version which came with it 3.10.2. Os: Windows11

That is from the Camera’s position in the default startup file:
Location:
X = 7.37889 m
Y = -6.90579 m
Z = 4.97831 m
Rotation
X = 63.5593°
Y = 0°
Z = 46.6919°

This is the result after running the script a second time, since the camera is now at the coordinates (2, -2, 2).

Thank you, that makes sense, did not know I had to update after changing the camera location

1 Like