Using Python and Gphoto2 to control the focus of a Canon T3i (EOS 600D)

Turns out it’s really not well documented how to control the focus of a Canon T3i using Gphoto2.  Here’s what I found did the trick:

  1. You have to change the focus from the shutter release to the * button. See this for a description of how.  Otherwise, the camera refocuses every time you take a picture.
  2. You have to be in preview mode (mirror up)
  3. Then you can make the calls to change the focus.
  4. The camera does seem to appreciate being fed commands too quickly, so I threw a bunch of delays in.  I’m not sure what the right timing is…

Here’s my code that did the trick:

def doFocusStep(camera, context, change):
    time.sleep(0.5)
    config = camera.get_config(context)
    focus = config.get_child_by_name('manualfocusdrive')
    focus.set_value(change)
    camera.set_config(config, context)
    time.sleep(0.5)
    return

To flip the mirror up, you just have to capture a preview:

# Set into preview mode
camera_file = camera.capture_preview(context)

You can then make calls like:

doFocusStep(camera, context, "Far 3")

The rest of the program (creating a context and a camera) are fairly well documented.