0
$\begingroup$

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. enter image description here

$\endgroup$
1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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)? $\endgroup$ – vndep Sep 10 at 0:41
  • 1
    $\begingroup$ @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. $\endgroup$ – Hikariztw Sep 10 at 2:48
  • 1
    $\begingroup$ 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. $\endgroup$ – Hikariztw Sep 10 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.