How to Exit / Stop Fusion API Script From Within Script (Throw Fatal Error)

How to Exit / Stop Fusion API Script From Within Script (Throw Fatal Error)

therealsamchaney
Advocate Advocate
1,808 Views
4 Replies
Message 1 of 5

How to Exit / Stop Fusion API Script From Within Script (Throw Fatal Error)

therealsamchaney
Advocate
Advocate

Hi, is it possible to throw an exception that will end/stop a Fusion 360 API script? Basically, I want to check something (for instance a folder directory given by a user) and if they give a bad path, exit the entire Fusion script (or stop the Run function) from within a function block. I've tried sys.exit() but it's not working as expected. I know the ui.inputBox has a cancel method that can exit the script so it would be great to be able to do something like that.

Thanks!

0 Likes
1,809 Views
4 Replies
Replies (4)
Message 2 of 5

therealsamchaney
Advocate
Advocate

I did get sys.exit() to work but it has to not be in a try/except block. I'm still wondering if there is a more Fusion API way to do this, like if there is a built-in cancel or exit method to stop scripts so I would say this doesn't really answer my original question.
If anyone is wondering, my code is below. It's a simple function to get a file folder path from the user.

 

 

 

        def get_path():
            export_path, cancelled = ui.inputBox('Enter path to the export folder')
            if cancelled:
                ui.messageBox('Cancelled')
                sys.exit('Cancelled')
            if not os.path.isdir(export_path):
                ui.messageBox('Path does not exist. Aborting program')
                sys.exit('Path does not exist')
            export_path = os.path.abspath(export_path)
            export_path+='\\'
            return export_path

 

 

 

One thing I noticed is that if you have a ui.inputBox call inside a function block, the cancel method doesn't actually end the script (hence why I had to have the second sys.exit). However, if you call the ui.inputBox method outside a block (just in the main run function), it does actually end the script. Must be a quirk about how cancel works. I looked into fusion.py and found the cancel method but it just calls TrimFeatureInput_cancel(self) and I can't find the definition for that method.

 

0 Likes
Message 3 of 5

kandennti
Mentor
Mentor

Hi @therealsamchaney .

 

Instead of aborting in the get_path function, how about aborting in the caller?

Also, since the UserInterface object has createFileDialog and createFolderDialog, I think it would be more user-friendly to use them.

# Fusion360API Python script
import traceback
import adsk.fusion
import adsk.core
import os

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

        export_path: str = get_folder_path()
        if len(export_path) < 1:
            return
        
        ui.messageBox(export_path)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def get_folder_path() -> str:
    app: adsk.core.Application = adsk.core.Application.get()
    ui: adsk.core.UserInterface = app.userInterface

    dlg: adsk.core.FolderDialog = ui.createFolderDialog()
    res: adsk.core.DialogResults = dlg.showDialog()

    if res:
        ui.messageBox('Cancelled')
        return ''

    export_path: str = dlg.folder
    if not os.path.isdir(export_path):
        ui.messageBox('Path does not exist. Aborting program')
        return ''

    return export_path + '\\'
Message 4 of 5

therealsamchaney
Advocate
Advocate
Thank you. I had thought about aborting in the caller, that might be better. I did not know about createFileDialog or createFolderDialog, so thank you. That is definitely better than the inputBox I was using. I am new to the API so there is a lot I don't know about yet. This should definitely improve my script!

あなたのバイオは日本語があります。日本語は話せますか?勉強しています
0 Likes
Message 5 of 5

kandennti
Mentor
Mentor

@therealsamchaney .

 

I don't understand anything but Japanese.

私の英語より、きれいな日本語になっていますよ。