1

I am trying to build a custom menu in maya.

google groups has this code and it shows that it works but when i try it does not show in menu bar for me. I have tried for hours. What else is required for it to show in menu. IOW why does it not show in maya menu bar.

https://groups.google.com/g/python_inside_maya/c/XqM7Rkm2kOE

import pymel.core as pm

# Name of the global variable for the Maya window
MainMayaWindow = pm.language.melGlobals['gMainWindow'] 

# Build a menu and parent underthe Maya Window
customMenu = pm.menu('Custom Menu', parent=MainMayaWindow)
# Build a menu item and parent under the 'customMenu'
pm.menuItem(label="menu item 'hihi'", command="print 'hihi'", parent=customMenu)

edit : when querying menus it does show it in the list

maya_main_window = mel.eval("$tmpVar = $gMainWindow")

menus = cmds.window(maya_main_window, query=True, menuArray=True)
for menu in menus:
    print menu
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
nish
  • 1,008
  • 4
  • 17
  • 34
  • 1
    The posts in that link are nearly 7 years old, a ton could have changed since then. I suggest finding something up-to-date and see if that helps. – Random Davis Nov 24 '21 at 16:14
  • 1
    @RandomDavis, I mean every thing i tried from google search does not show on menu bar. Querying all menus in script editor however has this custom menu ( see above edit ) – nish Nov 25 '21 at 13:34
  • 1
    You need to parent the menu to the menuBar, not the window. So get the menuBar of MainMayaWindow. and use that as parent – ziarra Nov 25 '21 at 16:05

1 Answers1

2

It works

I'm using Maya 2020 running on macOS Monterey. Your code works fine. Here's a screenshot:

enter image description here

import pymel.core as pm

MainMayaWindow = pm.language.melGlobals['gMainWindow']
customMenu = pm.menu('Custom Menu', parent=MainMayaWindow)
pm.menuItem(l="menu item 'hihi'", command="print 'hihi'", parent=customMenu)

If it doesn't work

If you can't create that menu, the problem is, your module might be corrupted. I think the best two things you can do about it – to reinstall it, or just use maya.cmds instead of PyMel or OpenMaya.

The main reason not to use PyMel – because it's too slow. The main reason not to use OpenMaya – because there's so much boiler plate code. Watch this video to find out what cons and pros PyMel module has.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • 1
    Thank you @Andy Jazz. Not so apparent but the window has been present somewhere behind the workspace option. – nish Nov 27 '21 at 11:05