[bug] Palette created in the run function and Run on Startup enabled

[bug] Palette created in the run function and Run on Startup enabled

JeromeBriot
Mentor Mentor
772 Views
4 Replies
Message 1 of 5

[bug] Palette created in the run function and Run on Startup enabled

JeromeBriot
Mentor
Mentor

Hello,

 

This is related to the discussion Add-in with Run on Startup enabled crashes Fusion 360 at launch by @BradAndersonJr 

 

Fusion 360 crashes when a palette is created in the run function of an add-in and the Run on Startup is enabled.

 

Here is an example :

#Author-Jerome Briot
#Description-Track time spent on Fusion 360

import adsk.core, adsk.fusion, traceback
import threading

app = adsk.core.Application.cast(None)
ui = adsk.core.UserInterface.cast(None)
ctrl = adsk.core.CommandControl.cast(None)

handlers = []

thisAddinName = 'bugPaletteDrawing3'
thisAddinVersion = '0.0.0'
thisAddinAuthor = 'Jerome Briot'
thisAddinContact = 'jbtechlab@gmail.com'


# Event handler for the commandExecuted event.
class ShowPaletteCommandExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args):

        try:

            palette = ui.palettes.itemById(thisAddinName + 'Palette')

            palette.isVisible = not(palette.isVisible)

        except:
            ui.messageBox('Command executed failed: {}'.format(traceback.format_exc()), thisAddinName, 0, 0)


# Event handler for the commandCreated event.
class ShowPaletteCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args):
        try:
            command = args.command
            onExecute = ShowPaletteCommandExecuteHandler()
            command.execute.add(onExecute)
            handlers.append(onExecute)
        except:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()), thisAddinName, 0, 0)


def run(context):

    global ui, app, ctrl    

    try:

        app = adsk.core.Application.get()
        ui = app.userInterface

        qatRToolbar = ui.toolbars.itemById('QATRight')

        showPaletteCmdDef = ui.commandDefinitions.addButtonDefinition(thisAddinName + 'CmdDef', thisAddinName, '', './resources')

        # Connect to Command Created event.
        onCommandCreated = ShowPaletteCommandCreatedHandler()
        showPaletteCmdDef.commandCreated.add(onCommandCreated)
        handlers.append(onCommandCreated)

        ctrl = qatRToolbar.controls.addCommand(showPaletteCmdDef, 'HealthStatusCommand', False)

        ui.messageBox('Create palette')
        palette = ui.palettes.add(thisAddinName + 'Palette', thisAddinName, 'test.html', True, True, True, 350, 400)
        ui.messageBox('OK')

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


def stop(context):

    try:

        cmdDef = ui.commandDefinitions.itemById(thisAddinName + 'CmdDef')
        if cmdDef:
            cmdDef.deleteMe()

        qatRToolbar = ui.toolbars.itemById('QATRight')
        cmd = qatRToolbar.controls.itemById(thisAddinName + 'CmdDef')
        if cmd:
            cmd.deleteMe()

        palette = ui.palettes.itemById(thisAddinName + 'Palette')
        if palette:
            palette.deleteMe()

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

Thank you

0 Likes
Accepted solutions (1)
773 Views
4 Replies
Replies (4)
Message 2 of 5

goyals
Autodesk
Autodesk
Accepted solution

Palette is an UI control so creating it in run method does look correct to me. If you need to create palette just after the application starts up, you can consider to listen DocumentOpened event on application object http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-270c87f0-04d7-4bd8-9632-0e025d68df39.

 

You need to add some kind of check so not to do it on every document creation.



Shyam Goyal
Sr. Software Dev. Manager
0 Likes
Message 3 of 5

JeromeBriot
Mentor
Mentor

@goyals wrote:

Palette is an UI control so creating it in run method does look correct to me.


Did you try to launch Fusion 360 with "Run As Startup" enabled for the add-in I attached ?

0 Likes
Message 4 of 5

goyals
Autodesk
Autodesk

I made a mistake while typing. I wanted to write  "Palette is an UI control so creating it in run method does not look correct to me". Sorry for confusion.



Shyam Goyal
Sr. Software Dev. Manager
Message 5 of 5

JeromeBriot
Mentor
Mentor

@goyals wrote:

Palette is an UI control so creating it in run method does not look correct to me


I agree but Fusion 360 should not crash when facing this issue.

0 Likes