-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhuilib.py
executable file
·602 lines (484 loc) · 18.5 KB
/
huilib.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
import hou
import tempfile
import types
import collections
def _attributes_to_string(obj, atrr_dict):
for k, v in atrr_dict.items():
if isinstance(v, bool) and v == True or v == None:
obj.attributes_string += '%s '% k.upper()
elif v == False:
continue
elif isinstance(v, (tuple, list)):
v = list(map(str,v))
obj.attributes_string += "%s(%s,%s) " % (k.upper(), v[0], v[1])
else:
obj.attributes_string += "%s(%s) " % (k.upper(), str(v))
def _randname():
from random import choice
from string import ascii_lowercase
return ''.join(choice(ascii_lowercase) for x in range(4))
def findDialog(name):
"""
Finds dialog by name. Dialog can be destroyed (destroy()) or shown (show())
"""
uival = "%s_ui.val" % name
def show(self):
self.setValue(uival, 1)
for dlg in hou.ui.dialogs():
try:
val = dlg.value(uival)
except hou.OperationFailed:
continue
else:
dlg.show = types.MethodType(show, dlg)
return dlg
return None
class HBaseContainer(object):
def __init__(self):
self.child_list = []
self.attributes_string = ""
self.attributes = dict(hstretch = True)
def addGadget(self, gadget):
self.child_list.append(gadget)
def addLayout(self, layout):
self.child_list.append(layout)
def setAttributes(self, **kwargs):
self.attributes.update(kwargs)
class HBaseGadget(object):
def __init__(self, name, label):
self.name = "%s.gad" % name
self.label = label
self._ui_value = "%s.val" % name
self.attributes = dict(hstretch = True)
self.attributes_string = ""
self.enabled = True
self.dialog = None
self.init_value = None
self.callbacks = set()
def setAttributes(self, **kwargs):
"""
Sets various attributes for Gadget object. Attributes are:
size = [w,h]
width = unit
height = unit
min_size/max_size = [w,h]
min_width/max_width = unit
min_height/max_height = unit
stretch = bool, [w,h]
hstretch/vstretch = bool
margin = unit
hmargin/vmargin = [h,v]
spacing = unit
spacing = [h,v]
justify = [h,v]
look = "plain", "line", "groove", "bevel", "beveldown"
rmbmenu = menuref
"""
self.attributes.update(kwargs)
def setEnabled(self, value = True):
self.enabled = value
if self.dialog:
self.dialog.enableValue(self._ui_value, value)
def setValue(self, value):
if self.dialog:
self.dialog.setValue(self._ui_value, value)
else:
self.init_value = value
def getValue(self):
if self.dialog:
return self.dialog.value(self._ui_value)
else:
raise ValueError('Can\'t get value for %s gadget' % self.name)
def _set_multivalue(self, iterable):
self.dialog.setValue(self._ui_value[0], iterable[0])
self.dialog.setValue(self._ui_value[1], iterable[1])
self.dialog.setValue(self._ui_value[2], iterable[2])
def connect(self, func):
self.callbacks.add(func)
class HRowLayout(HBaseContainer):
def __init__(self):
super(HRowLayout, self).__init__()
def __repr__(self):
_attributes_to_string(self, self.attributes)
return 'ROW'
class HColumnLayout(HBaseContainer):
def __init__(self):
super(HColumnLayout, self).__init__()
def __repr__(self):
_attributes_to_string(self, self.attributes)
return 'COL'
class HCollapserLayout(HBaseContainer):
def __init__(self, label = 'Collapser', layout = 'horizontal'):
super(HCollapserLayout, self).__init__()
self.attributes['layout'] = layout
self._label = label
def __repr__(self):
_attributes_to_string(self, self.attributes)
return 'COLLAPSER "%s"' % self._label
class HButton(HBaseGadget):
def __init__(self, name, label):
super(HButton, self).__init__(name, label)
def setAttributes(self, **kwargs):
try:
# For some reason setting the look attrib on a button, makes it disappear
del kwargs['look']
except KeyError:
pass
super(HButton, self).setAttributes(**kwargs)
def __repr__(self):
_attributes_to_string(self, self.attributes)
_s = "ACTION_BUTTON \"{label}\" VALUE({value}) ".format(
label = self.label, value = self._ui_value)
_s += self.attributes_string
_s += ';'
return _s
class HIconButton(HBaseGadget):
def __init__(self, name, icon):
super(HIconButton, self).__init__(name, "")
self._icon = icon
self.attributes = {"hstretch" : False}
def setIcon(self, iconpath):
self._icon = iconpath
def __repr__(self):
_attributes_to_string(self, self.attributes)
_s = "ACTION_ICONBUTTON \"{icon}\" VALUE({value}) ".format(
icon = self._icon, value = self._ui_value)
_s += self.attributes_string
_s += ';'
return _s
class HCheckbox(HBaseGadget):
def __init__(self, name, label):
super(HCheckbox, self).__init__(name, label)
def isChecked(self):
return self.getValue()
def __repr__(self):
_attributes_to_string(self, self.attributes)
_s = "TOGGLE_BUTTON \"{label}\" VALUE({value}) ".format(
label = self.label, value = self._ui_value)
_s += self.attributes_string
_s += ';'
return _s
class HSeparator(HBaseGadget):
def __init__(self):
super(HSeparator, self).__init__("", "")
def __repr__(self):
_attributes_to_string(self, self.attributes)
return "SEPARATOR %s;" % self.attributes_string
class HRadioButton(HBaseGadget):
def __init__(self, name, label):
super(HRadioButton, self).__init__(name, label)
def isChecked(self):
return self.getValue()
def __repr__(self):
_attributes_to_string(self, self.attributes)
_s = "RADIO_BUTTON \"{label}\" VALUE({value}) ".format(
label = self.label, value = self._ui_value)
_s += self.attributes_string
_s += ';'
return _s
class HLabel(HBaseGadget):
def __init__(self, label):
super(HLabel, self).__init__(name = "label_%s" % _randname(), label = label)
def setEnabled(self, value = True):
pass
def __repr__(self):
_attributes_to_string(self, self.attributes)
_s = "LABEL \"{label}\" ".format(label = self.label)
_s += self.attributes_string
_s += ';'
return _s
class HStringField(HBaseGadget):
def __init__(self, name, label):
super(HStringField, self).__init__(name, label)
def __repr__(self):
_attributes_to_string(self, self.attributes)
_s = "STRING_FIELD \"{label}\" VALUE({value}) ".format(
label = self.label, value = self._ui_value)
_s += self.attributes_string
_s += ';'
return _s
class HFloatSlider(HBaseGadget):
def __init__(self, name, label, noInputField = False):
super(HFloatSlider, self).__init__(name, label)
self.no_field = noInputField
def setRange(self, srange):
self.range = srange
def lockRange(self):
self.lock_range = True
def __repr__(self):
_attributes_to_string(self, self.attributes)
if self.no_field:
slidertype = 'FLOAT_SLIDER'
else:
slidertype = 'FLOAT_SLIDER_FIELD'
_s = "{slider} \"{label}\" VALUE({value}) {attrs} ".format(
label = self.label, value = self._ui_value, attrs = self.attributes_string, slider = slidertype)
if hasattr(self, 'range'):
_s += "RANGE(%f, %f) " % (self.range[0], self.range[1])
if hasattr(self, 'lock_range'):
_s += "LOCK_RANGE "
_s += ';'
return _s
class HIntSlider(HBaseGadget):
def __init__(self, name, label, range = (1, 10), noInputField = False):
super(HIntSlider, self).__init__(name, label)
self.no_field = noInputField
self.range = range
def setRange(self, srange):
self.range = srange
def lockRange(self):
self.lock_range = True
def __repr__(self):
_attributes_to_string(self, self.attributes)
if self.no_field:
slidertype = 'INT_SLIDER'
else:
slidertype = 'INT_SLIDER_FIELD'
_s = "{name} = {slider} \"{label}\" VALUE({value}) {attrs} ".format(
name = self.name, label = self.label, value = self._ui_value,
attrs = self.attributes_string, slider = slidertype)
if hasattr(self, 'range'):
_s += "RANGE(%d, %d) " % (self.range[0], self.range[1])
if hasattr(self, 'lock_range'):
_s += "LOCK_RANGE "
_s += ';'
return _s
# class HIntSpinner(HBaseGadget):
# def __init__(self, name, label, increment = 1):
# super(HIntSpinner, self).__init__(name, label)
# self.incrementsize = increment
#
# def setRange(self, srange):
# self.range = srange
#
# def lockRange(self):
# self.lock_range = True
#
# def __repr__(self):
# _attributes_to_string(self, self.attributes)
# _s = "INT_SPINNER_FIELD({increment}) \"{label}\" VALUE({value}) {attrs} ".format(
# label = self.label, value = self._ui_value,
# attrs = self.attributes_string, increment = self.incrementsize)
#
# if hasattr(self, 'range'):
# _s += "RANGE(%d, %d) " % (self.range[0], self.range[1])
# if hasattr(self, 'lock_range'):
# _s += "LOCK_RANGE "
# _s += ';'
# return _s
class HFileField(HBaseGadget):
def __init__(self, name, label, type_filter = 'all'):
super(HFileField, self).__init__(name, label)
self.type_filter = type_filter
def __repr__(self):
_attributes_to_string(self, self.attributes)
_s = "FILENAME_FIELD({filter}) \"{label}\" VALUE({value}) {attrs}; ".format(
filter = self.type_filter, label = self.label, value = self._ui_value,
attrs = self.attributes_string)
return _s
class HColorSelector(HBaseGadget):
def __init__(self, name, label):
super(HColorSelector, self).__init__(name, label)
self._ui_value = ["%s.%s" % (self.name, comp) for comp in 'rgb']
def getValue(self):
if self.dialog:
return hou.Color([self.dialog.value(v) for v in self._ui_value])
def setValue(self, color_value):
if self.dialog:
if isinstance(color_value, hou.Color):
values = color_value.rgb()
elif isinstance(color_value, collections.Iterable):
values = tuple(color_value)
self._set_multivalue(values)
else:
self.init_value = color_value
def setEnabled(self, value = True):
self.enabled = value
if self.dialog:
for val in self._ui_value:
self.dialog.enableValue(val, value)
def __repr__(self):
_attributes_to_string(self, self.attributes)
_s = "COLOR_FIELD \"{label}\" VALUE({val}) {attrs}".format(
label = self.label, val = ", ".join(self._ui_value), attrs = self.attributes_string)
_s += ';'
return _s
class HVectorField(HBaseGadget):
def __init__(self, name, label, size = 3):
assert 2 < size <= 4, "HVectorField size can be 2, 3 or 4"
super(HVectorField, self).__init__(name, label)
self._size = size
valcomponent = "xyzw"[:self._size]
self._ui_value = ["%s.%s" % (self._ui_value, comp) for comp in valcomponent]
self._vecclass = {2: hou.Vector2, 3: hou.Vector3, 4: hou.Vector4}[size]
def getValue(self):
if self.dialog:
val = [0.0 for i in range(self._size)]
tmp = [self.dialog.value(v) for v in self._ui_value]
for i,v in enumerate(tmp):
if v:
val[i] = float(v)
return val
def setValue(self, new_value):
if not isinstance(new_value, (list, tuple, self._vecclass)):
raise ValueError("Value type should be list, tuple or hou.Vector%d" % self._size)
values = list(new_value)
if self.dialog:
self._set_multivalue(values)
else:
self.init_value = values
def setEnabled(self, value = True):
self.enabled = value
if self.dialog:
for val in self._ui_value:
self.dialog.enableValue(val, value)
def __repr__(self):
_s = "FLOAT_VECTOR_FIELD({size}) \"{label}\" VALUE({value}) {attrs};".format(
size = self._size, label = self.label,
value = ", ".join(self._ui_value),
attrs = self.attributes_string)
return _s
class _HBaseMenu(HBaseGadget):
def __init__(self, name, label, items):
super(_HBaseMenu, self).__init__(name, label)
self.items = items
def menuItems(self):
if self.dialog:
return self.dialog.menuItems(self._ui_value)
def setMenuItems(self, items):
self.items = items
if self.dialog:
self.dialog.setMenuItems(self._ui_value, self.items)
def menuDefString(self):
_s = "%s = SELECT_MENU\n{\n" % self._ui_value
for i in self.items:
_s += '\t"%s"\n' % str(i)
_s += "}"
return _s
def __repr__(self):
return ""
class HStringMenu(_HBaseMenu):
def __init__(self, name, label, items = []):
super(HStringMenu, self).__init__(name, label, items)
def __repr__(self):
_attributes_to_string(self, self.attributes)
_s = super(HStringMenu, self).__repr__()
_s += '\nSELECT_MENU_BUTTON "%s:" ' % self.label
_s += "MENU(%s);" % self._ui_value
return _s
class HIconMenu(_HBaseMenu):
def __init__(self, name, label, items = []):
super(HIconMenu, self).__init__(name, label, items)
def __repr__(self):
_attributes_to_string(self, self.attributes)
_s = super(HIconMenu, self).__repr__()
_s += '\nACTION_MENU_BUTTON "%s:" ' % self.label
_s += "MENU(%s);" % self._ui_value
return _s
class HBaseWindow(object):
def __init__(self, name, title):
self.name = name
self.type = 'WINDOW'
self.title = title
self._ui_value = "%s_ui.val" % name
self.ui_str = ""
self.attributes = dict(hstretch = True, value = self._ui_value, look = 'plain')
self.attributes_string = ""
self.items_list = []
self._gadgets_flatten_list = []
self._menu_definitions = ""
self.dialog = None
def _indentWrite(self, string):
self.ui_str += " "*4 + string + '\n'
def setWindowAttributes(self, **kwargs):
self.attributes.update(kwargs)
def addGadget(self, gadget):
self.items_list.append(gadget)
def addLayout(self, layout):
self.items_list.append(layout)
def setWindowLayout(self, layout):
if layout not in ('vertical', 'horizontal', 'cell'):
raise ValueError('Unknown layout: %s' % layout)
self.attributes_string += "LAYOUT(%s) " % layout
def _write_menus(self):
## Menus definition must be written erlier in the script
def traverse_layout(item):
if isinstance(item, _HBaseMenu):
self._indentWrite(item.menuDefString())
elif isinstance(item, HBaseContainer):
for sub in item.child_list:
traverse_layout(sub)
for item in self.items_list:
traverse_layout(item)
def _write_gadget(self, gadget):
self._indentWrite(gadget.__repr__())
def _write_layouts(self):
_attributes_to_string(self, self.attributes)
def traverse_layout(item):
if isinstance(item, HBaseGadget):
self._write_gadget(item)
self._gadgets_flatten_list.append(item)
elif isinstance(item, HBaseContainer):
self._indentWrite(item.__repr__())
self._indentWrite('{')
self._indentWrite(item.attributes_string)
for sub_item in item.child_list:
traverse_layout(sub_item)
self._indentWrite('}\n')
for item in self.items_list:
traverse_layout(item)
def _make_ui_string(self):
_attributes_to_string(self, self.attributes)
self.ui_str = "{name} = {dtype} \"{title}\"".format(
name = self.name, dtype = self.type, title = self.title)
self.ui_str += "\n{\n"
self._indentWrite(self.attributes_string)
self._write_menus()
self._write_layouts()
self.ui_str += "\n}"
def initUI(self):
self._make_ui_string()
tmp_f = tempfile.mktemp(suffix ='huilib')
with open(tmp_f, 'w') as f:
f.write(self.ui_str)
try:
self.dialog = hou.ui.createDialog(tmp_f)
self.dialog.name = self.name
except hou.OperationFailed as e:
print("{a:#^50}\n{ui_str}\n{a:#^50}".format(error = e, ui_str = self.ui_str, a = '#'))
raise e
finally:
from os import remove
remove(tmp_f)
# Pass dialog instance to gadget objects , also set Enabled/Disable attr
for item in self._gadgets_flatten_list:
item.dialog = self.dialog
# Set init values
if item.init_value:
item.setValue(item.init_value)
# Add callbacks
if item.callbacks:
for cb in item.callbacks:
if hasattr(cb, '__call__'):
if isinstance(item._ui_value, list):
for valuecomp in item._ui_value:
self.dialog.addCallback(valuecomp, cb)
else:
self.dialog.addCallback(item._ui_value, cb)
# Set enable/disable
try:
item.setEnabled(item.enabled)
except hou.OperationFailed as e:
pass
def show(self):
self.dialog.setValue(self._ui_value, True)
def close(self):
self.dialog.setValue(self._ui_value, False)
def _print(self):
if not self.ui_str:
self._make_ui_string()
print(self.ui_str)
class HDialog(HBaseWindow):
def __init__(self, name, title):
super(HDialog, self).__init__(name, title)
self.type = 'DIALOG'