-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpresetsUI.py
278 lines (231 loc) · 8.37 KB
/
presetsUI.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from baseMelUI import *
from filesystem import *
import maya.cmds as cmd
import api
ui = None
class PresetOptionMenu(MelOptionMenu):
def __init__( self, parent, tool, extension, *a, **kw ):
MelOptionMenu.__init__( self, parent, *a, **kw )
self.setChangeCB( self.on_change )
self._manager = PresetManager( tool, extension )
self._presets = {}
self.update()
def update( self ):
self.clear()
for locale, presets in self._manager.listAllPresets( True ).iteritems():
for preset in presets:
self.append( preset.name() )
self._presets[ preset.name() ] = preset
def getValue( self ):
valueStr = MelOptionMenu.getValue( self )
return self._presets.get( valueStr, None )
### EVENT HANDLERS ###
def on_change( self, *a ):
self.sendEvent( 'presetChanged', self.getValue() )
class PresetLayout(MelFormLayout):
ALLOW_MULTI_SELECTION = True
def __new__( cls, parent, *a, **kw ):
return MelForm.__new__( cls, parent )
def __init__( self, parent, tool, locale=LOCAL, ext=DEFAULT_XTN ):
MelForm.__init__( self, parent )
self.tool = tool
self.locale = locale
self.ext = ext
self.presetManager = PresetManager(tool, ext)
self.populate()
def populate( self ):
children = self( q=True, ca=True )
if children is not None:
for c in children:
cmd.deleteUI( c )
other = self.other()
otherLbl = "<-- %s" % other
cmd.setParent( self )
self.UI_lbl_title = cmd.text(l='Managing "%s" presets' % self.ext)
self.UI_lbl_presets = cmd.text(l="%s presets" % self.locale)
self.UI_button_swap = cmd.button(h=18, l="view %s presets" % other, c=self.swap)
self.UI_tsl_presets = cmd.textScrollList(allowMultiSelection=self.ALLOW_MULTI_SELECTION, sc=self.updateButtonStatus)
self.UI_button_1 = cmd.button(l="move to %s" % other, c=self.move)
self.UI_button_2 = cmd.button(l="copy to %s" % other, c=self.copy)
self.UI_button_3 = cmd.button(l="rename", c=self.rename)
self.UI_button_4 = cmd.button(l="delete", c=self.delete)
self.POP_filemenu = cmd.popupMenu(b=3, p=self.UI_tsl_presets, pmc=self.popup_filemenu)
self( e=True,
af=((self.UI_lbl_title, "top", 5),
(self.UI_lbl_title, "left", 5),
(self.UI_lbl_presets, "left", 10),
(self.UI_button_swap, "right", 5),
(self.UI_tsl_presets, "left", 5),
(self.UI_tsl_presets, "right", 5),
(self.UI_button_1, "left", 5),
(self.UI_button_2, "right", 5),
(self.UI_button_3, "left", 5),
(self.UI_button_3, "bottom", 5),
(self.UI_button_4, "right", 5),
(self.UI_button_4, "bottom", 5)),
ac=((self.UI_lbl_presets, "top", 10, self.UI_lbl_title),
(self.UI_button_swap, "top", 7, self.UI_lbl_title),
(self.UI_button_1, "bottom", 0, self.UI_button_3),
(self.UI_button_swap, "left", 10, self.UI_lbl_presets),
(self.UI_tsl_presets, "top", 10, self.UI_lbl_presets),
(self.UI_tsl_presets, "bottom", 5, self.UI_button_1),
(self.UI_button_2, "bottom", 0, self.UI_button_4)),
ap=((self.UI_button_1, "right", 0, 50),
(self.UI_button_2, "left", 0, 50),
(self.UI_button_3, "right", 0, 50),
(self.UI_button_4, "left", 0, 50)) )
self.updateList()
def other( self ):
'''
returns the "other" locale
'''
return LOCAL if self.locale == GLOBAL else GLOBAL
def updateList( self ):
'''
refreshes the preset list
'''
presets = self.presetManager.listPresets(self.locale)
cmd.textScrollList(self.UI_tsl_presets, e=True, ra=True)
self.presets = presets
for p in presets:
cmd.textScrollList(self.UI_tsl_presets, e=True, a=p[-1])
self.updateButtonStatus()
def updateButtonStatus( self, *args ):
selected = self.selected()
numSelected = len(selected)
if numSelected == 0:
cmd.button(self.UI_button_1, e=1, en=0)
cmd.button(self.UI_button_2, e=1, en=0)
cmd.button(self.UI_button_3, e=1, en=0)
cmd.button(self.UI_button_4, e=1, en=0)
elif numSelected == 1:
cmd.button(self.UI_button_1, e=1, en=1)
cmd.button(self.UI_button_2, e=1, en=1)
cmd.button(self.UI_button_3, e=1, en=1)
cmd.button(self.UI_button_4, e=1, en=1)
else:
cmd.button(self.UI_button_1, e=1, en=1)
cmd.button(self.UI_button_2, e=1, en=1)
cmd.button(self.UI_button_3, e=1, en=0)
cmd.button(self.UI_button_4, e=1, en=1)
def selected( self ):
'''
returns the selected presets as Path instances - if nothing is selected, an empty list is returned
'''
try:
selectedIdxs = [idx-1 for idx in cmd.textScrollList(self.UI_tsl_presets, q=True, sii=True)]
selected = [self.presets[n] for n in selectedIdxs]
return selected
except TypeError: return []
def getSelectedPresetNames( self ):
selected = cmd.textScrollList( self.UI_tsl_presets, q=True, si=True ) or []
return [ Path( s ).name() for s in selected ]
def getSelectedPresetName( self ):
try: return self.getSelectedPresetNames()[ 0 ]
except IndexError:
return None
def copy( self, *args ):
files = []
for s in self.selected():
files.append( s.copy() )
self.sendEvent( 'presetsCopied', files )
def delete( self, *args ):
files = self.selected()
for s in files:
s.delete()
self.updateList()
self.sendEvent( 'presetsDeleted', files )
def move( self, *args ):
files = []
movedFiles = []
for s in self.selected():
ff = s.move()
movedFiles.append( ff )
self.updateList()
self.sendEvent( 'presetsMoved', files )
def rename( self, *args ):
'''
performs the prompting and renaming of presets
'''
selected = self.selected()[0]
ans, newName = api.doPrompt(m='new name', tx=selected.name())
if ans != api.OK:
return
if not newName.endswith('.'+ self.ext):
newName += '.'+ self.ext
renamedPreset = selected.rename( newName )
self.updateList()
self.sendEvent( 'presetRenamed', selected, renamedPreset )
def swap( self, *args ):
'''
performs the swapping from the local to global locale
'''
self.locale = self.other()
self.populate()
def syncall( self, *a ):
'''
syncs to ALL global presets for the current tool - NOTE: this syncs to all global preset dirs in
the mod hierarchy...
'''
dirs = getPresetDirs(self.locale, self.tool)
for dir in dirs:
#P4Data(dir).sync()
print 'syncing to %s...' % dir.resolve().asdir()
self.updateList()
def on_notepad( self, filepath ):
filepath = Path( filepath )
subprocess.Popen( 'notepad "%s"' % filepath.asNative(), cwd=filepath.up() )
def popup_filemenu( self, parent, *args ):
cmd.menu(parent, e=True, dai=True)
cmd.setParent(parent, m=True)
other = self.other()
items = self.selected()
numItems = len(items)
if numItems:
cmd.menuItem(l='copy to %s' % other, c=self.copy)
cmd.menuItem(l='move to %s' % other, c=self.move)
if len(items) == 1:
filepath = items[0].resolve()
cmd.menuItem(d=True)
cmd.menuItem(l='open in notepad', c=lambda *x: self.on_notepad( filepath ))
cmd.menuItem(d=True)
api.addExploreToMenuItems(filepath)
cmd.menuItem(d=True)
cmd.menuItem(l='delete', c=self.delete)
#if the file is a global file, display an option to sync to presets
if self.locale == GLOBAL:
if numItems: cmd.menuItem(d=True)
cmd.menuItem(l='sync to presets', c=self.syncall)
#if no files are selected, prompt the user to select files
if numItems == 0: cmd.menuItem(en=False, l='select a preset file')
PresetForm = PresetLayout
class PresetWindow(BaseMelWindow):
WINDOW_NAME = 'presetWindow'
WINDOW_TITLE = 'Preset Manager'
DEFAULT_SIZE = 275, 325
DEFAULT_MENU = 'Perforce'
FORCE_DEFAULT_SIZE = True
def __new__( cls, *a, **kw ):
return BaseMelWindow.__new__( cls )
def __init__( self, tool, locale=LOCAL, ext=DEFAULT_XTN ):
BaseMelWindow.__init__( self )
self.editor = PresetForm( self, tool, locale, ext )
cmd.setParent( self.getMenu( self.DEFAULT_MENU ), m=True )
cmd.menuItem(l='Sync to Global Presets', c=lambda *a: self.editor.syncall())
self.show()
def presetsCopied( self, presets ):
pass
def presetsDeleted( self, presets ):
pass
def presetsMoved( self, presets ):
pass
def presetRenamed( self, preset, renamedPreset ):
pass
PresetUI = PresetWindow
def load( tool, locale=LOCAL, ext=DEFAULT_XTN ):
'''
this needs to be called to load the ui properly in maya
'''
global ui
ui = PresetUI(tool, locale, ext)
#end