# How to drive camera's Y location with same camera's Focal Length

This isn't for an animation, so I'm not inserting keyframes - as I view the model through the virtual camera, I want to experiment with different focal length values in Eevee and Cycles; but to not lose the model, I want to move along the Y-axis specifically to keep the model roughly the same size in the frame.

• Can't you just select the camera then press G + Y to move the camera on the Y axis? While keeping the camera focused on one object by setting the camera to do so in the camera settings? – Nate_Sycro27 Feb 1 '20 at 4:23
• Sure, but that would not be driving the camera's movement with the Focal Length. – HelloHiHola Feb 1 '20 at 4:44
• The local -Z axis is the "business end" of a camera. Related blender.stackexchange.com/questions/107365/… blender.stackexchange.com/questions/128185/… blender.stackexchange.com/questions/130404/… – batFINGER Feb 1 '20 at 12:05
• Last night when I should have been sleeping, I wondered if I'm looking for a scripted expression. Camera's Z; d'oh! Yes, thank you. :) I think my first time making some python code will be ugly enough to offend a lot of people; I'll use another object to control both parts of the camera, Location and FL. I'll find out what the code call part is for Focal Length. I'll drive it with a shapely empty's Rotation, maybe parented to the camera (in view). – HelloHiHola Feb 1 '20 at 14:16

This solution uses only 1 driver, to drive the y value of the camera's transform depending on its focal length.

I used a linear regression calculator online (Search "Linear regression calculator" on your favorite search engine) and I mapped a few values of the focal length and the resulting y value necessary to keep more or less the same objects in frame. I arrived at a value of y (transform) = 0.28 * focal length +1. I assume the exact values will be different from scene to scene but it can be found quite easily using said regression calculator. I used this simple setup :

In order to drive the y transform value, right click in the focal length field and choose "Copy as new driver". Right click in the y transform field of the camera and choose "Paste driver". Right click again in the field, and choose "Edit driver". Then change the evaluation type to "scripted expression" and enter the above formula (in my case 0.28 * lens + 1) :

The result :

Edit :

If you want to use this technique to be always true for its local z-axis, that is to say the forward/backward axis of the camera, follow this :

• Select the camera, Press SHIFT + S then choose "Cursor to Selected"
• Press SHIFT + A then choose "Empty" (whichever type)
• Add a "track to" constraint to the empty, set it to one of the objects of focus in your scene. If you don't have a particular object, just add a new empty and set it somewhere in the middle of your scene. The constraint should look like this :

• Select the camera object, then the empty, and press CTRL + P, choose "Set parent and keep transform" Now when you move the empty around your scene, the camera should follow it exactly and always turn toward the focus object you chose earlier. Do not move the camera directly . If you followed previous steps this should already be working. You will most certainly have to tweak the values a little bit though.

Result :

• Wow. The linear regression tip is really handy. Local -Z might be more general than Global Y? Maybe not, you'd have to define a target... – Robin Betts Feb 2 '20 at 15:21
• Of course ! I updated my answer for the local Z axis – Gorgious Feb 2 '20 at 18:21
• I am delighted. At first I didn't think this was really answerable - then I stumbled and fumbled my way onto another object affecting the Camera Global Y (thru Transform constraint) and that Y location Driving the "lens" mm. There's no Copy/Paste involved, it updates IRT like your update; however, yours answers the initial question! So I'm picking it. Mine answers the later thought, so I could be compelled to post it unless BSE frowns on such things. "Linear Regression." Never heard of it until now. Sincere thanks! – HelloHiHola Feb 3 '20 at 17:39
• @HelloHiHola Alright, I am interested, do post it if you find the time ! A Linear regression is just a way to "reverse engineer" an affine equation of type y = a * x + b when you just have several x values and their corresponding y values. It only works for linear equations though. (no y = a * x ^2 + b * x + c for instance, this one is a little bit more complicated since it can have multiple or non-real solutions) anyways, MATHS are fun ! :) – Gorgious Feb 4 '20 at 10:25