An update for the original script (2022. 02. 25.)
Now it works on multiple selected cameras.
You can download the new script here.
The original post:
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:
- Copy the code to the Script Editor
- Select all
- Drag to the shelf (Middle Mouse Button)
// I removed the original script to avoid confusion
[…] DOF (MEL) A useful Depth of Field related script (thanks Domonkos for the help) […]
[…] You can find the better version here: True Focus Distance Loacator […]
Exactly what I was searching for, thanks!!
For existing cameras/animations, would we have to manually replace all instances of “persp” for this to work?
Yep. That’s the idea.
And if the camera uses an aim, use the aim shape instead?
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.
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 ;
Is it possible to make this work on the selected camera?
Hi John. I’ll modify the script to work with a selected camera.
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?
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.
An update. Works with multiple selected cameras.