Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

What's a good way to run an external script from within a python script add-in?

8 REPLIES 8
Reply
Message 1 of 9
jzaneS34LX
1473 Views, 8 Replies

What's a good way to run an external script from within a python script add-in?

Right now I have a simple python fusion add-in script that by itself works fine, but when I try to get it to trigger an external script to run I'm running into problems. I have tried calling both

os.system("python scriptpath")

and

exec(open(scriptpath).read())

but in both cases the add-in will fail to trigger these scripts. I've verified through textboxes that the add-in reaches both these commands and gets past them without crashing, but fails to actually trigger the external script. I have also tried moving the script from its original location to one within the Fusion Scripts directory, but that doesn't change the outcome. I'm a newbie to Fusion add-ins so any help would be appreciated. 

Tags (3)
8 REPLIES 8
Message 2 of 9
goyals
in reply to: jzaneS34LX

I just tried calling exec("app = adsk.core.Application.get()\nui = app.userInterface\nui.messageBox('Hi')") this from a script and it works for me.  May be you would like to check if there is some error happens in the script executed by exec function.



Shyam Goyal
Sr. Software Dev. Manager
Message 3 of 9
jzaneS34LX
in reply to: goyals

The script runs fine when I call it from the command line - do you think it's some dependency/directory issue with how fusion runs scripts? This isn't another autodesk API script, it's a custom script that takes a screenshot of the current window. 

Message 4 of 9
prainsberry
in reply to: jzaneS34LX

Is there  reason to run it ass a separate script?  The way scripts/add-inss work in Fusion is a little different than the way you would normally execute a standard python script (with main, etc).  Is it possible to structure it slightly differently that your "other" script defines a function or class that you could import into this main one?  Then in the standalone version you could also call that function?  This should work no problem.  Alternatively if the "other" script was also an add-in that defined a command in Fusion, the first could cause the command in the "other" one to execute.  
I hope that makes sense?

 



Patrick Rainsberry
Developer Advocate, Fusion 360
Message 5 of 9
jzaneS34LX
in reply to: prainsberry

I'm going to try that solution, although this is a test case for another, larger script that I'm hoping to run that generates a STL file that I would then import back to fusion. That separate script is fairly self-contained, and I was hoping to not have to rework it to fit into an Autodesk add-in format. 

Message 6 of 9
prainsberry
in reply to: jzaneS34LX

You would not have to do any special formatting in that case.  This is very common practice in python scripts I've done and seen in Fusion.  It looks some thing like this:

my_addin.py

|-- stl_package

      |-- __init__.py

      |-- stl_module_1.py

      |-- stl_module_2.py

 

then you would say something like:

import adsk.core
import adsk.fusion

from ./stl_package import stl_module_1 
from ./stl_package import stl_module_2

# Later in your addin code:

result = stl_module_1.cool_stl_function(my_brep_body)

 

 



Patrick Rainsberry
Developer Advocate, Fusion 360
Message 7 of 9
Anonymous
in reply to: jzaneS34LX

I am encountering the same problem -- I am trying to take a screenshot with pyautogui but it just does not work from the add-in script, hence I tried writing a separate script and running this from the add-in script. Still, no success.

Did you have any success with this?

Message 8 of 9
brad.bylls
in reply to: prainsberry

I am trying to run an external script from my add-in.

I have added to my add-in script:

import adsk.core, adsk.fusion, traceback

from .SHCS import shcs <<<<<  Added
from .FHSCS import fhscs <<<<<  Added

handlers = []

def run(context
 

 

    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        cmdDefs = ui.commandDefinitions
========== More code
 
class ShcsExecuteHandler(adsk.core.CommandEventHandler
 

try:

    def __init__(self
 

 

        super().__init__()
    def notify(selfargs
 

 

        eventArgs = adsk.core.CommandEventArgs.cast(args)

        # Code to react to the event.
        app = adsk.core.Application.get()
        ui  = app.userInterface
        gotoShcs = shcs() <<<<< Changed from a message box that works, so I know it gets here.
 
except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
========== Rest of the code
 
In the external script (SHCS.py which worked as a plain script) I have:
import adsk.core, adsk.fusion, traceback
import math, os

def shcs(): <<<<<  Added and tabbed over the rest of the code till end
    # Globals
    _app = adsk.core.Application.cast(None)
    _ui = adsk.core.UserInterface.cast(None)
    strUnits = ''
========== Rest of the code
 
While debugging, it goes thru the add-in script with no errors until:
        gotoShcs = shcs()
It then goes to core.py does a few things and then just sits there (in core.py) and does nothing.
I think I'm close from what I've seen in this thread, just can't figure it out.
I must be missing something.
Brad Bylls
Message 9 of 9
prainsberry
in reply to: brad.bylls

Is anything happening in shcs() ?

If you are willing to share the code I can take a look. You can email me directly at patrick.rainsberry_at_autodesk.com


Patrick Rainsberry
Developer Advocate, Fusion 360

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report