Rotate an object to camera view I try to make this as clear as possible:
Starting Point
I place an object at the center (0,0,0) of the scene. Then I place a camera given a fix radius on a hemisphere around the object (and use Constraints to make it always look at the object) and render the image. I export the object as .stl with y-up and minus-z-forward, in order to open it with another library that uses this different convention.
Problem
I would like to rotate the object in such a way that it would correspond to the view of the rendered image.
Approach
The angles for azimuth and elevation are known. So I calculate the rotation matrix given the two angles (I need a homogeneous rotation matrix, thus appending the 0s and 1).
def angles_to_matrix(phi, theta):
    # phi describes the azimuth
    azi = phi
    # theta describes the inclination angle, thus:
    ele = radians(90) - theta
    rol = 0
    
    e1 = cos(rol) * cos(azi) - sin(rol) * cos(ele) * sin(azi)
    e2 = sin(rol) * cos(azi) + cos(rol) * cos(ele) * sin(azi)
    e3 = sin(ele) * sin(azi)
    e4 = -cos(rol) * sin(azi) - sin(rol) * cos(ele) * cos(azi)
    e5 = -sin(rol) * sin(azi) + cos(rol) * cos(ele) * cos(azi)
    e6 = sin(ele) * cos(azi)
    e7 = sin(rol) * sin(ele)
    e8 = -cos(rol) * sin(ele)
    e9 = cos(ele)
    
    return np.array(((e1, e2, e3, 0), (e4, e5, e6, 0), (e7, e8, e9,0 ), (0, 0, 0, 1)))

The rotation does not look right. &amp; I feel there could be several things going wrong:

Did I miss something concerning the change of axis orientation?
Is it even possible to use the same angles for calculating the sphere coordinates to calculate the rotation of the object?

Do you have any ideas?
Align object to camera
This will give same result as adding an object in camera view, and using ALIGN to VIEW
By default a blender object faces -Y and has Z up. The camera on the other hand looks down -Z with Y up.
Can decompose an objects matrix into its location, rotation and scale, and then recompose.
In this case get the rotation part of the camera matrix world, transformed to -Y forward and Z up then recomposed into the object with its original scale and translation.
import bpy
from mathutils import Vector, Matrix
from bpy import context
from bpy_extras.io_utils import axis_conversion

scene = context.scene
cam_ob = scene.camera
ob = context.object

A = axis_conversion(
        from_forward='Z',
        from_up='-Y',
        to_forward='Y',
        to_up='Z').to_4x4()

loc, rot, scale = cam_ob.matrix_world.decompose()

ob.matrix_world = (
    Matrix.Translation(ob.matrix_world.to_translation()) @
   (rot.to_matrix().to_4x4() @ A) @
    Matrix.Diagonal(ob.matrix_world.to_scale()).to_4x4()
    )