1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Is there a way to call a script from within a script?

Discussion in 'Alibre Script' started by albie0803, May 16, 2019.

  1. albie0803

    albie0803 Alibre Super User

    What I am after is the last action of a script is to ask "Do you want to run X script?" and have it start if required.
     
  2. ajayre

    ajayre Alibre Super User

  3. idslk

    idslk Alibre Super User

    Hello albie0803,

    i haven't tried this with alibre script (it's possible in python...)
    what would you accomplish with that?

    Edit: Andy was faster...

    Regards
    Stefan
     
  4. albie0803

    albie0803 Alibre Super User

    I have my little gear creator script. At the end of it I would have it ask "Do you want to add a key way?" A yes response would trigger the next script.

    I've had a little play after looking at the link and the call seems to expect the script being called to be in the python folder.
     
  5. NateLiqGrav

    NateLiqGrav Alibre Super User

    I've thought about this a bit. Normal methods from google search didn't work. However I think If you build each script like function in a module then in theory you could import the file and call their functions. I was going to test but haven't yet.

    Edit: I should have looked at what Andy linked. Modules are in the answer there.
     
    Last edited: May 17, 2019
  6. albie0803

    albie0803 Alibre Super User

    It just said "Couldn't find module". I had it in the same directory as the one I was calling it from and the example I saw online had both files in the python directory.
     
  7. NateLiqGrav

    NateLiqGrav Alibre Super User

    Actually I tried execfile and it seems to run a script just fine but it seems you have no control over it from this script (except how the script reacts to current variables).
    Code:
    import os
    #replace OtherScript with name of script saved in the same folder as this script.
    myscript = os.path.join(ScriptFolder,'OtherScript.py')
    execfile(myscript)
    
    # if this script is not saved then ScriptFolder will be null and the path will have to be hardcoded instead.
    #execfile(r"C:\Users\Gamer\Documents\Alibre Script Library\Scripts\ColorNew.py") 
    Making and importing a module would give much more control but is much more difficult so I have removed my previous post.
     
    Last edited: May 18, 2019
  8. NateLiqGrav

    NateLiqGrav Alibre Super User

    NOTE OF CAUTION: execfile() will act as if the other script was copied and pasted exactly right there in your script.
    It will see your variables and can change them. This could easily mess up one or both script from running the way they did before.
    You can limit the variables by providing execfile() with empty dictionary or limited dictionary but that has issues as well.
    For example this is empty:
    Code:
    execfile(r"C:\your\path\here\OtherScript.py",{})
    But that blocks many build in AlibreScript things so you need to explicitly provide them.
    For example here are only a few of the things the script might need to run correctly:
    Code:
    execfile(r"C:\your\path\here\OtherScript.py",{'Windows':Windows, 'Units':Units, 'UnitTypes':UnitTypes, 'sys':sys, 'CurrentAssembly':CurrentAssembly, 'CurrentPart':CurrentPart})
    Also note: I'm not an expert in python. I'm learning as I go so I have before and will in the future get things wrong.
     
  9. ajayre

    ajayre Alibre Super User

    METHOD 1

    ModuleA.py:
    Code:
    def DoSomething(value):
      return value + 1
    
    ModuleB.py (in the same folder as ModuleA.py):
    Code:
    import imp
    
    ModuleA = imp.load_source('ModuleA', ScriptFolder + r'\ModuleA.py')
    
    print ModuleA.DoSomething(5)
    
    Andy
     
    NateLiqGrav likes this.
  10. ajayre

    ajayre Alibre Super User

    METHOD 2

    ModuleA.py:
    Code:
    def DoSomething(value):
      return value + 1
    
    ModuleB.py (in the same folder as ModuleA.py):
    Code:
    sys.path.append(ScriptFolder)
    
    from ModuleA import *
    
    print DoSomething(5)
    
    Andy
     
    NateLiqGrav likes this.

Share This Page