True Focus Distance Locator (update)

An update for the original script (2022. 02. 25.)
Now it works on multiple selected cameras.

You can download the new script here.

Cheers, D

The original post:

Hi there,

Finally. Here it is. The “true” Distance From Camera DOF locator. Yay.
It works as it should. It provides distance data from the camera plane (point to plane) not a simple point to point calculation (which is wrong).

Thank you, Inki for the great help.

What the script does:

  • Creates an expression which calculates the distance between the camera plane and the Locator
  • Creates an expression which converts the persp camera Focal Length and F Stop values to the Arnold specific Aperture size value
  • DOF is on in Viewport 2.0 and for Arnold
  • Scale up the Locator a bit

How to use:

  1. Copy the code to the Script Editor
  2. Select all
  3. Drag to the shelf (Middle Mouse Button)
// I removed the original script to avoid confusion

Cheers, D


      • You can do that also.
        In that case you don’t need to create the locator (first two lines).
        Replace the name of persp, perspShape and the focus_distance_locator for the already existing ones.
        By the way because the camera and aim always aiming at its locator through the center of the camera, you don’t need the first expression at all.
        You can directly connect the camera’s Center Of Interest attribute to Focus Distance and to aiFocusDistance.

        For example:
        setAttr “cameraShape1.aiEnableDOF” 1;
        setAttr “cameraShape1.depthOfField” 1;
        setAttr “camera1_aim.scaleZ” 10;
        setAttr “camera1_aim.scaleX” 10;
        setAttr “camera1_aim.scaleY” 10;
        connectAttr -f cameraShape1.centerOfInterest cameraShape1.aiFocusDistance;
        connectAttr -f cameraShape1.centerOfInterest cameraShape1.focusDistance;
        expression -s “cameraShape1.aiApertureSize = cameraShape1.focalLength/10/cameraShape1.fStop/2;” -o cameraShape1 -ae 0 -uc all ;

  1. Awesome work, I searched all over the internet dint found anything accurate, everyone is using a distance tool which is not accurate at all.
    Would it be possible to share the logic behind the script for someone like me from non-scripting background to understand what’s happening under the hood?

    • Hi,
      It’s really just a little bit of linear algebra: Distance from point to plane.

      In Maya, there is a special attribute for the camera called worldCenterOfInterest (that’s a pont in space where the camera „look at”).
      You can use that and the actual position of the camera to get a directional vector.
      Also there is the position of a locator (the plane where the focus will be, parallel to the camera’s plane).
      So that’s the main thing.
      Than calculate the distance’s absolute value (linked formula) .

      And there is one last thing, the Aperture Size calculation.
      Arnold’s ApertureSize (radius in cm) = focalLength/10/fStop/2

      The tricky part was really to get the correct syntax, so it can be used in an expression.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.