pythonmayamel

Remove Menu Item in Maya using Python


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";

Solution

  • 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