# Blender 2.8 (python) - how do I get the vector of the direction the camera points

In python, how do I calculate/find/retrieve the XYZ vector from the location of the camera and through the midpoint of the camera view?

In the example below the camera is at location (7.4, -6.9, 5.0) and points at the origin (0,0,0); therefore the vector should be (-7.4, +6.9, -5.0)

I suspect the camera's location and rotation parameters hold the answer, but the I can't figure out the calculations. For example, in moving the origin there is clearly movement along the Y axis, but the rotation on the Y-axis is zero so I don't know how to do it. Since a clear rotation camera (0,0,0) point down z-axis.

So the vector should be $$camera\ rotation\ matrix\times Vector(0,0,-1)$$

In code:

v = Vector((0,0,-1)) #Down vector
v.rotate(camera.rotation_euler)

v #this vector


Except

Keep in mind that camera can have another transform by parent offset and some other modifier. If the camera rotation is not pure, you will need to apply the transform to that rotation as well.

• How would this work as a generalized algorithm, rather than specifically pointing at the origin? For example, what if the camera is pointing at (2,8,-5)? – vndep Sep 10 at 0:41
• @vndep it doesn't matter where the camera is and where it's point at. Since camera doesn't allow apply any transform. The rotation of camera is same as rotation of the camera pointing vector if camera setting doesn't change. You can test it if you manually put camera in some places and use . and ctrl+alt+0 to point at some non-origin object to test it. – Hikariztw Sep 10 at 2:48
• I just test it second ago and it is correct. It it not a generalized algorithm but just how camera is treated in blender. And also since v is an unit vector, the pointing vector will be an unit vector as well. You can normalize the difference vector between cam and obj to see the same vector. – Hikariztw Sep 10 at 2:52