-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand_panel.py
509 lines (451 loc) · 13.6 KB
/
command_panel.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
from inspect import signature
import os
if False:
from _stubs import *
class Context:
def __init__(self, ownerComp):
self.ownerComp = ownerComp
def resolveOP(self, o):
if isinstance(o, str):
return self.ownerComp.op(o)
return o
def openUI(self, o, unique=True, borders=True):
o = self.resolveOP(o)
if not o:
return
if o.type == 'window':
o.par.winopen.pulse()
else:
o.openViewer(unique=unique, borders=borders)
def closeUI(self, o, topMost=False):
o = self.resolveOP(o)
if not o:
return
if o.type == 'window':
o.par.winclose.pulse()
else:
o.closeViewer(topMost=topMost)
def openOrToggleUI(self, o, borders=True):
o = self.resolveOP(o)
if not o:
return
if o.type == 'window':
self.toggleUI(o, borders=borders)
else:
self.openUI(o, unique=True, borders=borders)
def toggleUI(self, o, borders=True):
o = self.resolveOP(o)
if not o:
return
if o.type == 'window':
if o.isOpen:
self.openUI(o, unique=True, borders=borders)
else:
self.closeUI(o, topMost=False)
else:
raise NotImplementedError('toggleUI only supported for Window COMP')
@property
def activeEditor(self):
pane = ui.panes.current
if pane.type == PaneType.NETWORKEDITOR:
return pane
for pane in ui.panes:
if pane.type == PaneType.NETWORKEDITOR:
return pane
def getSelectedOps(self, predicate=None):
pane = self.activeEditor
if not pane:
return []
sel = pane.owner.selectedChildren or [pane.owner.currentChild]
if predicate is not None:
sel = list(filter(predicate, sel))
return sel
def getSelectedOrContext(self, predicate):
sel = self.getSelectedOps(predicate)
if sel:
return sel[0]
pane = self.activeEditor
if not pane:
return
o = pane.owner
while o:
if predicate(o):
return o
o = o.parent()
def navigateTo(self, o):
o = self.resolveOP(o)
if not o:
return
pane = self.activeEditor
if pane:
pane.owner = o
def openNetwork(self, o):
o = self.resolveOP(o)
if not o or not o.isCOMP:
return
pane = ui.panes.createFloating(type=PaneType.NETWORKEDITOR)
pane.owner = o
@staticmethod
def resolveFile(path):
return path if os.path.exists(path) else ''
def saveOP(self, o, path=None):
o = self.resolveOP(o)
if not o:
return False
if path is None:
if o.isDAT and getattr(o.par, 'file') and hasattr(o.par, 'writepulse'):
path = o.par.file
o.par.writepulse.pulse()
ui.status = 'saved {} to {}'.format(o.path, path)
return True
if o.isCOMP:
path = o.par.externaltox.eval()
elif hasattr(o.par, 'file'):
path = o.par.file.eval()
if not path:
return False
o.save(path)
ui.status = 'saved {} to {}'.format(o.path, path)
return True
def reloadOPFile(self, o):
o = self.resolveOP(o)
if not o:
return False
if o.isDAT and hasattr(o.par, 'loadonstartpulse'):
o.par.loadonstartpulse.pulse()
return True
if o.isCOMP:
o.par.reinitnet.pulse()
return True
return False
def _callWithOptionalParam(fn, param):
if len(signature(fn).parameters) == 0:
return fn()
else:
return fn(param)
class Command:
def __init__(self, action, **attrs):
self.action = action
self.attrs = attrs or {}
@property
def label(self): return self.attrs.get('label') or '--'
@property
def help(self): return self.attrs.get('help') or ''
@property
def img(self):
i = self.attrs.get('img')
return op(i) if isinstance(i, str) else i
@property
def isIcon(self):
return self.attrs.get('isIcon')
def invoke(self, context):
_callWithOptionalParam(self.action, context)
@classmethod
def forOpenUI(cls, o, unique=True, borders=True, **kwargs):
return cls(lambda context: context.openUI(o, unique=unique, borders=borders), **kwargs)
@classmethod
def forToggleUI(cls, o, borders=True, **kwargs):
return cls(lambda context: context.openOrToggleUI(o, borders=borders), **kwargs)
@classmethod
def forEdit(cls, o, **kwargs):
return cls(lambda context: context.navigateTo(o), **kwargs)
@classmethod
def forParams(cls, oppath, **kwargs):
def _action(context):
o = context.resolveOP(oppath)
if o:
o.openParameters()
return cls(_action, **kwargs)
@classmethod
def forReload(cls, oppaths, **kwargs):
def _action(context):
targetops = ops(oppaths)
for o in targetops:
context.reloadOPFile(o)
return cls(_action, **kwargs)
@classmethod
def forSave(cls, oppaths, path, **kwargs):
def _action(context):
targetops = ops(oppaths)
for o in targetops:
context.saveOP(o, path)
return cls(_action, **kwargs)
@classmethod
def forExec(
cls, oppath, args=None, endFrame=False, fromOP=None,
group=None, delayFrames=0, delayMilliSeconds=0, **kwargs):
def _action(context):
o = context.resolveOP(oppath)
if o and o.isDAT:
o.run(
*args,
endFrame=endFrame, fromOP=fromOP, group=group,
delayFrames=delayFrames, delayMilliSeconds=delayMilliSeconds,
**kwargs)
return cls(_action, **kwargs)
@classmethod
def forScriptDAT(cls, dat):
if 'def action' not in dat.text:
return cls.forExec(dat.path, label=dat.name)
else:
locs = dat.locals
return cls(
lambda context: _callWithOptionalParam(dat.module.action, context),
label=locs.get('label', None) or dat.name,
**{
k: locs[k]
for k in ['img', 'help', 'isIcon']
if k in locs
})
@classmethod
def forAction(cls, action, **kwargs):
return cls(action, **kwargs)
@classmethod
def fromRow(cls, dat, row):
obj = {
dat[0, i].val: dat[row, i].val
for i in range(dat.numCols)
}
return Command.fromObj(obj)
@classmethod
def fromObj(cls, obj):
if _asbool(obj.get('hidden'), False):
return None
typename = obj.get('type')
attrs = {
'label': obj.get('label'),
'help': obj.get('help'),
'img': obj.get('img'),
'isIcon': obj.get('isIcon'),
}
o = obj.get('op')
if not typename and 'script' in obj:
typename = 'script'
if not typename:
action = obj.get('action', _noop)
return Command.forAction(action, **attrs)
if typename in ['open', 'view']:
return Command.forOpenUI(
o,
unique=_asbool(obj.get('unique'), True),
borders=_asbool(obj.get('borders'), True),
**attrs)
elif typename in ['toggle']:
return Command.forToggleUI(
o,
borders=_asbool(obj.get('borders'), False),
**attrs)
elif typename in ['pars']:
return Command.forParams(o, **attrs)
elif typename in ['edit', 'navigate']:
return Command.forEdit(o, **attrs)
elif typename in ['run']:
return Command.forExec(
o,
delayFrames=_asint(obj.get('delayFrames'), 0),
**attrs)
elif typename == 'script':
code = obj.get('script')
if not code:
return Command.forAction(_noop, **attrs)
if code.startswith('lambda'):
return Command.forAction(eval(code), **attrs)
else:
return Command.forAction(lambda _: eval(code), **attrs)
elif typename in ['reload']:
return Command.forReload(o, **attrs)
elif typename in ['save']:
return Command.forSave(
o,
path=obj.get('path') or obj.get('file'),
**attrs)
def _noop(*args, **kwargs):
pass
def _asbool(val, defval):
if val is None or val == '':
return defval
if val == '1':
return True
if val == '0':
return False
return bool(val)
def _asint(val, defval):
if val is None or val == '':
return defval
return int(val)
def _strornull(val):
return str(val) if val is not None else None
class CommandPanel:
def __init__(self, comp):
self.ownerComp = comp
self.commandlist = []
def _AddCommand(self, command):
if command:
self.commandlist.append(command)
def _AddCommands(self, commands):
if commands:
for command in commands:
self._AddCommand(command)
def _AddCommandsFromTable(self, dat):
if not dat or dat.numRows < 2:
return
for row in range(1, dat.numRows):
self._AddCommand(Command.fromRow(dat, row))
def _AddCommandsFromScriptDATs(self):
scripts = self.ownerComp.par.Cmdscripts.evalOPs()
for script in scripts:
self._AddCommand(Command.forScriptDAT(script))
def _AddCommandsFromList(self):
cmdobjs = self.ownerComp.par.Cmdobjs.eval()
if cmdobjs:
if isinstance(cmdobjs, (list, tuple)):
for cmdobj in cmdobjs:
self._AddCommand(Command.fromObj(cmdobj))
else:
self._AddCommand(Command.fromObj(cmdobjs))
def RebuildCommands(self):
self.commandlist.clear()
self._AddCommandsFromTable(self.ownerComp.op('./command_table_in'))
if self.ownerComp.par.Includebasictoolcmds:
self._AddCommands(_basicToolCommands)
if self.ownerComp.par.Includetestcmds:
self._AddCommandsFromTable(self.ownerComp.op('./TEST_commands'))
self._AddCommandsFromList()
self._AddCommandsFromScriptDATs()
def BuildCommandTable(self, dat):
self.RebuildCommands()
dat.clear()
dat.appendRow(['label', 'help', 'img', 'isIcon'])
for command in self.commandlist:
dat.appendRow([
command.label,
command.help,
command.img or '',
bool(command.isIcon),
])
def _GetCommandByIndex(self, index):
if index < 0 or index >= len(self.commandlist):
return None
return self.commandlist[index]
def ExecuteCommandByIndex(self, index):
command = self._GetCommandByIndex(index)
if not command:
return
context = Context(self.ownerComp)
command.invoke(context)
def InitializeButton(self, button, index):
command = self._GetCommandByIndex(index) # type: Command
button.par.display = True
button.par.Buttonofflabel = command.label
button.par.Buttononlabel = command.label
button.par.Offtoonscript0 = 'iop.commands.ExecuteCommandByIndex({})'.format(index)
img = command.img
imgpath = img.path if img is not None else ''
button.par.Buttonofftop = imgpath
button.par.Buttonontop = imgpath
button.par.Popuphelp = command.help
if command.isIcon and not img:
button.par.Buttonfont = 'Material_Design_Icons'
def _copyPaths(context: Context):
sel = context.getSelectedOps()
ui.clipboard = ' '.join([o.path for o in sel])
def _saveTox(context: Context):
comp = context.getSelectedOrContext(lambda o: o.isCOMP and o.par.externaltox)
if comp:
context.saveOP(comp)
def _incrementComponentVersion(context: Context):
comp = context.getSelectedOrContext(lambda o: o.isCOMP and hasattr(o.par, 'Compversion'))
if not comp:
comp = context.getSelectedOrContext(lambda o: o.isCOMP)
if not comp:
ui.status = 'Unable to increment component version, no suitable COMP'
return
ui.status = 'Updating component version of {!r}'.format(comp)
if not hasattr(comp.par, 'Compversion'):
page = comp.appendCustomPage(':meta')
par = page.appendInt('Compversion', label=':Version')[0]
par.val = 0
par.default = 0
par.readOnly = True
else:
par = comp.par.Compversion
par.val += 1
par.default = par.val
if par.normMax < par.val:
par.normMax = par.val
par.readOnly = True
def _makeLastPage(comp, page):
if len(comp.customPages) == 1 and comp.customPages[0] == page:
return
orderedpages = [p.name for p in comp.customPages if p != page] + [page.name]
comp.sortCustomPages(*orderedpages)
def _addOrUpdatePar(appendmethod, name, label, value=None, expr=None, readonly=None, setdefault=False):
p = appendmethod(name, label=label)[0]
if expr is not None:
p.expr = expr
if setdefault:
p.defaultExpr = expr
elif value is not None:
p.val = value
if setdefault:
p.default = value
if readonly is not None:
p.readOnly = readonly
return p
def _addOrUpdateMetadataPar(appendmethod, name, label, value):
_addOrUpdatePar(appendmethod, name, label, value, readonly=True, setdefault=True)
def _setComponentMetadata(
context: Context,
description=None,
version=None,
typeid=None,
website=None,
author=None,
page=':meta'):
comp = context.getSelectedOrContext(lambda o: o.isCOMP)
if not comp:
ui.status = 'Unable to create metadata, no target comp'
return
page = comp.appendCustomPage(page)
if page.name.startswith(':'):
_makeLastPage(comp, page)
if typeid:
_addOrUpdateMetadataPar(page.appendStr, 'Comptypeid', ':Type ID', typeid)
_addOrUpdateMetadataPar(page.appendStr, 'Compdescription', ':Description', description)
_addOrUpdateMetadataPar(page.appendInt, 'Compversion', ':Version', version)
_addOrUpdateMetadataPar(page.appendStr, 'Compwebsite', ':Website', website)
_addOrUpdateMetadataPar(page.appendStr, 'Compauthor', ':Author', author)
page.sort('Comptypeid', 'Compdescription', 'Compversion', 'Compwebsite', 'Compauthor')
def _createTektTdCompsMetadata(context: Context):
_setComponentMetadata(
context,
website='https://github.com/optexture/td-components',
author='[email protected]')
def _destroyCustomPars(context: Context):
for targetOp in context.getSelectedOps(lambda o: hasattr(o, 'destroyCustomPars')):
targetOp.destroyCustomPars()
_basicToolCommands = [
Command.forAction(
_copyPaths,
label='copy path',
help='copy paths of selected ops'),
Command.forAction(
_saveTox,
label='save',
help='save selected or active component tox file'),
Command.forAction(
_incrementComponentVersion,
label='version++',
help='increment the component version attribute on selected or active',
),
Command.forAction(
_destroyCustomPars,
label='kill pars',
help='destroy all custom parameters on the selected OP(s)'
),
Command.forAction(
_createTektTdCompsMetadata,
label='[t] meta',
help='add tekt TD-components metadata to current component',
),
]