forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writelooks.py
executable file
·174 lines (152 loc) · 5.42 KB
/
writelooks.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
#!/usr/bin/env python
'''
Generate the "Looks.mtlx" example file programmatically.
'''
import sys, string
import MaterialX as mx
def main():
doc = mx.createDocument()
# Add document CMS/colorspace
doc.setColorManagementSystem("ocio")
doc.setColorSpace("lin_rec709")
# Add "SimpleSrf.mtlx" as a library, which will create an xi:include in the output file.
xi_doc = mx.createDocument()
inclfile = "SimpleSrf.mtlx"
try:
mx.readFromXmlFile(xi_doc, inclfile)
doc.importLibrary(xi_doc)
except mx.ExceptionFileMissing:
print 'Unable to find "%s" in current MATERIALX_SEARCH_PATH' % inclfile
#
# Materials
#
mplastic1 = doc.addMaterial("Mplastic1")
sref = mplastic1.addShaderRef("sr_mp1", "simple_srf")
bin1 = sref.addBindInput("diffColor", "color3")
bin1.setValue(mx.Color3(0.134, 0.130, 0.125))
bin2 = sref.addBindInput("specColor", "color3")
bin2.setValue(mx.Color3(0.114, 0.114, 0.114))
bin3 = sref.addBindInput("specRoughness", "float")
bin3.setValue(0.38)
mplastic2 = doc.addMaterial("Mplastic2")
mplastic2.setInheritsFrom(mplastic1)
sref = mplastic2.addShaderRef("sr_mp2", "simple_srf")
bin1 = sref.addBindInput("diffColor", "color3")
bin1.setValue(mx.Color3(0.17, 0.26, 0.23))
bin2 = sref.addBindInput("specRoughness", "float")
bin2.setValue(0.24)
mmetal1 = doc.addMaterial("Mmetal1")
sref = mmetal1.addShaderRef("sr_mm1", "simple_srf")
bin1 = sref.addBindInput("diffColor", "color3")
bin1.setValue(mx.Color3(0.001, 0.001, 0.001))
bin2 = sref.addBindInput("specColor", "color3")
bin2.setValue(mx.Color3(0.671, 0.676, 0.667))
bin3 = sref.addBindInput("specRoughness", "float")
bin3.setValue(0.005)
mmetal2 = doc.addMaterial("Mmetal2")
sref = mmetal2.addShaderRef("sr_mm2", "simple_srf")
bin1 = sref.addBindInput("diffColor", "color3")
bin1.setValue(mx.Color3(0.049, 0.043, 0.033))
bin2 = sref.addBindInput("specColor", "color3")
bin2.setValue(mx.Color3(0.115, 0.091, 0.064))
bin3 = sref.addBindInput("specRoughness", "float")
bin3.setValue(0.35)
#
# VariantSet
#
vset = doc.addVariantSet("varset")
vardry = vset.addVariant("dry")
vardry.setParameterValue("wetgain", 0.0)
varwet = vset.addVariant("wet")
varwet.setParameterValue("wetgain", 1.0)
vardamp = vset.addVariant("damp")
vardamp.setParameterValue("wetgain", 0.2)
#
# Collections
#
cplastic = doc.addCollection("c_plastic")
cplastic.setIncludeGeom("/a/g1,/a/g2,/a/g5")
cmetal = doc.addCollection("c_metal")
cmetal.setIncludeGeom("/a/g3,/a/g4")
clamphouse = doc.addCollection("c_lamphouse")
clamphouse.setIncludeGeom("/a/lamp1/housing*Mesh")
csetgeom = doc.addCollection("c_setgeom")
csetgeom.setIncludeGeom("/b")
#
# Nodedef and lightshader material
#
nd_disklgt = doc.addNodeDef("ND_disk_lgt_light", "lightshader", "disk_lgt")
nd_disklgt.setParameterValue("emissionmap", "", "filename")
nd_disklgt.setParameterValue("gain", 2000.0)
mheadlight = doc.addMaterial("mheadlight")
sref = mheadlight.addShaderRef("lgtsr1", "disk_lgt")
bpar = sref.addBindParam("gain", "float")
bpar.setValue(500.0)
#
# Propertyset
#
propset = doc.addPropertySet("standard")
p = propset.addProperty("displacementbound_sphere")
p.setTarget("rmanris")
p.setValue(0.05)
p = propset.addProperty("trace_maxdiffusedepth")
p.setTarget("rmanris")
p.setValue(3.0)
#
# Looks
#
lookA = doc.addLook("lookA")
ma = lookA.addMaterialAssign("ma1", mplastic1.getName())
ma.setCollection(cplastic)
ma = lookA.addMaterialAssign("ma2", mmetal1.getName())
ma.setCollection(cmetal)
ma = lookA.addMaterialAssign("ma3", mheadlight.getName())
ma.setGeom("/a/b/headlight")
va = lookA.addVisibility("v1")
va.setVisibilityType("shadow")
va.setViewerGeom("/a/b/headlight")
va.setGeom("/")
va.setVisible(0)
va = lookA.addVisibility("v2")
va.setVisibilityType("shadow")
va.setViewerGeom("/a/b/headlight")
va.setCollection(clamphouse)
psa = lookA.addPropertySetAssign("psa1")
psa.setAttribute("propertyset", propset.getName())
psa.setGeom("/")
lookB = doc.addLook("lookB")
ma = lookB.addMaterialAssign("ma4", mplastic2.getName())
ma.setCollection(cplastic)
ma = lookB.addMaterialAssign("ma5", mmetal2.getName())
ma.setCollection(cmetal)
pa = lookB.addPropertyAssign("pa1")
pa.setAttribute("property", "matte")
pa.setValue("true", "boolean")
pa.setGeom("/g/holdout,/g/holdout2")
psa = lookB.addPropertySetAssign("psa2")
psa.setAttribute("propertyset", propset.getName())
psa.setGeom("/")
vara = lookB.addVariantAssign("va1")
vara.setVariantSetString(vset.getName())
vara.setVariantString(vardamp.getName())
va = lookB.addVisibility("v3")
va.setVisibilityType("camera")
va.setCollection(csetgeom)
va.setVisible(0)
#
# Lookgroups
#
lookgrp = doc.addLookGroup("testlooks")
looks = "%s,%s" % (lookA.getName(), lookB.getName())
lookgrp.setLooks(looks)
# Validate the doc just to be sure
rc = doc.validate()
if (len(rc) >= 1 and rc[0]):
print "Mtlx document is valid."
else:
print "Mtlx document is NOT valid: %s" % str(rc[1])
outfile = "myLooks.mtlx"
mx.writeToXmlFile(doc, outfile)
print "Wrote materials and looks and such to %s" % outfile
if __name__ == '__main__':
main()