3

How can I remove the menu item from the main window using Python? I have it working using MEL but I need it in Python as well.

The part that isn't working is the find menu if exists and delete. I can't seem to find the equivalent in Python.

Python (not working)

import maya.cmds as cmds

if(???)
{
    #cmds.deleteUI('JokerMartini', menu=True )
}

cmds.menu(label='JokerMartini', tearOff=True, p='MayaWindow')
cmds.menuItem(label='Action 1', c= 'something.run()')
cmds.menuItem(divider=True)
cmds.menuItem(label='Action 2', c= 'something.run()')

Mel (working)

if(`menu -exists JokerMartini`)
{
    deleteUI JokerMartini;
}
global string $gMainWindow;
setParent $gMainWindow;
menu -label "JokerMartini" -to true -aob true JokerMartini;    
menuItem -label "Action 1" -command "something";
menuItem -label "Rename..." -command "something";
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
JokerMartini
  • 5,674
  • 9
  • 83
  • 193

1 Answers1

4

Here's an approach for main menu item creation:

import maya.cmds as mc

menuJM = "JM"
labelMenu = "JokerMartini"

mc.menu(menuJM, l=labelMenu, to=1, p='MayaWindow')
mc.menuItem(l='Action 1', c='something.run()')
mc.menuItem(d=True)
mc.menuItem(l='Action 2', c='something.run()')

And for deletion you should use this approach:

if mc.menu(menuJM, l=labelMenu, p='MayaWindow') != 0:
    mc.deleteUI(mc.menu(menuJM, l=labelMenu, e=1, dai=1))
    mc.deleteUI(menuJM)     

mc.refresh()

enter image description here

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • why are there 2 calls to mc.deleteUI? and what does dai = 1 and vis = 1 mean? – Jitesh Sep 24 '20 at 08:04
  • 1
    @Jitesh, `dai` = deleteAllItems and `vis` is deprecated now. Try one of calls, and then both of them and you'll see why. – Andy Jazz Sep 24 '20 at 08:15
  • 1
    in the latest version of maya, deleteAllItems is not needed anymore. and neither it seems refresh afterwards. simply run this: `maya.cmds.deleteUI(menu_name, menu=True)` – Hannes Mar 17 '23 at 22:33