add shelf creating class and usersetup that tries to build it from presets

This commit is contained in:
Milan Kolar 2019-07-10 19:28:46 +02:00
parent 19ca94fe24
commit 6dc33af075
2 changed files with 101 additions and 0 deletions

View file

@ -2308,3 +2308,89 @@ def get_attr_in_layer(attr, layer):
return value
return cmds.getAttr(attr)
def _null(*args):
pass
class shelf():
'''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 "customShelf".'''
###########################################################################
'''This is an example shelf.'''
# class customShelf(_shelf):
# def build(self):
# self.addButon(label="button1")
# self.addButon("button2")
# self.addButon("popup")
# p = cmds.popupMenu(b=1)
# self.addMenuItem(p, "popupMenuItem1")
# self.addMenuItem(p, "popupMenuItem2")
# sub = self.addSubMenu(p, "subMenuLevel1")
# self.addMenuItem(sub, "subMenuLevel1Item1")
# sub2 = self.addSubMenu(sub, "subMenuLevel2")
# self.addMenuItem(sub2, "subMenuLevel2Item1")
# self.addMenuItem(sub2, "subMenuLevel2Item2")
# self.addMenuItem(sub, "subMenuLevel1Item2")
# self.addMenuItem(p, "popupMenuItem3")
# self.addButon("button3")
# customShelf()
###########################################################################
def __init__(self, name="customShelf", iconPath="", preset={}):
self.name = name
self.iconPath = iconPath
self.labelBackground = (0, 0, 0, 0)
self.labelColour = (.9, .9, .9)
self.preset = preset
self._cleanOldShelf()
cmds.setParent(self.name)
self.build()
def build(self):
'''This method should be overwritten in derived classes to actually
build the shelf elements. Otherwise, nothing is added to the shelf.'''
for item in self.preset['items']:
if not item.get('command'):
item['command'] = self._null
if item['type'] == 'button':
self.addButon(item['name'], command=item['command'])
if item['type'] == 'menuItem':
self.addMenuItem(item['parent'], item['name'], command=item['command'])
if item['type'] == 'subMenu':
self.addMenuItem(item['parent'], item['name'], command=item['command'])
def addButon(self, label, icon="commandButton.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 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")

15
setup/maya/userSetup.py Normal file
View file

@ -0,0 +1,15 @@
import os
import sys
from pypeapp import config
from pype.maya import lib
reload(lib)
presets = config.get_presets()
shelf_preset = presets['maya']['project_shelf']
project = os.environ["AVALON_PROJECT"]
modules = {}
for k, v in shelf_preset['imports'].items():
sys.modules[k] = __import__(v, fromlist=[project])
projectShelf = lib.shelf(name=shelf_preset['name'], preset=shelf_preset)