forked from Psyop/CryptomatteArnold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uigen.py
executable file
·1220 lines (995 loc) · 37.5 KB
/
uigen.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python
import os
import sys
import uuid
from math import pow
from string import maketrans
def enum(*sequential, **named):
enums = dict(zip(sequential, range(len(sequential))), **named)
reverse = dict((value, key) for key, value in enums.iteritems())
enums['reverse_mapping'] = reverse
return type('Enum', (), enums)
class UiElement:
name = ''
ident = ''
description = None
parent = None
children = None
class Group(UiElement):
collapse = True
def __init__(self, name, collapse=True, description=None, ident=None):
self.name = name
self.collapse = collapse
self.description = description
if ident==None:
ident = name.replace(' ', '')
ident = ident.replace('_', '')
ident = ident.lower()
temptrans = maketrans('','')
self.ident = ident.translate(temptrans,'aeiouy')
else:
self.ident = ident
self.unique_name = None
def __str__(self):
return 'Group %s' % self.name
class Tab(Group):
def __init__(self, name, collapse=True, description=None, ident=None):
self.name = name
self.collapse = collapse
self.description = description
if ident==None:
ident = name.replace(' ', '')
ident = ident.replace('_', '')
ident = ident.lower()
temptrans = maketrans('','')
self.ident = ident.translate(temptrans,'aeiouy')
else:
self.ident = ident
self.unique_name = None
def __str__(self):
return 'Tab %s' % self.name
class Parameter(UiElement):
ptype = None
label = None
mn = None
mx = None
smn = None
smx = None
connectible = True
enum_names = None
default = None
fig = None
figc = None
short_description = ''
presets = None
mayane = False
ui = ''
def __init__(self, name, ptype, default, label=None, description=None, mn=None, mx=None, smn=None, smx=None, connectible=True, enum_names=None, fig=None, figc=None, presets={}, mayane=False, ui=''):
self.name = name
self.ptype = ptype
self.default = default
self.description = description
self.presets = presets
self.mayane = mayane
self.ui = ui
if description is not None:
if len(description) > 150:
# build the short description, < 200 chars
sentences = description.split('.')
sdlen = 0
for s in sentences:
self.short_description += s + "."
sdlen += len(s)
if sdlen > 150:
break
else:
self.short_description = description
if not label:
label = name
self.label = label
self.mn = mn
self.mx = mx
self.smn = smn
self.smx = smx
self.fig = fig
self.figc = figc
if ptype not in ('bool', 'int', 'float', 'rgb', 'vector', 'string', 'enum', 'matrix'):
raise ValueError('parameter %s has unrecognized ptype %r' % (name, ptype))
if ptype == 'enum':
self.enum_names = enum_names
if ptype == 'bool' or ptype == 'string' or ptype == 'int' or ptype == 'enum':
self.connectible = False
else:
self.connectible = connectible
# do a quick sanity check on the ptype and default
if ptype == 'bool' and type(default) is not bool:
raise ValueError('parameter %s was typed as %s but default had type %r' % (name, ptype, type(default)))
elif ptype in ('rgb', 'vector') and type(default) is not tuple:
raise ValueError('parameter %s was typed as %s but default had type %r' % (name, ptype, type(default)))
elif ptype in ('string', 'enum') and type(default) is not str:
raise ValueError('parameter %s was typed as %s but default had type %r' % (name, ptype, type(default)))
self.default = default
def __str__(self):
return 'Parameter %s %s' % (self.name, self.ptype)
class AOV(Parameter):
def __init__(self, name, ptype, label=None):
self.name = name
self.ptype = ptype
self.label = label
self.connectible = False
def __str__(self):
return 'AOV %s %s' % (self.name, self.ptype)
class ShaderDef:
name = None
intro = None
description = None
output = None
aov_shader = False
c4d_classification = None
c4d_menu = None
c4d_command_id = None
maya_name = None
maya_classification = None
maya_id = None
maya_swatch = False
maya_bump = False
maya_matte = False
soft_name = None
soft_classification = None
help_url = None
houdini_icon = None
houdini_category = None
soft_version = 1
hierarchy_depth = 0
root = None
current_parent = None
parameters = []
aovs = []
groups = []
tabs = []
def __init__(self):
self.root = Group('__ROOT__', False)
self.current_parent = self.root
def setMayaClassification(self, mc):
self.maya_classification = mc
def setDescription(self, d):
self.description = d
def setOutput(self, o):
self.output = o
def beginGroup(self, name, collapse=True, description='', ident=None):
g = None
if self.current_parent is self.root:
g = Tab(name, collapse, description,ident)
else:
g = Group(name, collapse, description,ident)
if not self.current_parent.children:
self.current_parent.children = [g]
else:
self.current_parent.children.append(g)
g.parent = self.current_parent
self.current_parent = g
def endGroup(self):
self.current_parent = self.current_parent.parent
def parameter(self, name, ptype, default, label=None, description=None, mn=None, mx=None, smn=None, smx=None, connectible=True, enum_names=None, fig=None, figc = None, presets=None, mayane=False, ui=''):
p = Parameter(name, ptype, default, label, description, mn, mx, smn, smx, connectible, enum_names, fig, figc, presets, mayane, ui)
if not self.current_parent.children:
self.current_parent.children = [p]
else:
self.current_parent.children.append(p)
p.parent = self.current_parent
self.parameters.append(p)
def aov(self, name, ptype, label=None, description=None):
p = AOV(name, ptype, label)
if not self.current_parent.children:
self.current_parent.children = [p]
else:
self.current_parent.children.append(p)
p.parent = self.current_parent
self.aovs.append(p)
def __str__(self):
return 'ShaderDef %s' % self.name
def shader(self, d):
self.name = d['name']
self.intro = d['intro']
self.description = d['description']
self.output = d['output']
self.aov_shader = d.get('aov_shader')
self.c4d_classification = d.get('c4d_classification')
self.c4d_menu = d.get('c4d_menu')
self.c4d_command_id = d.get('c4d_command_id')
self.maya_name = d['maya_name']
self.maya_classification = d['maya_classification']
self.maya_id = d['maya_id']
self.maya_swatch = d['maya_swatch']
self.maya_bump = d['maya_bump']
self.maya_matte = d['maya_matte']
self.soft_name = d['soft_name']
self.soft_classification = d['soft_classification']
self.soft_version = d['soft_version']
if 'help_url' in d.keys():
self.help_url = d['help_url']
else:
self.help_url = 'https://bitbucket.org/anderslanglands/alshaders/wiki/Home'
if 'houdini_icon' in d.keys():
self.houdini_icon = d['houdini_icon']
if 'houdini_category' in d.keys():
self.houdini_category = d['houdini_category']
#all groups need unique ident in houdini
def uniqueGroupIdents(self):
grpidents = []
for grp in self.groups:
oldident=grp.ident
if grp.ident in grpidents:
grp.ident = '%s%d' % (grp.ident, grpidents.count(oldident))
grpidents.append(oldident)
class group:
name = ''
collapse = True
description = ''
ui = None
ident = None
def __init__(self, ui, name, collapse=True, description=None, ident=None):
self.ui = ui
self.name = name
self.collapse = collapse
self.description = description
self.ident = ident
def __enter__(self):
self.ui.beginGroup(self.name, self.collapse, self.description, self.ident)
def __exit__(self, type, value, traceback):
self.ui.endGroup()
def DebugPrintElement(el, d):
indent = ''
for i in range(d):
indent += '\t'
print '%s %s' % (indent, el)
if isinstance(el, Group) and el.children:
print '%s has %d children' % (el, len(el.children))
for e in el.children:
DebugPrintElement(e, d+1)
d -= 1
def DebugPrintShaderDef(sd):
print '%s' % sd
DebugPrintElement(sd.root, 0)
def writei(f, s, d=0):
indent = ''
for i in range(d):
indent += '\t'
f.write('%s%s\n' %(indent, s))
def WalkAETemplate(el, f, d):
if isinstance(el, Group):
writei(f, 'self.beginLayout("%s", collapse=%r)' % (el.name, el.collapse), d)
if el.children:
for e in el.children:
WalkAETemplate(e, f, d)
writei(f, 'self.endLayout() # END %s' % el.name, d)
elif isinstance(el, AOV):
writei(f, 'self.addControl("%s", label="%s", annotation="%s")' % (el.name, el.label, el.short_description), d)
elif isinstance(el, Parameter):
if el.ptype == 'float':
writei(f, 'self.addCustomFlt("%s")' % el.name, d)
elif el.ptype == 'rgb':
writei(f, 'self.addCustomRgb("%s")' % el.name, d)
else:
writei(f, 'self.addControl("%s", label="%s", annotation="%s")' % (el.name, el.label, el.short_description), d)
def WalkAETemplateParams(el, f, d):
if isinstance(el, Group):
if el.children:
for e in el.children:
WalkAETemplateParams(e, f, d)
elif isinstance(el, Parameter):
writei(f, 'self.params["%s"] = Param("%s", "%s", "%s", "%s", presets=%s)' % (el.name, el.name, el.label, el.short_description, el.ptype, el.presets), d)
def WriteAETemplate(sd, fn):
f = open(fn, 'w')
writei(f, 'import pymel.core as pm', 0)
writei(f, 'from alShaders import *\n', 0)
writei(f, 'class AE%sTemplate(alShadersTemplate):' % sd.maya_name, 0)
writei(f, 'controls = {}', 1)
writei(f, 'params = {}', 1)
writei(f, 'def setup(self):', 1)
writei(f, 'self.params.clear()', 2)
for e in sd.root.children:
WalkAETemplateParams(e, f, 2)
writei(f, '')
if sd.maya_swatch:
writei(f, 'self.addSwatch()', 2)
writei(f, 'self.beginScrollLayout()', 2) # begin main scrollLayout
writei(f, '')
for e in sd.root.children:
WalkAETemplate(e, f, 2)
if sd.maya_bump:
writei(f, 'self.addBumpLayout()', 2)
writei(f, '')
writei(f, 'pm.mel.AEdependNodeTemplate(self.nodeName)', 2)
writei(f, 'self.addExtraControls()', 2)
writei(f, '')
writei(f, 'self.endScrollLayout()', 2) #end main scrollLayout
f.close()
def toMayaType(ptype):
if ptype == 'rgb' or ptype == 'vector':
return 'float3'
else:
return ptype
def WalkAEXMLAttributes(el, f, d):
if isinstance(el, Group):
if el.children:
for e in el.children:
WalkAEXMLAttributes(e, f, d)
elif isinstance(el, Parameter):
writei(f, '<attribute name="%s" type="maya.%s">' % (el.name, toMayaType(el.ptype)), 2)
writei(f, '<label>%s</label>' % el.label, 3)
writei(f, '<description>%s</description>' % el.short_description, 3)
writei(f, '</attribute>', 2)
def WalkNEXMLAttributes(el, f, d):
if isinstance(el, Group):
if el.children:
for e in el.children:
WalkNEXMLAttributes(e, f, d)
elif isinstance(el, Parameter) and el.mayane:
writei(f, '<attribute name="%s" type="maya.%s">' % (el.name, toMayaType(el.ptype)), 2)
writei(f, '<label>%s</label>' % el.label, 3)
writei(f, '</attribute>', 2)
def WalkNEXMLView(el, f, d):
if isinstance(el, Group):
if el.children:
for e in el.children:
WalkNEXMLView(e, f, d)
elif isinstance(el, Parameter) and el.mayane:
writei(f, '<property name="%s" />' % el.name, 2)
def WalkAEXMLGroups(el, f, d):
if isinstance(el, Group):
writei(f, '<group name="%s">' % ''.join(el.name.split()), d)
writei(f, '<label>%s</label>' % el.name, d+1)
if el.children:
for e in el.children:
WalkAEXMLGroups(e, f, d+1)
writei(f, '</group>', d)
elif isinstance(el, Parameter):
writei(f, '<property name="%s"/>' % el.name, d)
def WriteNEOutputAttr(sd, f, d):
if sd.output == 'rgb':
writei(f, '<attribute name="outColor" type="maya.float3">', d)
writei(f, '<label>Out Color</label>', d)
writei(f, '</attribute>')
elif sd.output == 'rgba':
writei(f, '<attribute name="outColor" type="maya.float3">', d)
writei(f, '<label>Out Color</label>', d)
writei(f, '</attribute>')
writei(f, '<attribute name="outAlpha" type="maya.float">', d)
writei(f, '<label>Out Alpha</label>', d)
writei(f, '</attribute>')
elif sd.output == 'vector':
writei(f, '<attribute name="outValue" type="maya.float3">', d)
writei(f, '<label>Out Value</label>', d)
writei(f, '</attribute>')
elif sd.output == 'float':
writei(f, '<attribute name="outValue" type="maya.float">', d)
writei(f, '<label>Out Value</label>', d)
writei(f, '</attribute>')
def WriteNEOutputProp(sd, f, d):
if sd.output == 'rgb':
writei(f, '<property name="outColor" />', d)
elif sd.output == 'rgba':
writei(f, '<property name="outColor" />', d)
writei(f, '<property name="outAlpha" />', d)
elif sd.output == 'vector':
writei(f, '<property name="outValue" />', d)
elif sd.output == 'float':
writei(f, '<property name="outValue" />', d)
def WriteAEXML(sd, fn):
f = open(fn, 'w')
writei(f, '<?xml version="1.0" encoding="UTF-8"?>', 0)
writei(f, '<templates>', 1)
writei(f, '<template name="AE%s">' % sd.maya_name, 1)
writei(f, '<label>%s</label>' % sd.maya_name, 2)
writei(f, '<description>%s</description>' % sd.description, 2)
for e in sd.root.children:
WalkAEXMLAttributes(e, f, 2)
writei(f, '</template>', 1)
writei(f, '<view name="Lookdev" template="AE%s">' % sd.maya_name, 2)
for e in sd.root.children:
WalkAEXMLGroups(e, f, 3)
writei(f, '</view>', 2)
writei(f, '</templates>', 0)
def WriteNEXML(sd, fn):
f = open(fn, 'w')
writei(f, '<?xml version="1.0" encoding="UTF-8"?>', 0)
writei(f, '<templates>', 1)
writei(f, '<template name="NE%s">' % sd.maya_name, 1)
WriteNEOutputAttr(sd, f, 2)
for e in sd.root.children:
WalkNEXMLAttributes(e, f, 2)
writei(f, '</template>', 1)
writei(f, '<view name="NEDefault" template="NE%s">' % sd.maya_name, 2)
WriteNEOutputProp(sd, f, 2)
for e in sd.root.children:
WalkNEXMLView(e, f, 2)
writei(f, '</view>', 2)
writei(f, '</templates>', 0)
#########
# C4DtoA
#########
# Parameter ids are generated from the name.
def GenerateC4DtoAId(name, parameter_name):
unique_name = "%s.%s" % (name, parameter_name)
pid = 5381
for c in unique_name:
pid = ((pid << 5) + pid) + ord(c) # hash*33 + c
#pid = pid*33 + ord(c)
#print "%d %c" % (pid, c)
# convert to unsigned int
pid = pid & 0xffffffff
if pid > 2147483647: pid = 2*2147483647 - pid + 2
return pid
# Writes out group ids to the header file.
def WalkC4DtoAHeaderGroups(el, f, name):
if isinstance(el, Group):
group_name = el.unique_name if el.unique_name else el.name
writei(f, 'C4DAI_%s_%s_GRP,' % (name.upper(), group_name.upper().replace(' ', '_')), 1)
if el.children:
for e in el.children:
WalkC4DtoAHeaderGroups(e, f, name)
# Writes out parameter ids to the header file.
def WalkC4DtoAHeaderParameters(el, f, name):
if isinstance(el, Group):
if el.children:
for e in el.children:
WalkC4DtoAHeaderParameters(e, f, name)
# skip aovs
#if isinstance(el, AOV):
# pass
if isinstance(el, Parameter):
pid = GenerateC4DtoAId(name, el.name)
writei(f, 'C4DAIP_%s_%s = %d,' % (name.upper(), el.name.upper().replace(' ', '_'), pid), 1)
# Writes .h header file.
def WriteC4DtoAHeaderFile(sd, name, build_dir):
path = os.path.join(build_dir, "C4DtoA", "res", "description", "ainode_%s.h" % name)
if os.path.exists(path):
os.remove(path)
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
f = open(path, 'w')
writei(f, '#ifndef _ainode_%s_h_' % name, 0)
writei(f, '#define _ainode_%s_h_' % name, 0)
writei(f, '', 0)
writei(f, 'enum', 0)
writei(f, '{', 0)
writei(f, 'C4DAI_%s_MAIN_GRP = 2001,' % (name.upper()), 1)
# groups
for e in sd.root.children:
WalkC4DtoAHeaderGroups(e, f, name)
# matte
if sd.maya_matte:
writei(f, 'C4DAI_%s_MATTE_GRP,' % name.upper(), 1)
writei(f, '', 0)
# parameters
for e in sd.root.children:
WalkC4DtoAHeaderParameters(e, f, name)
# matte
if sd.maya_matte:
writei(f, 'C4DAIP_%s_AIENABLEMATTE = %d,' % (name.upper(), GenerateC4DtoAId(name, 'aiEnableMatte')), 1)
writei(f, 'C4DAIP_%s_AIMATTECOLOR = %d,' % (name.upper(), GenerateC4DtoAId(name, 'aiMatteColor')), 1)
writei(f, 'C4DAIP_%s_AIMATTECOLORA = %d,' % (name.upper(), GenerateC4DtoAId(name, 'aiMatteColorA')), 1)
writei(f, '};', 0)
writei(f, '', 0)
writei(f, '#endif', 0)
writei(f, '', 0)
# Writes out layout to the resource file.
def WalkC4DtoARes(el, f, name, d):
if isinstance(el, Group):
group_name = el.unique_name if el.unique_name else el.name
writei(f, 'GROUP C4DAI_%s_%s_GRP' % (name.upper(), group_name.upper().replace(' ', '_')), d)
writei(f, '{', d)
if not el.collapse:
writei(f, 'DEFAULT 1;', d+1)
writei(f, '', 0)
if el.children:
for e in el.children:
WalkC4DtoARes(e, f, name, d+1)
writei(f, '}', d)
writei(f, '', 0)
# skip aovs
#elif isinstance(el, AOV):
# pass
elif isinstance(el, Parameter):
writei(f, 'AIPARAM C4DAIP_%s_%s {}' % (name.upper(), el.name.upper().replace(' ', '_')), d)
# Writes .res resource file.
def WriteC4DtoAResFile(sd, name, build_dir):
path = os.path.join(build_dir, "C4DtoA", "res", "description", "ainode_%s.res" % name)
if os.path.exists(path):
os.remove(path)
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
f = open(path, 'w')
writei(f, 'CONTAINER AINODE_%s' % name.upper(), 0)
writei(f, '{', 0)
writei(f, 'NAME ainode_%s;' % name, 1)
writei(f, '', 0)
writei(f, 'INCLUDE GVbase;', 1)
writei(f, '', 0)
writei(f, 'GROUP C4DAI_%s_MAIN_GRP' % name.upper(), 1)
writei(f, '{', 1)
writei(f, 'DEFAULT 1;', 2)
writei(f, '', 0)
# matte
if sd.maya_matte:
writei(f, 'GROUP C4DAI_%s_MATTE_GRP' % name.upper(), 2)
writei(f, '{', 2)
writei(f, 'AIPARAM C4DAIP_%s_AIENABLEMATTE {}' % name.upper(), 3)
writei(f, 'AIPARAM C4DAIP_%s_AIMATTECOLOR {}' % name.upper(), 3)
writei(f, 'AIPARAM C4DAIP_%s_AIMATTECOLORA {}' % name.upper(), 3)
writei(f, '}', 2)
writei(f, '', 0)
# groups and parameters
for e in sd.root.children:
WalkC4DtoARes(e, f, name, 2)
writei(f, '}', 1)
writei(f, '}', 0)
writei(f, '')
# Writes out group labels to the string file.
def WalkC4DtoAStringGroups(el, f, name):
if isinstance(el, Group):
group_name = el.unique_name if el.unique_name else el.name
writei(f, 'C4DAI_%s_%s_GRP "%s";' % (name.upper(), group_name.upper().replace(' ', '_'), el.name), 1)
if el.children:
for e in el.children:
WalkC4DtoAStringGroups(e, f, name)
# Writes out parameter labels to the string file.
def WalkC4DtoAStringParameters(el, f, name):
if isinstance(el, Group):
if el.children:
for e in el.children:
WalkC4DtoAStringParameters(e, f, name)
if isinstance(el, Parameter) and el.label:
writei(f, 'C4DAIP_%s_%s "%s";' % (name.upper(), el.name.upper(), el.label), 1)
# Writes .str string file.
def WriteC4DtoAStringFile(sd, name, build_dir):
path = os.path.join(build_dir, "C4DtoA", "res", "strings_us", "description", "ainode_%s.str" % name)
if os.path.exists(path):
os.remove(path)
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
f = open(path, 'w')
writei(f, 'STRINGTABLE ainode_%s' % name, 0)
writei(f, '{', 0)
writei(f, 'ainode_%s "Arnold %s node";' % (name, name), 1)
writei(f, '', 0)
writei(f, 'C4DAI_%s_MAIN_GRP "Main";' % (name.upper()), 1)
# groups
for e in sd.root.children:
WalkC4DtoAStringGroups(e, f, name)
# matte
if sd.maya_matte:
writei(f, 'C4DAI_%s_MATTE_GRP "Matte";' % name.upper(), 1)
writei(f, '', 0)
# parameters
for e in sd.root.children:
WalkC4DtoAStringParameters(e, f, name)
# matte
if sd.maya_matte:
writei(f, 'C4DAIP_%s_AIENABLEMATTE "Enable matte";' % name.upper(), 1)
writei(f, 'C4DAIP_%s_AIMATTECOLOR "Matte color";' % name.upper(), 1)
writei(f, 'C4DAIP_%s_AIMATTECOLORA "Matte opacity";' % name.upper(), 1)
writei(f, '}', 0)
writei(f, '', 0)
# Writes resource files for C4DtoA.
# To describe the UI we need three resource files:
# - .h header file which contains parameter ids
# - .res resource file which contains widget layout
# - .str string file which contains labels
def WriteC4DtoAResourceFiles(sd, name, build_dir):
# NOTE group list is build in WriteMTD
# create unique group names
names = set()
groups = sd.tabs[:]
groups.extend(sd.groups)
for group in groups:
if group.name not in names:
names.add(group.name)
continue
i = 1
group.unique_name = group.name
while group.unique_name in names:
group.unique_name = group.name + " %d" % i
i += 1
names.add(group.unique_name)
# header file
WriteC4DtoAHeaderFile(sd, name, build_dir)
# resource file
WriteC4DtoAResFile(sd, name, build_dir)
# string file
WriteC4DtoAStringFile(sd, name, build_dir)
#make list of groups and tabs
def buildGroupList(sd, el):
if isinstance(el, Group):
if not isinstance(el, Tab):
sd.groups.append(el)
if isinstance(el, Tab):
sd.tabs.append(el)
if el.children:
for e in el.children:
buildGroupList(sd, e)
#find the total number of child groups and parameters of a Tab
def findTotalChildGroups(tab):
if tab.children:
totalchildren=len(tab.children)
for t in tab.children:
totalchildren+=findTotalChildGroups(t)
return totalchildren
else:
return 0
#print ordered list of all parameters and groups
def writeParameterOrder(f, grp, first=1, orderc=1):
firstfolder=first
ordercount=orderc
for gr in grp.children:
if isinstance(gr, Parameter):
f.write('%s ' % gr.name)
if isinstance(gr, Group) and not isinstance(gr, Tab):
f.write('%s ' % gr.ident)
writeParameterOrder(f,gr,firstfolder,ordercount)
if isinstance(gr, Tab):
if firstfolder == 1:
f.write('folder1 ')
firstfolder=0
ordercount+=1
f.write('"\n\thoudini.order%d STRING "' % ordercount)
writeParameterOrder(f,gr, firstfolder,ordercount)
class HoudiniEntry:
name = ''
etype = ''
def __init__(self, nm, et):
self.name = nm
self.etype = et
class HoudiniFolder:
name = ''
group_list = []
def WalkHoudiniHeader(grp, group_list):
for el in grp.children:
if isinstance(el, Group):
group_list.append(HoudiniEntry(el.name, 'group'))
WalkHoudiniHeader(el, group_list)
elif isinstance(el, Parameter):
group_list.append(HoudiniEntry(el.name, 'param'))
def WriteHoudiniHeader(sd, f):
root_order_list = []
folder_ROOT_list = []
for el in sd.root.children:
if isinstance(el, Group):
new_folder = HoudiniFolder()
new_folder.name = el.name
group_list = []
WalkHoudiniHeader(el, group_list)
new_folder.group_list = group_list
folder_ROOT_list.append(new_folder)
elif isinstance(el, Parameter):
root_order_list.append(HoudiniEntry(el.name, 'param'))
# tell houdini how many groups we have and how big they are. in advance. because houdini can be dumb too sometimes
if len(folder_ROOT_list):
folder_ROOT_list_STR = 'houdini.parm.folder.ROOT STRING "'
for folder in folder_ROOT_list:
folder_ROOT_list_STR += '%s;%d;' % (folder.name, len(folder.group_list))
folder_ROOT_list_STR += '"'
writei(f, folder_ROOT_list_STR, 1)
# now tell houdini about all the headings we have...
heading_list = []
heading_idx = 0
for folder in folder_ROOT_list:
for el in folder.group_list:
if el.etype == 'group':
header_str = 'houdini.parm.heading.h%d STRING "%s"' % (heading_idx, el.name)
heading_idx += 1
writei(f, header_str, 1)
# write main houdini ordering
order = 'houdini.order STRING "'
for entry in root_order_list:
order += entry.name
order += ' '
if len(folder_ROOT_list):
order += ' ROOT"'
else:
order += '"'
writei(f, order, 1)
# now tell houdini what's actually in the damn groups
order_idx = 2
heading_idx = 0
for folder in folder_ROOT_list:
order_str = 'houdini.order%d STRING "' % order_idx
for el in folder.group_list:
if el.etype == 'group':
order_str = '%s h%d' % (order_str, heading_idx)
heading_idx += 1
else:
order_str = '%s %s' % (order_str, el.name)
order_str += '"'
writei(f, order_str, 1)
order_idx += 1
def WriteMDTHeader(sd, f):
writei(f, '[node %s]' % sd.name, 0)
writei(f, 'desc STRING "%s"' % sd.description, 1)
if sd.aov_shader: WriteMTDParam(f, 'aov_shader', 'bool', sd.aov_shader, 1)
if sd.c4d_classification: writei(f, 'c4d.classification STRING "%s"' % sd.c4d_classification, 1)
if sd.c4d_menu: writei(f, 'c4d.menu STRING "%s"' % sd.c4d_menu, 1)
if sd.c4d_command_id: writei(f, 'c4d.command_id INT %s' % sd.c4d_command_id, 1)
writei(f, 'maya.name STRING "%s"' % sd.maya_name, 1)
writei(f, 'maya.classification STRING "%s"' % sd.maya_classification, 1)
writei(f, 'maya.id INT %s' % sd.maya_id, 1)
#writei(f, 'houdini.label STRING "%s"' % sd.name, 1)
if sd.houdini_icon is not None:
writei(f, 'houdini.icon STRING "%s"' % sd.houdini_icon, 1)
hcat = ("alShaders" if sd.houdini_category is None else sd.houdini_category)
writei(f, 'houdini.category STRING "%s"' % hcat ,1)
writei(f, 'houdini.help_url STRING "%s"' % sd.help_url ,1)
WriteHoudiniHeader(sd, f)
def WriteMTDParam(f, name, ptype, value, d):
if value == None:
return
if ptype == 'bool':
if value:
bval = "TRUE"
else:
bval = "FALSE"
writei(f, '%s BOOL %s' % (name, bval), d)
elif ptype == 'int':
writei(f, '%s INT %d' % (name, value), d)
elif ptype == 'float':
writei(f, '%s FLOAT %r' % (name, value), d)
elif ptype == 'string':
writei(f, '%s STRING "%s"' % (name, value), d)
def WriteMTD(sd, fn):
#build tab and group lists
for e in sd.root.children:
buildGroupList(sd, e)
#make all groupident unique
sd.uniqueGroupIdents()
f = open(fn, 'w')
WriteMDTHeader(sd, f)
writei(f, '')
for p in sd.parameters:
writei(f, '[attr %s]' % p.name, 1)
writei(f, 'houdini.label STRING "%s"' % p.label, 2)
WriteMTDParam(f, "min", "float", p.mn, 2)
WriteMTDParam(f, "max", "float", p.mx, 2)
WriteMTDParam(f, "softmin", "float", p.smn, 2)
WriteMTDParam(f, "softmax", "float", p.smx, 2)
WriteMTDParam(f, "desc", "string", p.description, 2)
WriteMTDParam(f, "linkable", "bool", p.connectible, 2)
if p.ui == 'file':
WriteMTDParam(f, "c4d.gui_type", "int", 3, 2)
WriteMTDParam(f, "houdini.type", "string", "file:image", 2)
for a in sd.aovs:
writei(f, '[attr %s]' % a.name, 1)
writei(f, 'houdini.label STRING "%s"' % a.label, 2)
if (a.ptype == 'rgb'):
writei(f, 'aov.type INT 0x05', 2)
elif (a.ptype == 'rgba'):
writei(f, 'aov.type INT 0x06', 2)
writei(f, 'aov.enable_composition BOOL TRUE', 2)
writei(f, 'default STRING "%s"' % a.name[4:], 2)
f.close()
def WalkArgs(el, f, d):
if isinstance(el, Group):
writei(f, '<page name="%s">' % (el.name), d)
if el.children:
for e in el.children:
WalkArgs(e, f, d+1)
writei(f, '</page>', d)
elif isinstance(el, AOV):
writei(f, '<param name="%s" label="%s"/>' % (el.name, el.label), d)
elif isinstance(el, Parameter):
if el.ptype == 'bool':
writei(f, '<param name="%s" label="%s" widget="checkBox"/>' % (el.name, el.label), d)
elif el.ptype == 'int':
writei(f, '<param name="%s" label="%s" int="True"/>' % (el.name, el.label), d)
elif el.ptype == 'rgb':
writei(f, '<param name="%s" label="%s" widget="color"/>' % (el.name, el.label), d)
elif el.ptype == 'enum':
writei(f, '<param name="%s" label="%s" widget="popup">' % (el.name, el.label), d)
writei(f, '<hintlist name="options">', d+1)
for en in el.enum_names:
writei(f, '<string value="%s"/>' % en, d+2)
writei(f, '</hintlist>', d+1)
writei(f, '</param>', d)
else:
writei(f, '<param name="%s" label="%s"/>' % (el.name, el.label), d)
def WriteArgs(sd, fn):
f = open(fn, 'w')
writei(f, '<args format="1.0">', 0)
for e in sd.root.children:
WalkArgs(e, f, 0)
writei(f, '</args>', 0)
def getSPDLTypeName(t):
if t == 'bool':
return 'boolean'
elif t == 'int':
return 'integer'
elif t == 'float':
return 'scalar'
elif t == 'rgb':
return 'color'
elif t == 'enum':
return 'string'
else:
return t
def WriteSPDLParameter(f, p):
writei(f, 'Parameter "%s" input' % p.name, 1)
writei(f, '{', 1)
writei(f, 'GUID = "{%s}";' % uuid.uuid4(), 2)
if isinstance(p, AOV):
writei(f, 'Type = string;', 2)
si_aov_name = p.name.replace('aov_', 'arnold_').title()
writei(f, 'Value = "%s";' % si_aov_name, 2)
else: