-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
298 lines (249 loc) · 10.1 KB
/
service.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
import json
import numpy
import math
import os
import argparse
import sys
import random
# add dependencies for auto-py-to-exe
# https://github.com/pyinstaller/pyinstaller/issues/4363#issuecomment-522350024
# import numpy.random.common
# import numpy.random.bounded_integers
# import numpy.random.entropy
from PIL import Image
from remote import fetch
from pathlib import Path
from urlpath import URL
from util import colorprint, prepareOutFilename, test_scene, joinOutputPath
from remote import CachedFile
import igxc
import scene
import visitor
default_url = 'default/igcx/test-url'
default_file = 'default/igcx/test-file'
default_out = "test"
aoConfig = {"resolution": 1024}
def generateMap(vertices,
normals,
texcoord,
size=(aoConfig["resolution"], aoConfig["resolution"])):
import ig_rendering_support
w, h = size
buff = numpy.zeros((w, h, 4), dtype=numpy.uint8)
ig_rendering_support.bakeAO(buff, vertices, normals, texcoord)
blurred = ig_rendering_support.alphaBlur(buff, w, h)
return Image.frombuffer('RGBA', (w, h), blurred, 'raw', 'RGBA', 0, 1)
def start():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--url',
help='url to igxc resource',
nargs='?',
type=str,
const=default_url)
group.add_argument('--file',
help='path to igxc resource',
nargs='?',
type=str,
const=default_file)
group.add_argument('--test',
help='use default test scene',
action='store_true')
parser.add_argument('--out',
'-o',
help='basename of the generated output',
type=str,
default=default_out)
parser.add_argument('--debug',
help='show debug viewer',
action='store_true')
parser.add_argument('--face-normals',
help='use computed face normals',
action='store_true')
args = parser.parse_args()
dictArgs = vars(args)
# print(args)
startWithDirectArgs(dictArgs)
def modifyIgxc(igxcJson, outFileName, mapping):
igxcJson["ObjectAmbientOcclusionMaps"] = {'.': outFileName}
for tObject in igxcJson['Objects']:
nodeName = tObject["Path"]
if nodeName in mapping:
tObject["AOTransform"] = mapping[nodeName]
def startWithDirectArgs(args: dict):
root = None
igxcFile = None
igxcContent = None
urlArgument = None
basePath = None
result = None
outFileHashBase = ""
outFileNameBase = "AO_result"
resolutionValue = aoConfig["resolution"]
if "resolution" in args and args["resolution"] is not None:
resolutionValue = int(args["resolution"])
if "url" in args and args["url"] is not None:
print('fetch url', args["url"])
urlArgument = URL(args["url"])
igxcFile = fetch(urlArgument, '.igcx')
colorprint("source: " + str(urlArgument) + '.igcx', 36)
elif "file" in args and args["file"] is not None:
igxcFile = Path(args["file"])
colorprint("source: " + args["file"], 36)
elif "igxcContent" in args and args["igxcContent"] is not None:
igxcContent = args["igxcContent"]
colorprint("source: bakeDirect POST parameters", 36)
elif "test" in args and args["test"]:
outFileNameBase = prepareOutFilename(str(random.randint(1, 1e9)),
resolutionValue)
root = test_scene()
colorprint("source: test scene", 36)
if igxcFile is not None:
basePath = igxcFile.parent
if "basePath" in args and args["basePath"] is not None:
# print(args["basePath"])
basePath = CachedFile(args["basePath"])
print("basePath:", basePath)
debug = "debug" in args and args["debug"]
if igxcFile is not None:
with igxcFile.open('r') as resource:
igxcContent = json.load(resource)
# if igxcContent is None:
# return None
if igxcContent is None:
print("No content in igxc")
# check if configuration is already done
if "Objects" in igxcContent and igxcContent["Objects"] is not None:
outFileHashBase = outFileHashBase + json.dumps(igxcContent["Objects"],
sort_keys=True)
if "Hashes" in igxcContent and igxcContent["Hashes"] is not None:
outFileHashBase = outFileHashBase + json.dumps(igxcContent["Hashes"],
sort_keys=True)
if outFileHashBase == "" and urlArgument is not None:
outFileHashBase = urlArgument
outFileNameBase = prepareOutFilename(outFileHashBase, resolutionValue)
hasImage = os.path.isfile(joinOutputPath(outFileNameBase, 'png'))
hasMapping = os.path.isfile(joinOutputPath(outFileNameBase, 'json'))
if hasImage and hasMapping and not debug:
colorprint("Taking from cache ({})".format(outFileNameBase), 32)
mappingResult = None
with open(joinOutputPath(outFileNameBase, 'json'),
'r') as mappingInFile:
mappingResult = json.load(mappingInFile)
modifyIgxc(igxcContent, outFileNameBase + '.png', mappingResult)
result = {
"urlAoMapImage": outFileNameBase + '.png',
"urlAoMappingJson": outFileNameBase + '.json',
"urlIgxcModified": outFileNameBase + '.igxc',
"urlIgxcOriginal": outFileNameBase + '_original.igxc',
"transforms": mappingResult,
"igxcModified": igxcContent
}
return result
# save unmodified version of igxc
if igxcContent is not None:
igxcOutfileName = joinOutputPath(outFileNameBase + "_original", 'igxc')
with open(igxcOutfileName, 'w') as igxcOutfile:
json.dump(igxcContent,
igxcOutfile,
indent=4,
separators=(',', ': '),
sort_keys=False)
# result not in cache? proceed with baking
root = None
try:
root = igxc.load(igxcContent, basePath)
except AttributeError as e:
errorMsg = "attributes missing in igxc ({})".format(" ".join(e.args))
colorprint("startWithDirectArgs: " + errorMsg, 31)
print(e)
result = {
"error": errorMsg,
"urlIgxcOriginal": outFileNameBase + '_original.igxc'
}
return result
except ConnectionError as e:
errorMsg = "file referenced in igxc could not be fetched " + " ".join(
e.args)
colorprint("startWithDirectArgs: " + errorMsg, 31)
print(e)
result = {
"error": errorMsg,
"urlIgxcOriginal": outFileNameBase + '_original.igxc'
}
return result
except Exception as e:
errorMsg = "igxc couldn't be loaded"
colorprint("startWithDirectArgs: " + errorMsg, 31)
print(e)
print(type(e))
result = {
"error": errorMsg,
"urlIgxcOriginal": outFileNameBase + '_original.igxc'
}
return result
triCounter = visitor.TriCounter()
root.accept(triCounter)
print('total triangles', triCounter.count)
vertices = numpy.ndarray((triCounter.count, 3, 3), dtype=numpy.float)
normals = numpy.ndarray((triCounter.count, 3, 3), dtype=numpy.float)
texcoord = numpy.ndarray((triCounter.count, 3, 2), dtype=numpy.float)
meshCounter = visitor.MeshCounter()
root.accept(meshCounter)
print('total meshes', meshCounter.count)
amountBucketsX = math.ceil(math.sqrt(meshCounter.count))
amountBucketsY = math.ceil(meshCounter.count / amountBucketsX)
print('buckets: {}x{}'.format(amountBucketsX, amountBucketsY))
uvPacker = visitor.SimplePacker(amountBucketsX, amountBucketsY,
resolutionValue)
triExtractor = visitor.TransformedTriExtractor(vertices,
normals,
texcoord,
packer=uvPacker)
root.accept(triExtractor)
# if face normals should be used, empty normals array
if "face_normals" in args and args["face_normals"] == True:
normals = numpy.zeros((0, 3, 3), dtype=numpy.float)
# print(vertices)
# print(texcoord)
# print(triExtractor.mapping)
# print("Packer:", uvPacker.i)
img = generateMap(vertices, normals, texcoord,
(resolutionValue, resolutionValue))
# save AO map image
output = joinOutputPath(outFileNameBase, 'png')
print("Save output at", joinOutputPath(outFileNameBase, 'png'))
img.save(output)
# save AO mapping
mappingOutfileName = joinOutputPath(outFileNameBase, 'json')
with open(mappingOutfileName, 'w') as outfile:
json.dump(triExtractor.mapping,
outfile,
indent=4,
separators=(',', ': '),
sort_keys=True)
# extend existing IGXC with AO entries
if igxcContent is not None:
modifyIgxc(igxcContent, outFileNameBase + '.png', triExtractor.mapping)
igxcOutfileName = joinOutputPath(outFileNameBase, 'igxc')
with open(igxcOutfileName, 'w') as igxcOutfile:
json.dump(igxcContent,
igxcOutfile,
indent=4,
separators=(',', ': '),
sort_keys=False)
if debug:
import viewer
viewer.debug_view(vertices, texcoord, image=img)
result = {
"urlAoMapImage": outFileNameBase + '.png',
"urlAoMappingJson": outFileNameBase + '.json',
"urlIgxcModified": outFileNameBase + '.igxc',
"urlIgxcOriginal": outFileNameBase + '_original.igxc',
"transforms": triExtractor.mapping,
"igxcModified": igxcContent
}
return result
if __name__ == '__main__':
start()