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: 

subprocess.run unable to run external script

4 REPLIES 4
Reply
Message 1 of 5
students
1436 Views, 4 Replies

subprocess.run unable to run external script

Dear Fusion-community

 

We are currently trying to run a custom external python 3.5 script from a Fusion script. For that we use subprocess.run method.

 

In a normal python environment the command works perfectly fine and produce the following output:

>>> import subprocess
>>> subprocess.run(['/Users/Dani/Development/ALPA_Ordering/localpy_3.5/bin/python', '/Users/Dani/Development/ALPA_Ordering/smartsheet_import.py'], stdout=subprocess.PIPE)

CompletedProcess(args=['/Users/Dani/Development/ALPA_Ordering/localpy_3.5/bin/python', '/Users/Dani/Development/ALPA_Ordering/smartsheet_import.py'], returncode=0, stdout=b"{'_orderName': '15_05_2018_OleOle', '_Prozess': 'Formiga 100\xc2\xb5m', '_export': '', '_filepathPNG': '/Users/Dani/Desktop/STL_Export/2018_05_15_15_05_2018_OleOle', '_Lieferant': '', '_Kommentar': '', '_KTI': False, '_Oberflaeche': 'unbehandelt', '_Typ': 'AM Prototyp', '_boundingBox': 17.999999999999993, '_SelectionVolume': 4.075783958277259, '_filepathSTL': '/Users/Dani/Desktop/STL_Export/15_05_2018_OleOle.stl', '_Versanddatum': '11-12-2018', '_Farbe': 'unbehandelt', '_filepathSTP': '/Users/Dani/Desktop/STL_Export/2018_05_15_15_05_2018_OleOle.stp', '_Selection': 'K\xc3\xb6rper', '_Projekt': '1.1 Platon', '_Material': 'PA 2200', '_Quotation': 5.91, '_Anzahl': 1, '_Ausrichtung': 'Eingabe der Orientierung im Bauraum'}\nReached End\n")

By running the exact same command in the Fusion script, the script does not do what it is supposed to do and the following output appears:

>>> import subprocess
>>> subprocess.run(['/Users/Dani/Development/ALPA_Ordering/localpy_3.5/bin/python', '/Users/Dani/Development/ALPA_Ordering/smartsheet_import.py'], stdout=subprocess.PIPE)

CompletedProcess(args=['/Users/Dani/Development/ALPA_Ordering/localpy_3.5/bin/python', '/Users/Dani/Development/ALPA_Ordering/smartsheet_import.py'], returncode=1, stdout=b'')

 

How can we execute this external script from a Fusion script?  Does have an idea what could have gone wrong here?

 

PS: We use absolut path to call the script and the directly has 777 permissions. We are on Mac.

 

We tried also os.sys method, subprocess.check_output method, but they are not running either.

 

Thanks a lot in advance for your support. We're really running out of ideas.

 

Best,

Daniel

4 REPLIES 4
Message 2 of 5
marshaltu
in reply to: students

Hello,

 

I did a quick testing with simple Python script (print "Hello World" message). It worked well. I am not sure if you were calling Fusion API in your script which is launched by sub process? It would be good if you can post your test script here.

 

Thanks,

Marshal

 

import adsk.core, adsk.fusion, traceback
import subprocess

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()   
        ui = app.userInterface
        
        subprocess.run(['C:/Python35/python', 'C:/test.py'])
        
        ui.messageBox('Ok')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 3 of 5
students
in reply to: marshaltu

Dear Marshal,

 

Thanks a lot for your answer. Actually, I want to launch a non-Fusion-API script within my Fusion script, which is not working on my computer. I have prepared a little test script to demonstrate what is not working as expected.

 

Fusion Script:

import adsk.core, adsk.fusion, adsk.cam, traceback
import subprocess
import os

def find_curr_path():
    """
    Finds the path where the python script is stored
    """
    return os.path.dirname(os.path.realpath(__file__))

def run(context):
    ui = None
    try:
        
        app = adsk.core.Application.get()
        ui  = app.userInterface

        # for my machine
        # /usr/local/bin/python3.5      works
        # /usr/local/bin/python3.6      python crashes
        absolute_python_exec_path = "/usr/local/bin/python3.5"
        a = subprocess.run([absolute_python_exec_path, os.path.join(find_curr_path(), "called.py")])

        print(a.args)
        print(a.stdout)
        print(a.stderr)
        print(a.returncode)

        ui.messageBox('Finished')


    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
        print('Failed:\n{}'.format(traceback.format_exc()))

 

Called.py in the same directory as the Fusion script above:

import os
import time

def find_curr_path():
    """
    Finds the path where the python script is stored
    """
    return os.path.dirname(os.path.realpath(__file__))

"""File to call from the fusion-script, if it works creates a directory"""

print("What")
# time.sleep(10)
print("What")

path = os.path.join(find_curr_path(), 'testing')

# attempts to create a directory
if not os.path.exists(path):
    os.makedirs(path)

After running this script, the following error code occurs in the spyder console:

>>> ['/usr/local/bin/python3.5', '/Users/Dani/Library/Application Support/Autodesk/Autodesk Fusion 360/API/Scripts/Subprocess_Run_Test_Script/called.py']
None
None
1

 

The exact same command works in another python environment.

subprocess.run([absolute_python_exec_path, os.path.join(find_curr_path(), "called.py")])

 

 

Since I am really running out of ideas what the error could be, I am really looking forward to hearing from you - thanks in advance.

Best wishes,

Danny

Message 4 of 5
marshaltu
in reply to: students

Hello,

 

I can reproduce the issue in MAC and but it worked well in Windows. I logged it as UP-39001 in our internal system to track the issue. I cannot find workaround for you in MAC. Instead you could run your script in Windows.

 

Thanks,

Marshal



Marshal Tu
Fusion 360 Developer
Autodesk, Inc.

Message 5 of 5
students
in reply to: marshaltu

Thanks a lot Marshal - I really appreciate your support!

 

The automated ordering tool for AM parts works now on Windows computers in our company. For Mac we found a solution with an external button to run the second script.

 

Best regards

Daniel

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