Adding a custom script inside AYON Menu

Hi

I have written a custom script for Maya 2023. I am trying to add that into the AYON menu. Can anyone help me integrating that in the menu?

Currently, I am able to add it to the menu but when I run Maya, the menu disappears.

furthermore, if I just add it as a menu item without calling any functionality, I am able to see the menu, but as soon as I change the code and call the script on menuitem click. the menu disappears.

Could you share HOW you added your script into the menu? What does it look like in the configured settings?

call the script on menuitem click. the menu disappears.

So the menu works, it shows the entry - but when you click it in the script menu then suddenly the menu disappears?

Does the Maya script editor show any logs/errors?
Can you make a screen recording of this happening?

Thanks for the reply,

image
This is how menu is looking right now after updating the menu.

If I remove on click event, only then I can see the menu.
Once I enable it, it stops working.

This is how I am writing the code for it.

import bulk_renamer

        cmds.menuItem(divider=True)
        internal_tools_menu = cmds.menuItem(
            "Internal Tools",
            subMenu=True,
            tearOff=True,
            parent=MENU_NAME
        )
        cmds.menuItem(
            "Bulk Rename Utility",
            parent=internal_tools_menu,
            command= bulk_renamer.show_ui()
        )

bulk_renamer.py file is in the same folder as of menu.py

in bulk_rename.py I have following function to show the UI

def show_ui():
    ui = BulkRenameUI()
    ui.show()

I feel its not able to execute the import statement. Hence the menu is also not loading. Is there any way to use the import statement?

I have also tried using

from bulk_renamer import show_ui

Let me start of by saying - you may be better of using the customization features of AYON using settings, to add custom tools.

ayon+settings://maya/scriptsmenu

The maya addon settings allow you to generate a custom menu directly from settings:

They won’t be listed under the AYON menu - but under the separate unique menu (defaulting to “Custom Tools” but you can just change that around).

If you want to build complex menus there, you may use the Raw JSON builder and the tooltip will provide you a link that has more details on that JSON format. For sake of this discussion, here is a direct link to that page.

Bonus points here is that you do not need to adjust any AYON code - you can just customize the settings.

Does that help?

(Using the JSON format in our studio we have a deeply nested customized menu there with tons of custom scripts)

Generating the JSON from a folder containing scripts

Here’s a Gist that may guide you on automatically generating the Menu JSON if you happen to have nested folders of scripts and you want to use that folder structure to create the menu list:


If you really want to mess with the AYON menu itself and add menu entries in code there, then:

Code issues

        cmds.menuItem(
            "Bulk Rename Utility",
            parent=internal_tools_menu,
            command= bulk_renamer.show_ui()
        )

This basically run the show UI command directly when generating the menu - not when pressing the button because the Python executes directly.

It should either be:

        cmds.menuItem(
            "Bulk Rename Utility",
            parent=internal_tools_menu,
            command= bulk_renamer.show_ui
        )

Or:

        cmds.menuItem(
            "Bulk Rename Utility",
            parent=internal_tools_menu,
            command=lambda: bulk_renamer.show_ui()
        )

I may recommend the latter to ensure the menu item doesn’t start passing arguments to your command that you may not be expecting.

Additionally, not sure what type of UI you’re making (whether it’s maya UI or Qt UI) but if it’s Qt UI be aware of garbage collecting:

def show_ui():
    ui = BulkRenameUI()
    ui.show()

In this case the variable ui goes out of scope at the end of the function, so it may e.g. garbage collect your UI directly and instantly make it disappear. (unless you keep a reference to it or parent it to another UI object which then keeps the the reference to it)

This is then an easy ‘trick’ to work around it: (Although it may not be good practice).

def show_ui():
    global ui
    ui = BulkRenameUI()
    ui.show()

Thanks for the detailed guide!! I will try it out.