Set Focal Distance script
I know what some of you are thinking: "Another set focus script? But I already have like five of them!"
In my time using DAZ Studio, I've found several different scripts on the web that purport to automate setting a camera's focal distance for renders making use of depth of field. Problem is, every last one of them calculates the focal distance incorrectly. They all compute the length of the direct vector from the camera to the selected target. Unless the target is located exactly along the camera's facing vector, this will always result in back-focusing the camera (often by considerable amounts - even meters or more). The correct way to calculate the focal distance is to determine the length of the vector from the camera to a plane that intersects the target location and is perpendicular to the camera's facing vector.
Additionally, these scripts typically get the location the the target itself incorrect. Some simply use the raw target location. While this is fine for objects that are a single point (such as Studio's Null object), it's almost never what you want for setting the focal distance for any object with volume. For example, the object origin of most DAZ figures is down just below their feet. This is great for adjusting a figure to look like it is standing on a surface, but for setting focal distance? Not so much... Other scripts take the center location of the object's bounding box. This also leads to inaccurate focal distances. Take the eyes of a figure, for example. The center of the bounding box is inside the eye, not on the eye surface. Using the centroid results in back-focusing by around a centimeter. If you selected the head itself instead of one of the eyes, you'd end up back-focusing by several centimeters. If the camera is far away, this might not be noticeable, but if you're doing a close-up portrait, where DoF is much narrower, that translates into a pretty huge critical focus error.
For a long time I've been manually eyeballing adjustments to focal distance in the viewport. But this is tedious and inaccurate, so I finally broke down and wrote a new script to set focal distance for Studio. This script uses the correct distance calculation, and only allows selection of Null objects as the target. This forces you to ensure the focal point is really where it should be. My script also supports Studio's undo function.
Basic steps to use:
Setup:
1) Create a Null object
2) Parent the Null to the object/bone closest to your desired point of critical focus (typically the left or right eye bone of a figure, a weapon, etc.)
3) Adjust the position of the Null to the point of critical focus (e.g., the front surface of the eye, the barrel opening of a gun, etc.)
Application:
1) Select the Null object in the Scene list
2) Set the active viewport's camera to the camera you want to set focus on
3) Run this script
(For ease of use, find this script in your runtime in the content library, right-click and select "Create Custom Action". You can then use the Edit->Customize menu to add the action to one of your toolbars or menus to make it simple to invoke from anywhere without having to look in the content library each time.)
I've been testing/polishing up this script for public release over the past few days, but please leave a reply in this thread if you encounter any bugs/issues with it.
The script (Dropbox link): Set Focal Distance.dsa (Updated 2017-07-21 with new link to correct for Dropbox breaking all old public share links.)
Comments
Thanks. I'd given up on DOF because it was a PITA to setup and like you said, scripts didn't really get it right anyway. And I like that you let the user directly control the target, takes much of the guesswork out of it. :D
Interesting script. You know, you can package this up and host it on ShareCG.com as a freebie, and then provide a link to the freebie's ShareCG in the freebie forums here.
Thank you very much for the script! Will definitely give it a try.
Updated original post with a Dropbox link to the script, rather than the inline code block that required manually copying and pasting into a file.
Thanks cwichura. Works great, thanks for posting.
Quickie render, first time trying script out:
i quickly cooked up a null node "shooter"
http://www.daz3d.com/forums/discussion/43694/
the camera was located where the left figure head is
i selected the right figure's head
ran the script and bam we have a null node on the nose ! ( and on the nose )
( then i parented a null node of the left figure's fingernail tip and used mcjAutoLimb2014 to auto-position the arm )
Thanks cwichura, it's very useful.
Until now I used another script (called "distance" and part of the omNode tools) and I see that there are differences in the measures between the old and the new scripts ranging from zero (in case of the null placed on the camera optical axis) to some cm (for a close portrait). With your explanation I understand the reason for these discrepancies.
Even few cm in the focal distance can make a big difference in scenes with small DOFs.
I have a little request: could you add a "cancel" button in the dialog window so that the scripts can be used as an "informative" tool without actually changing the camera focal distance?
I know it's possible to undo the change, but when making several exploratory measures it could be more handy to have an option to just see the distance without making changes in the previously set focal distance.
It's an interesting idea, abramo, but I believe the primary use case for most people will be to set the focal distance and move on. Yes, knowing the focal distance is useful when determining what the DoF will be for a given f/Stop, but you can also see that visually in Studio with its fore and aft focus lines and you need the camera to be set to the correct focal distance for those to work. So I think I will leave it as is for now, since the ability to undo the change is there.
Very useful! Thanks a lot. :)
Many thanks. DOF was starting to drive me crazy. Now I can get on with learning about posing/textures/lighting/etc instead of worrying about exactly how far the camera is from the subject (and testing/tweaking/testing/tweaking...)
That dropbox link just takes you to a page of text, how do you download it as a .dsa file?
CHEERS!
Same problem here :)
Same problem here :)
Copy the text and paste it into an editor, then save it setting the format to all files (*.*) and typing the .dsa extension.
Open your favorite text editor (notepad will work). Then go to that page of text. Control-A, Control-C. Then in the text editor, if you aren't at a blank file, create a new one. Then paste (control-v) the copied text from the Dropbox page. Then Save As...Set Focal Distance.dsa. That should then save it as script.
Method 2...
The select and copy parts are the same. In DS open the Script Editor tab...and paste the copied text from the Dropbox page. Then save.
Thanks fellas,
You're lifesavers!
CHEERS!
The paste and save worked. It's now a recognised .dsa file.
CHEERS!
Alternate option to save the file: just go up to your browsers options and "save page as". Set the file type to "all files" and remove the .txt suffix. Voila, one .dsa.
And thank you for the script! I've been doing DOF manually and just kind of winging it, so this is going to be very useful.
I've found that while that usually works, Dropbox has a habit of mucking up things...usually linebreaks or wrapping issues. It seems the copy and paste method is more reliable at Dropbox, at least to me.
Awessome ... much appreciated!! Thank you!
Oh my, THIS saved my life!!!!!!! Thanks so much!
Very easy to use and extremely useful. Thank you for this smart script!
Quickie png icon for the handy script. Thanks!
Stupid question: how do I create a Null Object? :)
From the Create menu, choose New Null.
Hi cwichura,thans for the script!!!
im a totally rookie in DAZ , don't understand what's the "null object" ?how to create it? Plese forgive me and teach me more detail LOL...
For example, in the camera view I select the left eye and run the script,then there's a window jumped out and said Please select a Null object to focus on.....
Great script, thanks!
Ok, this post contains two scripts from ten years ago, @cwichura , @mCasual , shall we renew and combine them?
Here I tried, combined mCasual's mcjclosestvertex.dsa with cwichura's Set Focal Distance.dsa, to make the life easier. Now user needn't to create and move null, all automatically.
The usage is:
1. viewport set to Camera view;
2. select a focus target; (smaller is more accurate, such like an eye, nose, finger, etc.)
3. run this script, done. Will pop up the new Focal distance value.
This script turns DOF On, otherwise the Focal Distance is meaningless. This script adjusts F/Stop a little bit according to the Focal distance changes for blur effect.
However, @mCasual , the mcjclosestvertex.dsa seems has problem now, for some bones such like G8 or G3 head, the 'getCachedGeom' thinks it is no face bone. As a compromise, this combined script uses the middle position of those 'no face bone' instead of closest vertex, the result is close.
Finally, this Set Focal Distance v2 script, plus Cayman Studios' Perspective Control for DAZ Studio which can adjust Focal Length together with camera position, can let anyone to become a camera expert immediately. Have fun.
Just wondering if maybe I'm missing something? Everytime I run the script, I get the pop-up message to "Please select a null". I've even gone as far as creating a null object, placing it where I want it, and making sure the camera is pointing at it. Same pop-up.
Thank you very much for your hard work.