[SOLVED] File-Path of a Script

dyodyodyodyo Posts: 42

This has probably been answered hundreds of times, but I can't seem to find the answer:

Is there a built-in Daz function that will tell me exactly where my script is located, file-path-wise (absolute or relative path); it could be where it's supposed to be in the Daz folder system, or on a networked-drive, or anywhere.

I've seen quite a few functions in the DzApp object (I think), but searching through the object reference docs doesn't seem to turn up anything (could be totally wrong, of course).

Thanks,

Post edited by dyodyo on

Comments

  • Richard HaseltineRichard Haseltine Posts: 100,888
    edited April 2019

    There's a global DzScriptContext, http://docs.daz3d.com/doku.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/object_index/scriptcontext_dz , so you can just use name = getScriptFileName(); which you could either parse or use to create a DzFileInfo from which you could obtain the path.

    Post edited by Richard Haseltine on
  • djigneodjigneo Posts: 283

    Something that tripped me up when I was figuring it out was that the script has to be a saved file on disk. It can't be an ad-hoc script in the Script editor, otherwise errors will occur.

    Very obvious in retrospect, but I remember being frustrated for a little bit.

  • dyodyodyodyo Posts: 42

    Thanks Richard!

    This is what I came up with:

    function get_script_path(){    // returns the directory path of this script (without last /)    var file_path = getScriptFileName();    var path = file_path.substring(0, file_path.lastIndexOf("/"));            return path;};

    @djigneo: yep, that got me as well for a while ;)

  • djigneodjigneo Posts: 283

    Instead of doing the substring thing, I would suggest:

    var sPath = new DzFileInfo(getScriptFileName()).path();

     

  • dyodyodyodyo Posts: 42
    djigneo said:

    Instead of doing the substring thing, I would suggest:

    var sPath = new DzFileInfo(getScriptFileName()).path();

     

    Yeah, the substring thing is a little bit hacky and could lead to issues -- using native objects is a much safer / faster. Thanks for the hint :)

Sign In or Register to comment.