-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup_maya_icon_menus.py
120 lines (92 loc) · 3.57 KB
/
setup_maya_icon_menus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
Author: Ben Hearn
Set up maya icon tools and maya custom menus
Adds a tearable toolbar and 2 clickable iconswith example scripts attached to them
"""
import maya.OpenMayaUI as apiUI
import maya.mel as mel
from PySide import QtGui, QtCore
import shiboken
#import sip
#from PyQt4 import QtGui
import os
import maya.cmds as cmds
def getMayaStatusLine():
gStatusLine = mel.eval('$temp=$gStatusLine')
ptr = apiUI.MQtUtil.findControl(gStatusLine)
if ptr is not None:
return shiboken.wrapInstance(long(ptr), QtGui.QWidget) # PySide command
#return sip.wrapinstance(long(ptr), QtGui.QWidget) # PyQt command
""" Add menus to Maya """
def addMenuItem(parentMenu=None, labelName='', callback=None, sub=False, tearable=False):
# If we do not specify a callback we are making a new sub-menu
if callback == None:
newMenu = cmds.menuItem(parent=parentMenu, label=labelName, subMenu=sub, tearOff=tearable)
return newMenu
else:
cmds.menuItem(p=parentMenu, label=labelName, command=callback)
def addSeparator(parentMenu):
cmds.menuItem(parent=parentMenu, divider=True)
def addToolBarFunction(icon, statusLine, callback):
""" Sets up a toolbar icon and function """
pixmap = QtGui.QPixmap(icon)
icon = QtGui.QIcon(pixmap)
iconSize = QtCore.QSize(32, 32)
btnSize = QtCore.QSize(32, 32)
btn = QtGui.QPushButton(statusLine)
btn.setFlat(True)
btn.setText("")
btn.setIcon(icon)
btn.setMinimumSize(btnSize)
btn.setMaximumSize(btnSize)
btn.setIconSize (iconSize)
btn.clicked.connect(callback)
statusLine.layout().addWidget(btn)
def setupMenuBar():
"""
Sets up a menu bar
Passing in a string representation of our script allows it to be pinnable to our Maya
Ctrl + Shift + click to pin to shelf
"""
# Create a main menu that we will place on the main maya toolbar
MAIN_MENU = cmds.menu(label='CUSTOM MENU', parent='MayaWindow', tearOff=True)
# Sub Menus of Main Menu
POLY_ACTIONS = addMenuItem(parentMenu=MAIN_MENU, labelName='Poly Actions', sub=True, tearable=True)
QT_ACTIONS = addMenuItem(parentMenu=MAIN_MENU, labelName='Qt Actions', sub=True, tearable=True)
# Add our sub menus to the main menu
# Poly actions
addMenuItem(parentMenu=POLY_ACTIONS, labelName='Create Cube', callback='createCube()')
addSeparator(parentMenu=POLY_ACTIONS) # Adding a separator example
addMenuItem(parentMenu=POLY_ACTIONS, labelName='Create Sphere', callback='createSphere()')
# Qt Actions
addMenuItem(parentMenu=QT_ACTIONS, labelName='Display Message Box', callback='createInfoBox()')
# Print Actions
addSeparator(parentMenu=MAIN_MENU)
addMenuItem(parentMenu=MAIN_MENU, labelName='Display Dummy Dialog', callback='printExample()')
def setupToolBar():
statusLine = getMayaStatusLine()
#currentDir = os.path.dirname(os.path.realpath('__file__'))
iconDirectory = r'C:\projects\git_projects\maya_custom_menus\icons'
clickMeIcon = os.path.join(iconDirectory, 'click_me.png')
thumbsUpIcon = os.path.join(iconDirectory, 'thumb_up.png')
print thumbsUpIcon
addToolBarFunction(icon=clickMeIcon, statusLine=statusLine, callback=clickMe)
addToolBarFunction(icon=thumbsUpIcon, statusLine=statusLine, callback=thumbsUp)
""" Simple Menu Functions """
def createCube():
cmds.polyCube()
def createSphere():
cmds.polySphere()
def createInfoBox(parent=None):
QtGui.QMessageBox.information(parent, 'Hello there', 'I am a message box')
return
def printExample():
print 'I am printing!'
def clickMe():
print 'I have been clicked!!!'
def thumbsUp(parent=None):
QtGui.QMessageBox.information(parent, 'Well Then', 'Thumbs up!!!')
return
if __name__ == '__main__':
setupToolBar()
setupMenuBar()