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: 

Command button won't rename

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
bevis.hobbs21
315 Views, 4 Replies

Command button won't rename

The property to rename a command button in the UI: 

 

CommandDefinition.name

 

It is documented here: https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-cdf53108-bcaf-418e-b678-f041f2e207c4

And it won't work, when I set it, it doesn't change the name in the UI. The tooltip property changes the name of the tooltip fine, but the name property doesn't change the name of the command button in the UI. 

Here's my code:

 

import adsk.core, adsk.fusion, traceback

button_names = [("Start calc Add-In", "Starts the calculation Add-In."),
                ("Stop calc Add-In", "Stops the calculation Add-In.")]
panel_id = "SolidScriptsAddinsPanel"
command_id = "calc_add_in"

handlers = []; running = False

app = adsk.core.Application.get()
ui = app.userInterface
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
rootComp = design.rootComponent

def msg(message): ui.messageBox(str(message))

class CommandCreatedEventHandlerPanel(adsk.core.CommandCreatedEventHandler):

    def __init__(self):
        super().__init__()

    def notify(self, args):
        global running
        try:
            running = not running
            commandDefinitionPanel_.name = button_names[int(running)][0]
            commandDefinitionPanel_.tooltip = button_names[int(running)][1]
            msg("Calculation "+("started" if running else "stopped"))
        except:
            if ui: ui.messageBox("Failed:\n{}".format(traceback.format_exc()))

def run(context):
    global commandDefinitionPanel_
    try:
        workspaces_ = ui.workspaces
        modelingWorkspace_ = workspaces_.itemById("FusionSolidEnvironment")
        toolbarPanels_ = modelingWorkspace_.toolbarPanels
        toolbarPanel_ = toolbarPanels_.itemById(panel_id)
        toolbarControlsPanel_ = toolbarPanel_.controls
        toolbarControlPanel_ = toolbarControlsPanel_.itemById(command_id)

        if not toolbarControlPanel_:
            commandDefinitionPanel_ = ui.commandDefinitions.itemById(command_id)
            if not commandDefinitionPanel_:
                commandDefinitionPanel_ = ui.commandDefinitions.addButtonDefinition(
                    command_id, *button_names[0])

            toolbarControlPanel_ = toolbarControlsPanel_.addCommand(commandDefinitionPanel_)
            toolbarControlPanel_.isVisible = True

            onCommandCreated = CommandCreatedEventHandlerPanel()
            commandDefinitionPanel_.commandCreated.add(onCommandCreated)
            handlers.append(onCommandCreated)
    except:
        if ui: ui.messageBox("Failed:\n{}".format(traceback.format_exc()))

def commandControlByIdForPanel(id):
    if not id:
        ui.messageBox("commandControl id is not specified")
        return None
    workspaces_ = ui.workspaces
    modelingWorkspace_ = workspaces_.itemById("FusionSolidEnvironment")
    toolbarPanels_ = modelingWorkspace_.toolbarPanels
    toolbarPanel_ = toolbarPanels_.itemById(panel_id)
    toolbarControls_ = toolbarPanel_.controls
    toolbarControl_ = toolbarControls_.itemById(id)
    return toolbarControl_

def destroy_object(item):
    if item and item.isValid: item.deleteMe()

def stop(context):
    global running
    try:
        running = False

        commandDefinitions_ = ui.commandDefinitions
        commandDefinition_ = commandDefinitions_.itemById(command_id)
        destroy_object(commandDefinition_)
        commandControlPanel_ = commandControlByIdForPanel(command_id)
        destroy_object(commandControlPanel_)
    except:
        if ui: ui.messageBox("Failed:\n{}".format(traceback.format_exc()))

 

 

Labels (1)
4 REPLIES 4
Message 2 of 5
BrianEkins
in reply to: bevis.hobbs21

This looks like a bug to me.  It appears that setting the name property is working but the displayed name in the panel is not updating.  I could see this because after setting the name and getting the value of the name property after, it is returning the name I set it to.  Also, after setting the name and if I choose the "Pin to Toolbar" option for the command to show the command in the toolbar, I see the new name in the tooltip.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 5
thomasa88
in reply to: bevis.hobbs21

"Wait, I already do that in an add-in.."

I loaded your code and I can not for the life of me figure out why it works for my add-in but not yours:

 

  • Tried moving the update code to the Execute handler (created by the Created handler)
  • Tried setting resourceFolder - but on commandDefinition creation and on update
  • Tried putting the command def under the Utility menu
  • Tried putting the command def under my add-in's menu, and under a sub-menu (Dropdown). The last place is where my add-ins Record button successfully updates! :S

 

Have not tried:

Making sure the command completes successfully

Letting the event loop run (adsk.doEvents() or a zero delay CustomEvent) - but I can't see my add-in doing that either.

 

My add-in, for reference: https://github.com/thomasa88/AnyShortcut  AnyShortcut.py is the main file.

I have wrapped the Event handlers quite dramatically, so it's a bit hard to compare.., but they are the add_handler() calls.

You can run the add-in and see that the Record menu item updates when clicked..

 

Fusion 360 v. 2.0.10253

Message 4 of 5
bevis.hobbs21
in reply to: thomasa88

I don't appear to be able to access your add-in, could you attach it to this forum? I am eager to see if I can find out why your add-in will rename the command button and mine won't.

Message 5 of 5
thomasa88
in reply to: bevis.hobbs21

That's strange. So you can't access github at all?

 

I realized that my DirectName add-in behaves more similar to your code and is a bit smaller. It only changes its icon by default, but I patched it to change the name, which worked fine.

 

I have attached the patched version as a zip.

Patch: https://github.com/thomasa88/DirectName/commit/88c043fdef78a4b5c4dd791df0252fcf44e3ae9b

 

I tried disabling .isPromoted(ByDefault) in AnyShortcut without any problems. So the promotion does not seem to be part of it.

 

Edit. @bevis.hobbs21  Are you running Windows or Mac btw? I'm using Windows 10.

Edit. update_enable_button() is the function that changes the text.

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

Post to forums  

Autodesk Design & Make Report