Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Running python script from menu

Running python script from menu

miklos.gabor
Explorer Explorer
276 Views
4 Replies
Message 1 of 5

Running python script from menu

miklos.gabor
Explorer
Explorer

Hi everyone!

 

So because the old menu system has removed and the new menu system way too restrictive I run into a problem that prevents us from creating Qt based menu on the main menu bar.

 

The problem comes from the fact that the #welcomeScreenDone is not the latest callback that runs, but the #cuiRegisterMenus. This unfortunately destroys the menu created by the #welcomeScreenDone callback.

 

Of course the #cuiRegisterMenus system cannot run python scripts at all since it accepts only MacroScripts.

 

Is there a reliable solution for this problem? Creating .mcr files to run the python files are also a no go, since there are other python dependencies.

0 Likes
277 Views
4 Replies
Replies (4)
Message 2 of 5

roman_woelker
Autodesk
Autodesk

Hi Miklos,
you should not use the #welcomeSceenDone notification for creating menus. The correct way of adding cui menus to the 3dsMax menu structure would be using the  #cuiRegisterMenus notification. The notification params provide a reference to the menu manager that is currently loading / evaluating the menu structure, it should be used for registering your menus. The #cuiRegisterMenus notification is send out whenever the menu system is recreating it's menu structure, that can be on startup or when the user changes his menu presets.

Here are some useful SDK help links that you could have a look at, everything should be in there:
https://help.autodesk.com/view/MAXDEV/2025/ENU/?guid=menu_system
https://help.autodesk.com/view/MAXDEV/2025/ENU/?guid=menu_migration_guide

 

Adding menus by maxscript:
https://help.autodesk.com/view/MAXDEV/2025/ENU/?guid=GUID-FF48D0EC-6669-4EC7-AB43-E9998A14A198


The menu system also offers support for python, please see SDK help page here:
https://help.autodesk.com/view/MAXDEV/2025/ENU/?guid=MAXDEV_Python_using_pymxs_pymxs_macroscripts_me...


I hope that helps you.

 

Thanks,

Roman


Roman Woelker
Software Development Engineer

0 Likes
Message 3 of 5

miklos.gabor
Explorer
Explorer

Hi! Thanks for your answer!

 

Yes I know how it works. My problem is that from now we have to launch python codes from .mcr files. (which is not really ideal) Since the entire menu system is based on Qt we should be able to take advantage of it. This is how we are doing it now.

 

We are creating a new macro (that generates an .mcr file) for the menu item.

rt.macros.new(
    "BARB",
    "TestScript",
    "Run TestScript MaxScript",
    "TestScript",
    "python.execute \"from importlib import reload;import mytool;reload(mytool);mytool.main()\""
)

With this, the system generates a new .mcr file within the usermacros folder, which we want to prevent, since we have to clean up the entire folder if a tool from the menu item become deprecated or removed.

 

Then obviously we are using the above solution to construct our menus. (using MD5 hash of the menu path to construct a stable guid, so we can fill up the menu and sub-menus based on them...)

 

BUT! We used to be able to do this instead:

win = get_mainwin()
top = win.topLevelWidget()
menuBar = top.findChild(QtWidgets.QMenuBar)

# setup the base menu
menu = QtWidgets.QMenu(key, menuBar)
menu.setObjectName(key)
menu.setTearOffEnabled(True)
menu.setTitle(key)
menu.setToolTipsVisible(True)

etc...

 

All we had to do is bind it to the welcomeScreenDone callback and violá... No .mcr files created. No ActionItem registration was necessary. It just worked. Dynamic menu without a struggle. Actions called natively by the addAction().

 

With the new system we are forced to use the .mcr way which adds unnecessary steps to use dynamically constructed menus.

 

Would it be possible to update the system to call custom actions based on action type in the future? (update/next release of max)

0 Likes
Message 4 of 5

roman_woelker
Autodesk
Autodesk

Hi Miklos,

 

I know that Qt and PySide gives you a lot of freedom to mess around with the underlying application stuff. 😉
Accessing / modifying the Qt menus directly from the main menubar is annulling the customize user interface (cui) menu system, if you do that for your own scripting it might be ok...
but when you are a 3rd party plugin developer, doing things for customers, this direct UI manipulation won't allow your customers to customize/modify the menus that you've been added.
Your menus won't show up in the menu editor and the final menu structure shown in main menubar won't match with what they see in the menu editor.
This can be confusing to the end-user. So I can't really recommend that approach.

 

However if you really want to go that route (not the currently officially documented way), the #welcomeSceenDone notification is not the right notification to listen at, since as soon as the user or something else load another menu structure your directly added menus will be gone.
There are more suitable menu notifications you could look at. Unfortunately I just noticed, that the recent MaxScript/Python documentation is not containing the docs on those. 😢
There is for instance the #cuiMenusPostLoad notification that you could hook up, which is send out after the menu structure has been loaded.
Before accessing the Qt menus it might be necessary to call on Qt side QApplication::sendPostedEvents(), since the Max Qt menus are updating with a deferred mechanism to the underlying cui menu structure changes.
As said this, this is not the official way to work with cui menus. 🙄

 

>> Would it be possible to update the system to call custom actions based on action type in the future? (update/next release of max)

We'll consider that idea and evaluate if we can add a mechanism that allows you to execute functions / code simpler than specifying an ActionItem/MarcoScriptAction. I think that's what you had in mind, right?

 

Thanks,

Roman


Roman Woelker
Software Development Engineer

0 Likes
Message 5 of 5

miklos.gabor
Explorer
Explorer

Ah thanks for this info. I'll check this for sure.

 

Actually it's not for plug-in development, but pipeline development, where we are adding custom python scripts doing pipeline stuffs. So basically that's the reason why I asked for this solution. 🙂

 

There are situations where it's ideal for us to manipulate Qt related things, like adding custom icons or to just simply adding custom menu items to the toolbar. These are quite useful especially when the goal is to set up the system the non-standard way, like unifying the complete menu system in different software like Maya and 3ds max. When this happens you just have to write it only once which is good from the maintenance point of view. Python is python almost everywhere and works the same way. 🙂

0 Likes