--
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: http://groups.google.com/group/python_inside_maya/subscribe
# Line 1.116: Invalid use of Maya object "True".
import os
from PyQt4 import QtCore as QtCore
from PyQt4 import QtGui as QtGui
import sip
import pymel.all as p
import maya.OpenMayaUI as apiUI
class MayaQtMenu(QtGui.QWidget):
'''Show qTools menu (with icons) inside Maya'''
def __init__(self, name, label='', parent=None, lazy=False):
parent = parent or self.getMayaWindow()
QtGui.QWidget.__init__(self, parent)
label = label or name
self.name = name.replace(' ', '_')
if p.menu(self.name, exists=True):
p.menu(self.name, e=True, dai=True)
else:
p.menu(self.name, l=label, parent='MayaWindow')
self.menu = self.toQtObject(self.name)
if not lazy:
self.addItems()
def addItems(self):
'''Add menu items (actions) to menu. Placeholder. Must be implemented by subclass'''
pass
def getMayaWindow(self):
ptr = apiUI.MQtUtil.mainWindow()
return sip.wrapinstance(long(ptr), QtCore.QObject)
def toQtObject(self, mayaName):
'''
Given the name of a Maya UI element of any type,
return the corresponding QWidget or QAction.
If the object does not exist, returns None
'''
ptr = apiUI.MQtUtil.findControl(mayaName)
if ptr is None:
ptr = apiUI.MQtUtil.findLayout(mayaName)
if ptr is None:
ptr = apiUI.MQtUtil.findMenuItem(mayaName)
if ptr is not None:
return sip.wrapinstance(long(ptr), QtCore.QObject)
class qtIconsMenu(MayaQtMenu):
def addItems(self):
iconPath = p.util.getEnv('MAYA_LOCATION') + '/icons/'
pngs = [iconPath + f for f in os.listdir(iconPath) if '.png' in f]
by_letter = {}
for f in pngs:
prefix = os.path.basename(f).replace('icons','').replace('icons','')[0]
by_letter.setdefault(prefix, [])
by_letter[prefix].append(f)
for prefix, icons in by_letter.items():
submenu = QtGui.QMenu(self.menu)
submenu.setTearOffEnabled(True)
submenu.setTitle(prefix)
self.menu.addAction(submenu.menuAction())
def printPath(path):
print path
for iconPath in icons:
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(iconPath),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
menuItem = QtGui.QAction(self)
menuItem.setIcon(icon)
menuItem.setText(os.path.basename(iconPath))
menuItem.triggered.connect(lambda x, f=iconPath: printPath(f))
submenu.addAction(menuItem)
if __name__=='__main__':
qtIconsMenu('Maya Icons')