DazScript import external script file?

shoei321shoei321 Posts: 113

Simple question -- I've built a library of utility functions located in a file "tools.dsa". I want to include that script from another Daz script so that I can call the functions in it. How do I do this?

I've tried :

import("tools.dsa");  

No error, but my functions aren't available from the calling script. I've dug around DazScript and QTScript docs but can't seem to find an answer. I found the DzScript object but that doesn't appear to be what I need.

Thanks

Post edited by shoei321 on

Comments

  • rbtwhizrbtwhiz Posts: 2,250
    edited December 1969

    void include( String scriptPath )

    Includes the contents of scriptPath in the same context as the calling script. This function should only be called within the global scope of the script; it should not be called within a nested scope and it should not be called inline. As a safeguard against circular references, the script engine keeps an internal list of unique paths for included scripts; per script context, per execution. Each time the function is called, scriptPath is checked against the list to ensure that the path has only been included once within the context of the script.

    Parameters:
    scriptPath - The path of the script to include. The path is assumed to be relative to the ./scripts directory. Absolute paths are also supported.

    -Rob

  • 3dcheapskate3dcheapskate Posts: 2,719
    edited June 2013

    ...but what if the imported DAZ script is in a random DAZ content directory? The code below seems to work, but...

    n=App.getContentMgr().getNumContentDirectories()
    sFile="";
    i=0;
    while (i<=n && sFile=="")
    { s=App.getContentMgr().getContentDirectoryPath(i++)+"/scripts/ScriptName.dsa";
     oNewFile = new DzFile(s);
     if (oNewFile.exists())
      sFile=s;
    }
    if (sFile!="")
    {
     include (sFile);
     // Do more stuff
    }
    else
     MessageBox.warning("Unable to find ScriptName.dsa. Terminating.", "TITLE", "&OK;", "" );

    ...does it fall foul of the "...should not be called within a nested scope and it should not be called inline" criteria?

    (P.S. Of course, the '// Do more stuff' involves calling the functions from the included file)

    Post edited by 3dcheapskate on
  • Richard HaseltineRichard Haseltine Posts: 100,864
    edited December 1969

    An alternative, if you want to execute a script, is to use DzScript - that lest you load a script (DzScript.loadFromFile( name ) ) as an object and modify or execute it. The include command is like the equivalent directive in C or BASIC - it expands the included code and runs it as part of the flow of the script (it's there to avoid duplication of code that needs to be used in multiple scripts without modification).

  • 3dcheapskate3dcheapskate Posts: 2,719
    edited December 1969

    Thanks Richard, using DzScript seems to work fine.
    But now I want to pass parameters to the script, so I use 'Boolean execute (Array args)' as per the DAZ Script 2: DzScript docs:

    
     o_Script = new DzScript(sFile); 
     o_Script.loadFromFile(sFile,true)
     o_Script.execute( ["Parameter-String1","Parameter-String2"])

    I just can't work out how to pick up those parameters in the called script!
    :roll:

  • Richard HaseltineRichard Haseltine Posts: 100,864
    edited December 1969

    getArguments() returns the parameters as an array.

  • TotteTotte Posts: 13,965

    * Thread Necro Warning*

    I know this one is old, but I decided to start to move several of my commonly used functions and objects into separate files, just to test if it works, and works likej a charm, including .dse (encrypted files) as well. 

    // Include libsvar includeDir_oFILE = new DzFile( getScriptFileName() );include( includeDir_oFILE.path()+'/XYZResources/XYZObjects.dse');include( includeDir_oFILE.path()+'/XYZResources/BWCGraphics.dse');

    Needed to be put outside the wrapper

    (function(){			})();

    and no wrapper on the included files, but then it do work. Maybe Rob the Wizard knows why but that is how I interpreted his post ^^up there too.

     

  • functionfunction Posts: 283

    Totte said:

    * Thread Necro Warning*

    I know this one is old, but I decided to start to move several of my commonly used functions and objects into separate files, just to test if it works, and works likej a charm, including .dse (encrypted files) as well. 

    // Include libsvar includeDir_oFILE = new DzFile( getScriptFileName() );include( includeDir_oFILE.path()+'/XYZResources/XYZObjects.dse');include( includeDir_oFILE.path()+'/XYZResources/BWCGraphics.dse');

    Needed to be put outside the wrapper

    (function(){			})();

    and no wrapper on the included files, but then it do work. Maybe Rob the Wizard knows why but that is how I interpreted his post ^^up there too.

    My God! Is this a magic?

    I tried to load another .dse in my script but it doesn't work, cost me few hours to find an answer, at beginning I saw your post but don't understand what you mean,  but, after long time can not find out another answer, I tried yours, and it works!   Unbelievable, what an includeDir comes from.

  • Richard HaseltineRichard Haseltine Posts: 100,864

    Totte said:

    * Thread Necro Warning*

    I know this one is old, but I decided to start to move several of my commonly used functions and objects into separate files, just to test if it works, and works likej a charm, including .dse (encrypted files) as well. 

    // Include libsvar includeDir_oFILE = new DzFile( getScriptFileName() );include( includeDir_oFILE.path()+'/XYZResources/XYZObjects.dse');include( includeDir_oFILE.path()+'/XYZResources/BWCGraphics.dse');

    Needed to be put outside the wrapper

    (function(){			})();

    and no wrapper on the included files, but then it do work. Maybe Rob the Wizard knows why but that is how I interpreted his post ^^up there too.

    Puting it in a wrapper (an anonymous function) limits the scope of all items in the script to the called scopt because the wrapper is anonymous and cannot be called from elsewhere (except, I think, vasriables which are not properly declared, which are universal in scope, but I am not certain that applies) so that they do not bleed through into the calling script. Without the wrapper everything that is at the top level in the script is global through the calling script.

Sign In or Register to comment.