Remove menu item in Maya using Python

How can I remove a menu item from the main window using Python? It works for me using MEL, but I need it in Python too.

The part that doesn't work is this find menu if exists and delete

. I can't seem to find an equivalent in Python.

Python (doesn't work)

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

      

+3


source to share


1 answer


Here's an approach for creating a main menu item:

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 to remove you have to use this approach:



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

mc.refresh()

      

enter image description here

Hope this helps.

+4


source







All Articles