def get_computer_vision_camera_transformation_matrix(
    blender_camera, check_scale=True, op=None
):
    """Derive camera transformation matrix from a Blender camera."""

    # Only if the objects have a scale of 1, the 3x3 part
    # of the corresponding matrix_world contains a pure rotation.
    # Otherwise, it also contains scale or shear information
    if check_scale and not np.allclose(tuple(blender_camera.scale), (1, 1, 1)):
        log_report(
            "ERROR",
            "Blender camera contains scale: " + str(blender_camera.scale),
            op,
        )
        assert False

    camera_matrix = np.array(blender_camera.matrix_world)
    blender_camera_rotation_inverse = camera_matrix.copy()[0:3, 0:3]
    blender_camera_rotation = blender_camera_rotation_inverse.T

    # Important: Blender uses a camera coordinate frame system, which looks
    # down the negative z-axis. This differs from the camera coordinate
    # systems used by most SfM tools/frameworks. Thus, rotate the camera
    # rotation matrix by 180 degrees (i.e. invert the y and z axis).
    sfm_camera_rotation = invert_y_and_z_axis(blender_camera_rotation)
    sfm_camera_rotation_inverse = sfm_camera_rotation.T

    rotated_camera_matrix_around_x_by_180 = camera_matrix.copy()
    rotated_camera_matrix_around_x_by_180[0:3, 0:3] = (
        sfm_camera_rotation_inverse
    )
    return rotated_camera_matrix_around_x_by_180
Derive camera transformation matrix from a Blender camera.
photogrammetry_importer/importers/camera_utility.py