Hey there,
I created a shelf in Maya 2019, using this good ol’ Super Class that I found here years ago:
Everything works well, except that I can’t reload the shelf using a button from the shelf: this instantly makes maya crash, without any error prompt/window.
To reload the shelf when developping or debugging, I create an instance of my subclass, then call
inst._cleanOldShelf() and inst.build() . This works perfectly when used from the script editor.
If I create a button in the shelf with those methods, clicking it will crash maya.
Do you have an idea how I can let my users reload my shelf, without the need to give them scripts to launch in the script editor?
Thanks
import sgtk
import maya.cmds as cmds
import maya.mel as mel
def _null(*args):
pass
class CreateMayaShelf(object):
'''
A simple class to build shelves in maya. Since the build method is empty,
it should be extended by the derived class to build the necessary shelf elements.
By default it creates an empty shelf called 'WIP'.
BEWARE Do not use any space character ' ' in the name as Maya will freack out.
'''
def __init__(self, name='WIP'):
self.engine = sgtk.platform.current_engine()
self.name = name
self.iconPath = 'L:/_tech/media/icons/maya_shelfs/'
self.labelBackground = (0, 0, 0, 0)
self.labelColour = (.9, .9, .9)
self._cleanOldShelf()
cmds.setParent(self.name)
# self.build()
def setName(self, name):
'''
BEWARE shelf name must be unique as it will replace any shelf with same name.
BEWARE Do not use any space character ' ' in the name as maya will freack out
:param: str name
'''
self.name = name
def setName(self, name):
self.name = name
def build(self):
'''This method should be overwritten in derived classes to actually build the shelf
elements. Otherwise, nothing is added to the shelf.'''
pass
def addButon(self, label, icon='wip_32.png', command=_null, doubleCommand=_null):
'''Adds a shelf button with the specified label, command, double click command and image.'''
cmds.setParent(self.name)
if icon:
icon = self.iconPath + icon
cmds.shelfButton(width=37, height=37, image=icon, l=label, command=command, dcc=doubleCommand, imageOverlayLabel=label, olb=self.labelBackground, olc=self.labelColour)
def addSeparator(self):
# cmds.addShelfSeparator()
# cmds command seems broken
mel.eval("addShelfSeparator()")
def addMenuItem(self, parent, label, command=_null, icon=''):
'''Adds a shelf button with the specified label, command, double click command and image.'''
if icon:
icon = self.iconPath + icon
return cmds.menuItem(p=parent, l=label, c=command, i='')
def addSubMenu(self, parent, label, icon=None):
'''Adds a sub menu item with the specified label and icon to the specified parent popup menu.'''
if icon:
icon = self.iconPath + icon
return cmds.menuItem(p=parent, l=label, i=icon, subMenu=1)
def _cleanOldShelf(self):
'''Checks if the shelf exists and empties it if it does or creates it if it does not.'''
if cmds.shelfLayout(self.name, ex=1):
if cmds.shelfLayout(self.name, q=1, ca=1):
for each in cmds.shelfLayout(self.name, q=1, ca=1):
cmds.deleteUI(each)
else:
cmds.shelfLayout(self.name, p='ShelfLayout')
def _deleteOldShelf(self):
if cmds.shelfLayout(self.name, q=1, ex=1):
cmds.deleteUI(self.name, layout=1)