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


7 thoughts on “True Focus Distance Locator

      1. 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 ;

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 )

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.