DSLR focus control with gphoto2

DSLR focus control with gphoto2

If you read the documentation on gphoto2 regarding the focus control (“manual” focus as they call it) it mentions that focus control only works while liveview mode is on, and that seemed to be sufficient for Canon, but it turned out that for Nikon cameras there was more to it than just liveview.

First the necessary disclaimer though: I’ve only tried the Nikon D5100 here and Canon 550D and 600D, so YMMV.

But let’s start with liveview (or preview) mode itself first – this one is controlled by “/main/actions/viewfinder” setting in both Nikon and Canon.

Essentially it’s name is “viewfinder mode”. That’s how it’s actually called by GPhotow – when you get list of camera properties via GPhoto2 on Canon it’s name is “Canon EOS Viewfinder”, and on Nikon “Nikon Viewfinder” (key is same – “/main/actions/viewfinder” – on both). So let’s not call it liveview/preview mode anymore – it’s viewfinder mode hereafter.

Next thing to be aware of is that this mode is switched off when gphoto2 releases the camera. So running command-line tool “gphoto2” with parameters “–set-config /main/actions/viewfinder=1” is not the right way to do it – gphoto2 will enable viewfinder mode, then since no more commands are there it’s going to quit, and on quit it will release the camera, which will disable viewfinder mode.

So just run gphoto2 in interactive mode (if you use gphoto2 in command line – if you use gphoto2 library and call it’s API methods you’re fine, just be aware that doing camera release – actually the method name is gp_camera_exit – will disable viewfinder mode).

Also the viewfinder mode will disable itself after some seconds of camera inactivity (you’ll hear the clicking when it does that). Maybe this is configurable somewhere in the menus, but I didn’t find out where, and in general one should just be aware of it behaving that way by default.

Ok, so we run “gphoto2 –shell” to start gphoto2 command-line tool in interactive mode, do “set-config /main/actions/viewfinder=1”, what’s next?

Well, one more sanity check thing to do is to make sure on your lens the physical switch from auto to manual focus is set to auto – that enables your DSLR body to control the focus, but doesn’t necessarily means autofocusing. Which might be confusing for some.

Then on Canon I could just set “/main/actions/manualfocusdrive” to one of those “Near 1/2/3 or Far 1/2/3” values and that got focus engine rotate the lens accordingly. But not on Nikon.

Experimentally I’ve found out that on Nikon I also have to set these (works for me in this specific order):

  • /main/actions/viewfinder=1 (enable viewfinder mode first, as you already know)
  • /main/capturesettings/focusmode2=MF (selection) – don’t bother with
  • /main/capturesettings/liveviewaffocus=Single-servo AF
  • Then control focusing engine via /main/actions/manualfocusdrive (set to 1000/-1000 to see clear rotation)

Again, this is what I had found out on Nikon D5100 specifically, but I assume it’s more/less same for all or most Nikon DSLRs.

That’s it. Now go and programmatically control that focus on your Nikon 🙂

Source: DSLR focus control with gphoto2 | M.V.M-n.

DSLR focus control with gphoto2 was last modified: September 6th, 2021 by Jovan Stosic

Leave a Reply