True Focus Distance Locator

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:

  • Create an expression which calculate the distance between the camera plane and the Locator
  • Create an expression which convert 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)

spaceLocator -p 0 0 0;
rename "locator1" "focus_distance_locator";


setAttr "perspShape.aiEnableDOF" 1;
setAttr "perspShape.depthOfField" 1;
setAttr "focus_distance_locator.scaleZ" 10;
setAttr "focus_distance_locator.scaleX" 10;
setAttr "focus_distance_locator.scaleY" 10;


expression -s "perspShape.focusDistance = 0;\nperspShape.focusDistance = focus_distance_locator.translateX;\nperspShape.focusDistance = focus_distance_locator.translateY;\nperspShape.focusDistance = focus_distance_locator.translateZ;\nperspShape.focusDistance = persp.translateX;\nperspShape.focusDistance = persp.translateY;\nperspShape.focusDistance = persp.translateZ;\nperspShape.focusDistance = persp.rotateX;\nperspShape.focusDistance = persp.rotateY;\nperspShape.focusDistance = persp.rotateZ;\nfloat $camFoc[] = `camera - q - worldCenterOfInterest persp`;\nfloat $camPos[] = `camera - q - position persp`;\nvector $directionVector;\nvector $one = <<$camFoc[0], $camFoc[1], $camFoc[2] >>;\nvector $two = <<$camPos[0], $camPos[1], $camPos[2] >>;\nvector $final = ($two - $one);\n$directionVector = unit($final);\nfloat $p1X=`getAttr persp.translateX`;\nfloat $p1Y=`getAttr persp.translateY`;\nfloat $p1Z=`getAttr persp.translateZ`;\nfloat $p2X=`getAttr focus_distance_locator.translateX`;\nfloat $p2Y=`getAttr focus_distance_locator.translateY`;\nfloat $p2Z=`getAttr focus_distance_locator.translateZ`;\nvector $p1=<<$p1X, $p1Y, $p1Z >>;\nvector $p2=<<$p2X, $p2Y, $p2Z >>;\nfloat $d_result; $d_result = ($directionVector.x * ($p2.x - $p1.x) + $directionVector.y * ($p2.y - $p1.y) + $directionVector.z * ($p2.z - $p1.z)) / sqrt($directionVector.x*$directionVector.x + $directionVector.y*$directionVector.y + $directionVector.z*$directionVector.z);\n$d_result = abs($d_result);\nperspShape.focusDistance = $d_result;" -o perspShape -ae 0 -uc all ;


connectAttr -f perspShape.focusDistance perspShape.aiFocusDistance;


expression -s "perspShape.aiApertureSize = perspShape.focalLength/10/perspShape.fStop/2;" -o perspShape -ae 0 -uc all ;

Cheers, D

11 comments

      • 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.
      https://mathinsight.org/distance_point_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:

WordPress.com Logo

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

Google photo

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

Twitter picture

You are commenting using your Twitter 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.