/
menus.py
170 lines (130 loc) · 6.08 KB
/
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
mGui.menus
Wrapper classes for Menus and MenuItems
"""
from mGui.core import Nested, Control, cmds, inspect
class Menu(Nested):
CMD = cmds.menu
_ATTRIBS = ['allowOptionBoxes', 'deleteAllItems', 'defineTemplate', 'docTag', 'enable', 'enableBackground',
'exists', 'familyImage', 'helpMenu', 'label', 'mnemonic', 'parent', 'useTemplate', 'visible',
'postMenuCommandOnce']
_CALLBACKS = ['postMenuCommand']
_READ_ONLY = ['numberOfItems', 'itemArray']
ACTIVE_MENU = None
ADD_TO_LAYOUT = False
def __enter__(self):
self._cache_menu = Menu.ACTIVE_MENU
Menu.ACTIVE_MENU = self
return self
def __exit__(self, typ, value, tb):
# see Nested.__exit___ for more details
if typ and not self.ignore_exceptions:
return False
owning_scope = inspect.currentframe().f_back
for key, value in owning_scope.f_locals.items():
if type(value) not in (MenuItem, CheckBoxMenuItem, RadioMenuItem, RadioMenuItemCollection, SubMenu, Menu):
continue
if hasattr(value, 'widget'):
parentpath = value.widget.rpartition("|")[0]
if parentpath == self.widget:
self.add(value, key)
# restore the layout level
Menu.ACTIVE_MENU = self._cache_menu
self._cache_menu = None
cmds.setParent(Menu.ACTIVE_MENU, menu=True)
class SubMenu(Menu):
CMD = cmds.menu
def __init__(self, key=None, **kwargs):
# When creating a submenu, we use the menuItem command, however it returns a menu.
# So we shadow the class attribute during initialization
self.CMD = cmds.menuItem
kwargs['subMenu'] = True
super(SubMenu, self).__init__(key, **kwargs)
# And remove the shadow once we've finished.
del self.CMD
self._cache_menu = None
class MenuItem(Control):
CMD = cmds.menuItem
_ATTRIBS = ["altModifier", "annotation", "allowOptionBoxes", "boldFont", "checkBox", "collection",
"commandModifier", "ctrlModifier", "divider", "data", "defineTemplate", "docTag", "echoCommand",
"enableCommandRepeat", "enable", "exists", "familyImage", "image", "insertAfter", "imageOverlayLabel",
"italicized", "keyEquivalent", "label", "mnemonic", "optionBox", "optionBoxIcon", "optionModifier",
"parent", "radioButton", "radialPosition", "shiftModifier", "subMenu", "sourceType", "tearOff",
"useTemplate", "version", 'postMenuCommandOnce']
_READ_ONLY = ['isCheckBox', 'isOptionBox', 'isRadioButton']
_CALLBACKS = ['command', 'dragDoubleClickCommand', 'dragMenuCommand', 'postMenuCommand']
ADD_TO_LAYOUT = False
def __init__(self, key=None, **kwargs):
super(MenuItem, self).__init__(key, **kwargs)
self.owner = None
class MenuDivider(MenuItem):
def __init__(self, key=None, **kwargs):
kwargs['divider'] = True
super(MenuDivider, self).__init__(key, **kwargs)
class RadioMenuItemCollection(Control):
CMD = cmds.radioMenuItemCollection
_READ_ONLY = ['exists', 'defineTemplate', 'gl', 'parent', 'useTemplate']
ADD_TO_LAYOUT = False
def __init__(self, key=None, **kwargs):
super(RadioMenuItemCollection, self).__init__(key, **kwargs)
self.owner = None
def __enter__(self):
return self
def __exit__(self, typ, value, tb):
mGui_expand_stack = True
# This closes out the collection, usually we'd do a setParent, but that doesn't seem to apply here.
self.CMD()
return False
class RadioMenuItem(MenuItem):
def __init__(self, key=None, **kwargs):
kwargs['radioButton'] = kwargs.get('radioButton', kwargs.get('rb', False))
super(RadioMenuItem, self).__init__(key, **kwargs)
class CheckBoxMenuItem(MenuItem):
def __init__(self, key=None, **kwargs):
kwargs['checkBox'] = kwargs.get('checkBox', kwargs.get('cb', False))
super(CheckBoxMenuItem, self).__init__(key, **kwargs)
class OptionMenu(Nested):
CMD = cmds.optionMenu
_ATTRIBS = ['alwaysCallChangeCommand', 'annotation', 'backgroundColor', 'docTag', 'enableBackground', 'exists',
'height', 'label', 'manage', 'parent', 'preventOverride', 'select', 'value', 'visible', 'width']
_READ_ONLY = ['fullPathName', 'itemListLong', 'itemListShort', 'isObscured', 'numberOfItems', 'numberOfPopupMenus',
'popupMenuArray']
_CALLBACKS = ['changeCommand', 'dragCallback', 'dropCallback', 'visibleChangeCommand']
_BIND_SRC = 'value'
_BIND_TGT = 'items'
_BIND_TRIGGER = 'changeCommand'
@property
def items(self):
return [i.tag for i in self.controls]
@items.setter
def items(self, value):
selected = self.select
self.clear()
self.controls[:] = [MenuItem(val, parent=self, tag=val) for val in value]
if selected:
self.select = selected
def clear(self):
for long_name in self.itemListLong or []:
cmds.deleteUI(long_name)
class ActiveOptionMenu(OptionMenu):
"""
A variant of the default OptionMenu which will call the command attached to
the menuItem. This allows for dropdown menus which behave like regular
menus rather than like pure dropdown selectors
"""
def __init__(self, key=None, *args, **kwargs):
super(ActiveOptionMenu, self).__init__(key, *args, **kwargs)
self.changeCommand += self.fire_menu_callback
def fire_menu_callback(self, *args, **kwargs):
"""
this ensures that the command attached to the selected MenuItem is fired when that menu is selected
"""
selected = self.controls[self.select - 1]
selected.command()
class PopupMenu(Menu):
CMD = cmds.popupMenu
_ATTRIBS = ['altModifier', 'allowOptionBoxes', 'button', 'ctrlModifier', 'deleteAllItems', 'defineTemplate',
'exists', 'markingMenu', 'parent', 'shiftModifier', 'useTemplate', 'visible', 'postMenuCommandOnce']
_CALLBACKS = ['postMenuCommand']
_READ_ONLY = ['numberOfItems', 'itemArray']
ADD_TO_LAYOUT = True