-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrpdc.py
560 lines (419 loc) · 19.3 KB
/
rpdc.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
# check python version, this script requires python3
import sys
if sys.version_info[0] < 3 or (sys.version_info[0] == 3 and sys.version_info[1] < 8):
print('ERROR: This script requires Python 3.8 or higher')
sys.exit(1)
import urllib.request
import urllib.error
import os
import json
import time
from argparse import ArgumentParser
import pathlib
# add the six and jsonschema packages (locally, so users don't have to know pip etc.)
sys.path.insert(0, os.path.abspath("schema/six"))
sys.path.insert(0, os.path.abspath("schema/"))
import jsonschema
# ERROR CODES
OPTIMIZATION_FAILED_ERROR = 2
# ################################ #
# RapidCompact.Cloud API endpoints #
# ################################ #
LOGIN_ENDPOINT = "login"
PRESIGNED_FETCH_ENDPOINT = "rawmodel/api-upload/start"
FINALIZE_RAWMODEL_ENDPOINT = "rawmodel/{id}/api-upload/complete"
OPTIMIZE_MODEL_ENDPOINT = "rawmodel/optimize/{id}"
BASE_ASSET_ENDPOINT = "rawmodel/{id}"
RAPID_MODEL_ENDPOINT = "rapidmodel/{id}"
# ################################ #
# Helper Functions #
# ################################ #
def executeServerRequest(request):
try:
response = urllib.request.urlopen(request)
except urllib.error.URLError as e:
print("******************************************************")
if hasattr(e, "reason"):
print("ERROR: Failed to fulfill the request.")
print("Error code: ", e.code)
print("Reason: ", e.reason)
elif hasattr(e, "code"):
print("ERROR: The server couldn't fulfill the request.")
print("Error code: ", e.code)
try:
errorLines = e.readlines()
try:
serverErrors = json.loads(errorLines[0].decode("utf-8"))
print("Server message: \"" + serverErrors["message"] + "\"")
print("Reported Errors:")
print(json.dumps(serverErrors["errors"], indent=2))
except:
# json parsing doesn't work - just try to print the string then
print("Server message: \"")
for errLine in errorLines:
print(errLine.decode("utf-8"))
except:
# we don't know how to parse the content
print(e.readlines())
print("******************************************************")
return None, e.code
return response, response.getcode()
# #############################################################################
def getServerRequestJSON(request):
response, code = executeServerRequest(request)
if not response:
return None, code
res = response.read().decode("utf8")
try:
rJSON = json.loads(res)
except:
print("ERROR: Could not parse the result from server.")
return rJSON, response.getcode()
# #############################################################################
def downloadFile(fileURL, outputFilePath):
req = urllib.request.Request(fileURL)
response, code = executeServerRequest(req)
if not response:
print("Error: No data received to write output file \"" + outputFilePath + "\".")
return False
try:
with open(outputFilePath, 'wb') as f:
f.write(response.read())
except:
print("Error: Cannot write to output file \"" + outputFilePath + "\".")
return False
return True
# #############################################################################
def getUploadURLs(fileExt, accessToken, modelLabel, baseUrl):
reqHeaders = {'Authorization' : 'Bearer ' + accessToken,
'content-type' : 'application/json'}
payload = {"filenames": ["rapid" + fileExt],"model_name": modelLabel}
data = json.dumps(payload).encode("utf8")
print("Obtaining Signed Upload URLs ...")
req = urllib.request.Request(baseUrl+PRESIGNED_FETCH_ENDPOINT, data, headers=reqHeaders)
responseJSON, code = getServerRequestJSON(req)
return responseJSON
# #############################################################################
def uploadRawModel(modelFile, fileExt, uploadURLs, accessToken, baseUrl):
dataModel = None
try:
dataModel = open(modelFile, 'rb')
except IOError:
print("Error: cannot open model file \"" + modelFile + "\"")
return False
url_model = uploadURLs['links']['s3_upload_urls']['rapid' + fileExt]
id = uploadURLs['id']
# upload binary data via PUT
print("Uploading model file ...")
req = urllib.request.Request(url_model, data=dataModel.read(), method='PUT')
response, code = executeServerRequest(req)
if response == None:
print("Could not upload model file.")
return False
# finalize model upload
reqHeaders = {'Authorization' : 'Bearer ' + accessToken,
'Content-Type' : 'application/json'}
print("Inserting into base assets section ...")
finalizeUploadURL= baseUrl+FINALIZE_RAWMODEL_ENDPOINT.replace("{id}", str(id))
req = urllib.request.Request(finalizeUploadURL, headers=reqHeaders)
response, code = executeServerRequest(req)
if response == None:
print("Could not upload model file.")
return False
upload_status = ""
checkOptStatusURL = baseUrl+BASE_ASSET_ENDPOINT.replace("{id}", str(id))
# if this is a zip file, make sure that the server processed the file (unzipped)
if fileExt == ".zip":
print("Waiting for the server to unzip the model.")
upload_status = "unzipping"
while upload_status == "unzipping":
req = urllib.request.Request(checkOptStatusURL, headers=reqHeaders)
rJSON, code = getServerRequestJSON(req)
if rJSON == None:
print("Could not get the base asset status.")
return False
upload_status = rJSON["data"]["upload_status"]
time.sleep(1)
# wait for the model to be ready
print("Waiting for the server to analyse the model.")
while upload_status != "complete":
req = urllib.request.Request(checkOptStatusURL, headers=reqHeaders)
rJSON, code = getServerRequestJSON(req)
if rJSON == None:
print("Could not get the base asset status.")
return False
upload_status = rJSON["data"]["upload_status"]
time.sleep(1)
return True
# #############################################################################
def deleteBaseAsset(id, accessToken):
reqHeaders = {'Authorization' : 'Bearer ' + accessToken,
'Content-Type' : 'application/json'}
# delete base asset via DELETE
print("Deleting base asset from cloud storage ...")
deleteURL = baseUrl+BASE_ASSET_ENDPOINT.replace("{id}", str(id))
req = urllib.request.Request(deleteURL, headers=reqHeaders, method='DELETE')
response, code = executeServerRequest(req)
if response == None or code != 200:
print("Could not delete base asset from cloud storage.")
return False
else:
print("Success.")
return True
# #############################################################################
def deleteRapidModel(id, accessToken):
reqHeaders = {'Authorization' : 'Bearer ' + accessToken,
'Content-Type' : 'application/json'}
# delete base asset via DELETE
print("Deleting optimized model from cloud storage ...")
deleteURL = baseUrl+RAPID_MODEL_ENDPOINT.replace("{id}", str(id))
req = urllib.request.Request(deleteURL, headers=reqHeaders, method='DELETE')
response, code = executeServerRequest(req)
if response == None or code != 200:
print("Could not delete optimized model from cloud storage.")
return False
else:
print("Success.")
return True
# #############################################################################
def makeProgessBarStr(progress):
barStr = "["
percSteps = 20
for i in range(1,percSteps+1):
if progress >= i * 5:
barStr += "#"
else:
barStr += "_"
barStr += "]"
return barStr
# #############################################################################
def generateOptimizedVariant(modelID, outputModelFilePrefix, variant, accessToken, baseUrl):
rawModelIDStr = str(modelID)
reqHeaders = {'Authorization' : 'Bearer ' + accessToken,
'Content-Type' : 'application/json'}
# submit optimization job
payload = variant
data = json.dumps(payload).encode("utf8")
print("Submitting optimization job ...")
optimizedModelURL= baseUrl+OPTIMIZE_MODEL_ENDPOINT.replace("{id}", rawModelIDStr)
req = urllib.request.Request(optimizedModelURL, data=data, headers=reqHeaders)
rJSON, code = getServerRequestJSON(req)
if rJSON == None:
print("Could not submit optimization job.")
return -1
rapidModelID = rJSON["id"]
rapidModelIDStr = str(rapidModelID)
# check model status
print("Waiting for optimization to complete for rapid model " + rapidModelIDStr)
opt_status = "sent_to_queue"
checkOptStatusURL = baseUrl+RAPID_MODEL_ENDPOINT.replace("{id}", rapidModelIDStr)
while (opt_status != "done"):
req = urllib.request.Request(checkOptStatusURL, headers=reqHeaders)
rJSON, code = getServerRequestJSON(req)
# too many requests?
if code == 429:
print("Retrying in 30 seconds...")
time.sleep(30)
continue
if rJSON == None:
print("Could not get the optimization status.")
return -1
if "data" in rJSON and "progress" in rJSON["data"]:
progress = rJSON["data"]["progress"]
barDisplay = makeProgessBarStr(progress)
for i in range(3-len(str(progress))):
barDisplay += " "
pStep = ""
if rJSON["data"]["processing_step"]:
pStep = " | " + rJSON["data"]["processing_step"]
for i in range(45-len(pStep)):
pStep += " "
print(f"\rProgress: {barDisplay} {progress}%{pStep}", end = '')
opt_status = rJSON["data"]["optimization_status"]
if (opt_status != "sent_to_queue" and opt_status != "done"):
print("Error: Unexpected status code from optimization run (" + opt_status + ").")
return -1
time.sleep(2)
# optimization successful
barDisplayFinal = makeProgessBarStr(100)
print(f"\rProgress: {barDisplayFinal} 100% | Finished. \n", end = '')
exports = variant["config"]["compressionAndExport"]["fileExports"]
downloadURLs = rJSON["data"]["downloads"]["all"]
# name and download results
# using "downloads"->"all" is the most straightforward way,
# since each entry there corresponds to one entry in "fileExports"
# if you need to rename and differentiate between file formats,
# there are also individual lists / values for each file extension
i = 0
for key in downloadURLs:
dlURL = downloadURLs[key]
fileType = exports[i]["fileType"]
fileExt = fileType
if (fileType == "obj" or fileType == "gltf"):
fileExt = ".zip"
filenameSuffix = "_e" + str(i) + "." + fileExt
downloadFile(dlURL, outputModelFilePrefix + filenameSuffix)
i += 1
return rapidModelID
# #############################################################################
def validateJSONWithAPISchema(variantConfig, schemaFile, silent):
schema = None
try:
with open(schemaFile) as f:
schema = json.load(f)
except:
if (silent == False):
print("Error: Unable to validate configuration against schema: schema couldn't be read from file \"" + schemaFile + "\".")
return False
try:
jsonschema.validate(variantConfig, schema)
if (silent == False):
print("Variant configuration passed validation.")
return True
except Exception as e:
if (silent == False):
print("Error: Variant configuration is not valid - see JSON validation report on how to fix this:")
print("********************************************************************************")
print(e)
print("********************************************************************************")
return False
# #############################################################################
def validateJSONConfigContent(variantConfig):
exports = variantConfig["compressionAndExport"]["fileExports"]
exportType0 = exports[0]["fileType"]
# the V1 currently expects the first export to always be in glb format
if (exportType0 != "glb"):
print("Error when checking additional constraint: With API V1, first export must be \"glb\" (given: \"" + exportType0 + "\").")
return False
return True
# ################################ #
# Main Program #
# ################################ #
# argument parsing
parser = ArgumentParser()
parser.add_argument("model", help="input directory or 3D model (must be a .glb file OR .zip file OR base asset ID in the form <number>.id)")
parser.add_argument("-b", "--base-url", dest="baseUrl", default="https://api.rapidcompact.com/api/v2/", help="api base url")
parser.add_argument("-c", "--credentials-file", dest="credentialsFile", default="credentials.json", help="credentials JSON file")
parser.add_argument("-v", "--variants-file", dest="variantsFile", default="variants.json", help="variant definitions JSON file")
parser.add_argument("-s", "--settings-file", dest="settingsFile", default="settings.json", help="settings JSON file")
parser.add_argument("-l", "--label", dest="modelLabel", default="", help="label for the model")
parser.add_argument("-o", "--origin", dest="originDesc", default="Gallery Uploader Script", help="origin label for the model")
parser.add_argument('--cleanup', dest='cleanup', action='store_true')
parser.add_argument('--no-cleanup', dest='cleanup', action='store_false')
parser.add_argument("-e", "--exit", dest="exitOnError", default=False, help="exit script on optimize error. Set False or True")
parser.set_defaults(cleanup=True)
pArgs = parser.parse_args()
argsDict = vars(pArgs)
modelFile = argsDict["model"]
variantsFile = argsDict["variantsFile"]
credentialsFile = argsDict["credentialsFile"]
settings_file = argsDict["settingsFile"]
modelLabel = argsDict["modelLabel"]
originDesc = argsDict["originDesc"]
baseUrl = argsDict["baseUrl"]
cleanup = argsDict["cleanup"]
exitOnError = argsDict["exitOnError"]
print("API Endpoint: "+baseUrl)
userCredentials = None
userVariants = None
settings = None
try:
with open(credentialsFile) as f:
userCredentials = json.load(f)
except:
print("Unable to load and parse credentials JSON file \"" + credentialsFile + "\". Make sure the file exists and is valid JSON.")
sys.exit(1)
try:
with open(variantsFile) as f:
userVariants = json.load(f)
except:
print("Unable to load and parse variant definitions JSON file \"" + variantsFile + "\". Make sure the file exists and is valid JSON.")
sys.exit(1)
try:
with open(settings_file) as f:
settings = json.load(f)
except(OSError, json.JSONDecodeError):
print("Unable to load and parse settings JSON file \"" + settings_file + "\". Make sure the file exists and is valid JSON.")
sys.exit(1)
# Load Settings
SchemaJSONPath = settings["schemaPath"]
# For ExitOnError Flag
failedOptimizations = 0
# 1) obtain token from credentials file
accessToken = userCredentials["token"]
# iterate over input directory OR use input file
directoryMode = os.path.isdir(modelFile)
filesToProcess =[]
if directoryMode:
print("Running in directory mode.")
filesToProcess = os.listdir(modelFile)
# to streamline code below, add the path prefix, just like in single-file mode
for i in range(0, len(filesToProcess)):
filesToProcess[i] = os.path.join(modelFile, filesToProcess[i])
else:
print("Running in single-file mode.")
filesToProcess = [modelFile]
for nextModelFile in filesToProcess:
nextModelFileWithoutExt = nextModelFile[0:nextModelFile.rfind('.')]
nextModelFileWithoutExtAndPath = os.path.basename(nextModelFileWithoutExt)
if (accessToken == ""):
print("Couldn't log in. Are your credentials valid?")
sys.exit(1)
if (nextModelFile.endswith(".id")):
modelID = nextModelFile[:-3]
else:
fileExt = pathlib.Path(nextModelFile).suffix
allVariantsInvalid = True
# validate before uploading, to prevent unnecessary waiting and traffic
for variantName in userVariants["variants"]:
variant = userVariants["variants"][variantName]
print("Validating configuration for variant \"" + variantName + "\".")
if (validateJSONWithAPISchema(variant["config"], SchemaJSONPath, False)):
allVariantsInvalid = False
if (allVariantsInvalid):
print("No valid variant configuration found. Terminating.")
sys.exit(1)
# 2) obtain signed URLs for upload
if (modelLabel != ""):
uploadURLs = getUploadURLs(fileExt, accessToken, modelLabel, baseUrl)
else:
uploadURLs = getUploadURLs(fileExt, accessToken, nextModelFileWithoutExtAndPath, baseUrl)
if (uploadURLs is None):
print("Couldn't obtain signed upload URLs from server.")
sys.exit(1)
# 3) upload model into the "Base Assets" section, or take existing ID
success = uploadRawModel(nextModelFile, fileExt, uploadURLs, accessToken, baseUrl)
if (success == False):
print("Couldn't upload base asset.")
sys.exit(1)
modelID = uploadURLs['id']
# 4) create optimized variants
variantIdx = 0
newRapidModelIDs = []
for variantName in userVariants["variants"]:
variant = userVariants["variants"][variantName]
# check if output folder exists
if not os.path.isdir("output"):
os.mkdir("output")
outputModelFilePrefix = "output/" + nextModelFileWithoutExtAndPath + "_" + variantName
print("Producing asset variant \"" + variantName + "\".")
if (validateJSONWithAPISchema(variant["config"], SchemaJSONPath, True)):
if (validateJSONConfigContent(variant["config"])):
resultRapidModelID = generateOptimizedVariant(modelID, outputModelFilePrefix, variant, accessToken, baseUrl)
if (resultRapidModelID != -1):
newRapidModelIDs.append(resultRapidModelID)
else:
failedOptimizations += 1
variantIdx += 1
# 5) where desired, delete the base asset from the cloud storage after optimization
if (not nextModelFile.endswith(".id")):
fileExt = pathlib.Path(nextModelFile).suffix
if (cleanup):
print("Cleaning up: deleting uploaded base asset and optimized results. If you want to skip this step, run again with option --no-cleanup.")
deleteBaseAsset(uploadURLs['id'], accessToken)
for rapidModelID in newRapidModelIDs:
deleteRapidModel(rapidModelID, accessToken)
# Exit with error
if(exitOnError and failedOptimizations > 0):
print("Exiting with error because of failed optimizations")
sys.exit(OPTIMIZATION_FAILED_ERROR)