How to access the Maya main menu and toolbar?

Question:

I use Autodesk Maya 2016 + Python.

I had a problem. I need to replace the functions of standard menu items and toolbars. For example, you must add the function to save a recording function. And because different people use different functionality, you have to change everything at once. Someone presses Ctrl+S, others go to the menu File->Save, and some press icon on the toolbar. In all areas it is necessary to replace the functionality. I understand that the problem boils down to in order to access the menus, toolbars and keyboard shortcuts. Next, find the associates item on the estate to know what function is now called. Then replace it with their cause and at the end of the function that invoked earlier in this menu.

  1. how to get Maya main menu (like File and other)?
  2. how to get Maya main toolbar?
  3. how to get Maya hotkeys?

Maya is based on QT. So just need to get the objects, and then have the standard means of QT can do everything.

How to do it?

Asked By: Massimo

||

Answers:

All three ways of doing this call the runTimeCommand named SaveScene. Override that command and you have hijacked all of the three ways you describe. This is a bit tricky since the command is marked with the flag -default which makes it impossible to change them on the fly. You can hook them where they are first defined. They are defined in the file:

  • Mayadir/scripts/startup/defaultRunTimeCommands.mel

Copy this file to your user profile maya script directory or studio script directry. These will get precedence of factory script due to resolution order. Do not overwrite the factory file. Then change -command in the lines,

runTimeCommand -default true
    -annotation (uiRes("m_defaultRunTimeCommands.kSaveSceneAnnot"))
    -category   ("File")
    -command    ("checkForUnknownNodes(); FileMenu_SaveItem")
    SaveScene;

to something else. Best would be just to add a hook here.

Obviously, you can also change the button/menuitem and hotkey too. So what you do is run following mel (it’s easier to do this in mel since the bulk of the commands are built that way, port to python or c++ if you must):

runTimeCommand
    -annotation "Print the word "Hello""
    -command ("print "foo"")
    MySave;
nameCommand         
    -annotation "Print the word "Hello""
    -command ("print "foo"")
    MySaveNamed;
buildFileMenu();
menuItem -e -c "MySave" "MayaWindow|mainFileMenu|saveItem";
iconTextButton -e -c "MySave2" openSceneButton;
hotkey -keyShortcut "s" -ctl -name ("MySaveNamed");

Warning: The hotkey will be permanent in prefs, until you change it back or reset prefs.

Please note: There are at least two other ways user could save and you could not have full control of those even if you wanted to.

Answered By: joojaa

seeing the original answer was from 2016 and maya has lots of python support added since then, here is something for more recent versions.

  1. how to get Maya main menu (like File and other)?

You can search for a menu from the main menu toolbar with this code

import maya

def find_menu(name):
    # get the main window
    gMainWindow = maya.mel.eval('$temp=$gMainWindow')

    # get all the menus that are children of the main menu
    mainWindowMenus = maya.cmds.window(gMainWindow, query=True, menuArray=True)
    for menu in mainWindowMenus:
        print(menu)
        if menu.lower() == name.lower():
            return menu

you can then make a new menu entry under this with

pm.menuItem(optional_unique_name,label="click me", command="print('hi')", parent= the_name_you_got_returned)
Answered By: Hannes
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.