Set Focal Distance script

cwichuracwichura Posts: 1,042
edited July 2017 in Freebies

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.)

Post edited by cwichura on

Comments

  • SemicharmSemicharm Posts: 376
    edited December 1969

    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

  • Tramp GraphicsTramp Graphics Posts: 2,412
    edited July 2014

    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.

    Post edited by Tramp Graphics on
  • Mustakettu85Mustakettu85 Posts: 2,933
    edited December 1969

    Thank you very much for the script! Will definitely give it a try.

  • cwichuracwichura Posts: 1,042
    edited December 1969

    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.

  • none01ohonenone01ohone Posts: 862
    edited December 1969

    Thanks cwichura. Works great, thanks for posting.
    Quickie render, first time trying script out:

    Script-Test.png
    1024 x 1024 - 652K
  • mCasualmCasual Posts: 4,607
    edited July 2014

    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 )

    closest.jpg
    748 x 748 - 249K
    Post edited by mCasual on
  • edited December 1969

    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.

  • cwichuracwichura Posts: 1,042
    edited December 1969

    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.

  • Sam_16471Sam_16471 Posts: 57
    edited December 1969

    Very useful! Thanks a lot. :)

  • AenesidemusOZAenesidemusOZ Posts: 2
    edited December 1969

    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...)

    DOF_Test_1_-_50mm_f4.0_.jpg
    700 x 910 - 11K
  • RogerbeeRogerbee Posts: 4,460
    edited December 1969

    That dropbox link just takes you to a page of text, how do you download it as a .dsa file?

    CHEERS!

  • StrixowlStrixowl Posts: 301
    edited December 1969

    Rogerbee said:

    That dropbox link just takes you to a page of text, how do you download it as a .dsa file?

    CHEERS!

    Same problem here :)

  • Richard HaseltineRichard Haseltine Posts: 101,346
    edited December 1969

    Strixowl said:
    Rogerbee said:

    That dropbox link just takes you to a page of text, how do you download it as a .dsa file?

    CHEERS!

    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.

  • mjc1016mjc1016 Posts: 15,001
    edited December 1969

    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.

  • RogerbeeRogerbee Posts: 4,460
    edited December 1969

    Thanks fellas,

    You're lifesavers!

    CHEERS!

  • RogerbeeRogerbee Posts: 4,460
    edited December 1969

    The paste and save worked. It's now a recognised .dsa file.

    CHEERS!

  • LuckBeLuckBe Posts: 93
    edited December 1969

    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.

  • mjc1016mjc1016 Posts: 15,001
    edited December 1969

    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.

    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.

  • 3dbug3dbug Posts: 67

    Awessome ... much appreciated!! Thank you!

  • khyrkatkhyrkat Posts: 4

    Oh my, THIS saved my life!!!!!!! Thanks so much!

  • UnseenUnseen Posts: 654

    Very easy to use and extremely useful. Thank you for this smart script!

  • RAMWolffRAMWolff Posts: 10,212

    Quickie png icon for the handy script.  Thanks!  

     

    Set Focal Distance.png
    182 x 182 - 26K
  • Stupid question: how do I create a Null Object? :)

  • Tim NTim N Posts: 193

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

  • MKCMKC Posts: 23

    Great script, thanks!

  • functionfunction Posts: 283
    edited April 29

    mCasual said:

    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 )

    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.

    zip
    zip
    Set Focal Distance_v2.zip
    252K
    Set Focal Distance_v2.png
    1016 x 1030 - 251K
    Post edited by function on
  • vikl1964vikl1964 Posts: 28

    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.

  • Silver DolphinSilver Dolphin Posts: 1,609

    Thank you very much for your hard work.

Sign In or Register to comment.