-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBVLG_VMTgen.py
227 lines (197 loc) · 8.82 KB
/
BVLG_VMTgen.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
import bpy
import re
import os
import math
FMT = ".2f"
basetexture = "$basetexture [texture]"
bumpmap = "$bumpmap [texture]"
color2 = "$color2 [RGB field]"
envmap = "$envmap [boolean]"
envmaptint = "$envmaptint [RGB field]"
envmapfresnel = "$envmapfresnel [value]"
phong = "$phong [bool]"
phongboost = "$phongboost [value]"
phongexponent = "$phongexponent [value]"
phongfresnelranges = "$phongfresnelranges [value field]"
phongtint = "$phongtint [RGB field]"
rimlight = "$rimlight [bool]"
rimlightboost = "$rimlightboost [value]"
selfillum = "$selfillum [bool]"
class VLG_VMT :
def __init__(self) :
self.basetexture = None
self.bumpmap = None
self.color2 = None
self.envmap = None
self.envmaptint = None
self.envmapfresnel = None
self.phong = None
self.phongboost = None
self.phongexponent = None
self.phongfresnelranges = None
self.phongtint = None
self.rimlight = None
self.rimlightboost = None
self.selfillum = None
def find(mat, auto = 0) :
if not(mat.use_nodes) :
print(mat.name + " SKIPPED : Not Nodes")
return None
nodes = mat.node_tree.nodes
material_output = None
for node in nodes:
if (node.type == "OUTPUT_MATERIAL") and (node.is_active_output):
material_output = node
break
node = material_output
while (node) and ((node.__class__.__name__ != "ShaderNodeGroup") or (node.node_tree.name != "VertexLitGeneric")) :
print (node)
node = node_CrawlUp(node)
if not (node) :
if (auto) :
print(mat.name + " SKIPPED : No BVLG found in NodeTree")
else:
print("ERROR : No BVLG found in NodeTree")
return None
return node
def load(self, BVLG, cdmaterials = "") :
self.basetexture = node_GetImg(node_CrawlUp(BVLG, basetexture))
if (self.basetexture) :
self.basetexture = ' $basetexture "' + cdmaterials + self.basetexture + '"'
self.color2 = node_GetField(BVLG.inputs[color2])
if (self.color2) :
self.color2 = ' $color2 ' + self.color2
self.envmap = node_GetField(BVLG.inputs[envmap])
if (self.envmap != 0) :
self.envmap = ' $envmap env_cubemap'
self.envmaptint = node_GetField(BVLG.inputs[envmaptint])
if (self.envmaptint) :
self.envmaptint = ' $envmaptint ' + self.envmaptint
self.envmapfresnel = node_GetField(BVLG.inputs[envmapfresnel])
if (self.envmapfresnel) :
self.envmapfresnel = ' $envmapfresnel ' + format(self.envmapfresnel, FMT)
self.phong = node_GetField(BVLG.inputs[phong])
if (self.phong != 0) :
self.phong = ' $phong 1'
self.phongboost = node_GetField(BVLG.inputs[phongboost])
self.phongboost = ' $phongboost ' + format(self.phongboost, FMT)
self.phongexponent = node_CrawlUp(BVLG, phongexponent)
if (self.phongexponent) and (self.phongexponent.__class__.__name__ == "ShaderNodeGroup") and (self.phongexponent.node_tree.name == "$phongexponenttexture splitter") :
self.phongexponent = ' $phongexponenttexture "' + cdmaterials + str(node_GetImg(node_CrawlUp(self.phongexponent, 0))) + '"'
self.phongtint = node_CrawlUp(BVLG, phongtint)
if (self.phongtint) and (self.phongtint.__class__.__name__ == "ShaderNodeGroup") and (self.phongtint.node_tree.name == "$phongalbedotint") :
self.phongtint = ' $phongalbedotint 1'
else :
self.phongexponent = node_GetField(BVLG.inputs[phongexponent])
self.phongexponent = ' $phongexponent ' + format(self.phongexponent, FMT)
self.phongfresnelranges = node_GetField(BVLG.inputs[phongfresnelranges])
if (self.phongfresnelranges) :
self.phongfresnelranges = ' $phongfresnelranges ' + self.phongfresnelranges
if not(self.phongtint) :
self.phongtint = node_GetField(BVLG.inputs[phongtint])
if (self.phongtint) :
self.phongtint = ' $phongtint ' + self.phongtint
self.rimlight = node_GetField(BVLG.inputs[rimlight])
if (self.rimlight != 0) :
self.rimlight = ' $rimlight 1'
self.rimlightboost = node_GetField(BVLG.inputs[rimlightboost])
self.rimlightboost = ' $rimlightboost ' + format(self.rimlightboost, FMT)
self.selfillum = node_GetField(BVLG.inputs[selfillum])
if (self.selfillum != 0) :
self.selfillum = ' $selfillum 1'
self.bumpmap = node_GetImg(node_CrawlUp(BVLG, bumpmap))
if (self.bumpmap) :
self.bumpmap = ' $bumpmap "'+ cdmaterials + self.bumpmap + '"'
elif (self.phong) :
self.bumpmap = ' $bumpmap "dev/bump_normal"'
def write(self, file) :
file.write("VertexLitGeneric\n{\n")
if self.basetexture :
file.write(self.basetexture + "\n")
if self.color2 :
file.write(self.color2 + "\n")
if self.bumpmap :
file.write(self.bumpmap + "\n")
file.write("\n")
if self.envmap :
file.write(self.envmap + "\n")
file.write(self.envmaptint + "\n")
if self.envmapfresnel :
file.write(self.envmapfresnel + "\n")
file.write("\n")
if self.phong :
file.write(self.phong + "\n")
file.write(self.phongboost + "\n")
file.write(self.phongexponent + "\n")
file.write(self.phongfresnelranges + "\n")
if self.phongtint :
file.write(self.phongtint + "\n")
if (self.rimlight) :
file.write(self.rimlight + "\n")
file.write(self.rimlightboost + "\n")
file.write("\n")
if self.selfillum :
file.write(self.selfillum + "\n")
file.write("}")
def node_GetImg(node) :
if not(node) or (node.__class__.__name__ != "ShaderNodeTexImage") :
return None
if not(node.image) :
return None
name = re.sub('\....$', '', node.image.name)
return name
def node_GetField(socket) :
if (socket.__class__.__name__ == "NodeSocketColor") :
if (socket.default_value[0] == 1 and socket.default_value[1] == 1 and socket.default_value[2] == 1) :
return None
return ('"[ ' + format(socket.default_value[0], FMT) + " " + format(socket.default_value[1], FMT) + " " + format(socket.default_value[2], FMT) + ' ]"')
elif (socket.__class__.__name__ == "NodeSocketVector") :
return ('"[ ' + format(socket.default_value[0], FMT) + " " + format(socket.default_value[1], FMT) + " " + format(socket.default_value[2], FMT) + ' ]"')
elif (socket.__class__.__name__ == "NodeSocketFloat") :
return socket.default_value
return None
def node_CrawlUp(node_start, index = 0) :
socket = node_start.inputs[index]
type = socket.type
if not(socket.is_linked) :
return None
link = socket.links[0]
socket = link.from_socket
if (type != socket.type) : #consider mismatched routes as invalid
return None
if (socket.node.__class__.__name__ == "NodeReroute") : #recursively crawl up reroutes
return (node_CrawlUp(socket.node, 0))
return socket.node
def VMTgen(mat, cdmaterials, folder = "export", mode = "name") :
if (mode == "name") :
mat = bpy.data.materials[mat]
elif (mode != "data") :
print ("ERROR invalid arguments")
return
if (cdmaterials[-1] != '/') :
cdmaterials += '/'
node = VLG_VMT.find(mat, mode == "data")
if (node) :
BVLG = node
print(node)
VMT = VLG_VMT()
VMT.load(BVLG, cdmaterials)
path = os.path.join(bpy.path.abspath("//"), folder)
if not(os.path.exists(path)) :
os.makedirs(path)
path += "/" + mat.name + ".vmt"
print(path)
print(mat.name + ".vmt")
file = open(path, "a+")
file.truncate(0)
VMT.write(file)
file.seek(0)
print("\ngenerated output:")
print(file.read())
file.close
def AutoVMTgen(cdmaterials, folder = "export") :
for mat in bpy.data.materials :
VMTgen(mat, cdmaterials, folder, "data")
if __name__ == "__main__" :
#print("\n\nnew run:\n")
AutoVMTgen("cdmaterials")