forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writenodegraphs.py
executable file
·142 lines (112 loc) · 4.58 KB
/
writenodegraphs.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
#!/usr/bin/env python
'''
Generate the "NodeGraphs.mtlx" example file programmatically.
'''
import sys, string
import MaterialX as mx
def main():
doc = mx.createDocument()
#
# Nodegraph example 1
#
ng1 = doc.addNodeGraph("NG_example1")
img1 = ng1.addNode("image", "img1", "color3")
# Because filenames look like string types, it is necessary to explicitly declare
# this parameter value as type "filename".
img1.setParameterValue("file", "layer1.tif", "filename")
img2 = ng1.addNode("image", "img2", "color3")
img2.setParameterValue("file", "layer2.tif", "filename")
img3 = ng1.addNode("image", "img3", "float")
img3.setParameterValue("file", "mask1.tif", "filename")
n0 = ng1.addNode("mix", "n0", "color3")
# To connect an input to another node, you must first add the input with the expected
# type, and then setConnectedNode() that input to the desired Node object.
infg = n0.addInput("fg", "color3")
infg.setConnectedNode(img1)
inbg = n0.addInput("bg", "color3")
inbg.setConnectedNode(img2)
inmx = n0.addInput("mix", "float")
inmx.setConnectedNode(img3)
n1 = ng1.addNode("multiply", "n1", "color3")
inp1 = n1.addInput("in1", "color3")
inp1.setConnectedNode(n0)
inp2 = n1.setInputValue("in2", 0.22)
nout = ng1.addOutput("diffuse", "color3")
nout.setConnectedNode(n1)
#
# Nodegraph example 3
#
ng3 = doc.addNodeGraph("NG_example3")
img1 = ng3.addNode("image", "img1", "color3")
img1.setParameterValue("file", "<diff_albedo>", "filename")
img2 = ng3.addNode("image", "img2", "color3")
img2.setParameterValue("file", "<dirt_albedo>", "filename")
img3 = ng3.addNode("image", "img3", "float")
img3.setParameterValue("file", "<areamask>", "filename")
img4 = ng3.addNode("image", "img4", "float")
img4.setParameterValue("file", "<noisemask>", "filename")
n5 = ng3.addNode("constant", "n5", "color3")
# For colorN, vectorN or matrix types, use the appropriate mx Type constructor.
n5.setParameterValue("value", mx.Color3(0.8,1.0,1.3))
n6 = ng3.addNode("multiply", "n6", "color3")
inp1 = n6.addInput("in1", "color3")
inp1.setConnectedNode(n5)
inp2 = n6.addInput("in2", "color3")
inp2.setConnectedNode(img1)
n7 = ng3.addNode("contrast", "n7", "color3")
inp = n7.addInput("in", "color3")
inp.setConnectedNode(img2)
n7.setInputValue("amount", 0.2)
n7.setParameterValue("pivot", 0.5)
n8 = ng3.addNode("mix", "n8", "color3")
infg = n8.addInput("fg", "color3")
infg.setConnectedNode(n7)
inbg = n8.addInput("bg", "color3")
inbg.setConnectedNode(n6)
inmx = n8.addInput("mix", "float")
inmx.setConnectedNode(img3)
t1 = ng3.addNode("texcoord", "t1", "vector2")
m1 = ng3.addNode("multiply", "m1", "vector2")
inp1 = m1.addInput("in1", "vector2")
inp1.setConnectedNode(t1)
m1.setInputValue("in2", 0.003)
# If limited floating-point precision results in output value strings like "0.00299999",
# you could instead write this as a ValueString (must add the input to the node first):
# inp2 = m1.addInput("in2", "float")
# inp2.setValueString("0.003")
n9 = ng3.addNode("noise2d", "n9", "color3")
intx = n9.addInput("texcoord", "vector2")
intx.setConnectedNode(m1)
n9.setParameterValue("amplitude", mx.Vector3(0.05,0.04,0.06))
n10 = ng3.addNode("inside", "n10", "color3")
inmask = n10.addInput("mask", "float")
inmask.setConnectedNode(img4)
inp = n10.addInput("in", "color3")
inp.setConnectedNode(n9)
n11 = ng3.addNode("add", "n11", "color3")
inp1 = n11.addInput("in1", "color3")
inp1.setConnectedNode(n10)
inp2 = n11.addInput("in2", "color3")
inp2.setConnectedNode(n8)
nout1 = ng3.addOutput("albedo", "color3")
nout1.setConnectedNode(n11)
nout2 = ng3.addOutput("areamask", "float")
nout2.setConnectedNode(img3)
# It is not necessary to validate a document before writing but it's nice
# to know for sure. And you can validate any element (and its children)
# independently, not just the whole document.
rc = ng1.validate()
if (len(rc) >= 1 and rc[0]):
print "Nodegraph %s is valid." % ng1.getName()
else:
print "Nodegraph %s is NOT valid: %s" % (ng1.getName(), str(rc[1]))
rc = ng3.validate()
if (len(rc) >= 1 and rc[0]):
print "Nodegraph %s is valid." % ng3.getName()
else:
print "Nodegraph %s is NOT valid: %s" % (ng3.getName(), str(rc[1]))
outfile = "myNodeGraphs.mtlx"
mx.writeToXmlFile(doc, outfile)
print "Wrote nodegraphs to %s" % outfile
if __name__ == '__main__':
main()