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