Run external script from add-in

brad.bylls
Collaborator

Run external script from add-in

brad.bylls
Collaborator
Collaborator

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

I have added to my add-in:

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😞
    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.
========== 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 just sits there and does nothing.
I think I'm close from what I've seen in the forums, just can't figure it out.
 
Brad Bylls
0 Likes
Reply
Accepted solutions (1)
881 Views
3 Replies
Replies (3)

KrisKaplan
Autodesk
Autodesk

It's a little hard to guess. But this general behavior of "debug stepping through the code and it just stops, and going back to Fusion it's like my command just stopped" often means there was an uncaught exception. I would recommend following the addin samples in adding a 'try: catch:' block around all the code in all of your 'notify' functions on your event handlers, and do something with the exception info (log it, or in debug message it). I would expect you could get something useful from the message, or better yet the formatted traceback.

 

Kris



Kris Kaplan
0 Likes

brad.bylls
Collaborator
Collaborator

I looked all through the add-in examples and searched for catch: and found nothing.

So I tried this with 'try:'

# Event handler for the shcs execute event.
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
            #subprocess.run([sys.executable, 'SHCSCS'])
            gotoShcs = shcs()

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
 
It didn't help. No exceptions came up. Just went into core.py and stopped.
 
Brad
Brad Bylls
0 Likes

pludikar
Collaborator
Collaborator
Accepted solution

Hi @brad.bylls 

 

If you haven't resolved this yet I'd suggest you try a couple of things:

  1. tighten down the try/except block

 

class ShcsExecuteHandler(adsk.core.CommandEventHandler‌‌
        def __init__(self‌‌
            super().__init__()
        def notify(self, args‌‌
            eventArgs = adsk.core.CommandEventArgs.cast(args)

            # Code to react to the event.
            app = adsk.core.Application.get()
            ui  = app.userInterface
            try:
                #subprocess.run([sys.executable, 'SHCSCS'])
                gotoShcs = shcs()

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

and see if that throws any useful traceback messages 

 

  • If that doesn't do anything useful - try putting a break in the notify def block, and once the code breaks, execute the shcs() statement in the immediate debug console command line.  If there are any traceback messages to be had, they'll show up then.

If nothing else this will verify that your shcs function actually works - if it does then it's almost certainly a F360 python API bug, and the F360 team can take it from there.

 

I hope that helps you move forward.

Peter 

I'm not an expert, but I know enough to be very, very dangerous.

Life long R&D Engineer (retired after 30+ years in Military Communications, Aerospace Robotics and Transport Automation).
1 Like