-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscene_creation_example.py
More file actions
117 lines (97 loc) · 3.93 KB
/
scene_creation_example.py
File metadata and controls
117 lines (97 loc) · 3.93 KB
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
import getopt
import sys
import requests
SHAPESPARK_ROOT_URL = 'https://cloud.shapespark.com'
def usage():
print("""
Import a model to Shapespark via HTTP API.
Usage:
%(prog)s -t PATH-TO-FILE -m PATH-TO-FILE
-m, --model Path to a .zip archive with model and textures.
-s, --scene Name of a scene to be created (default test-scene).
-t, --token Path to a file with user authorization token.
-h, --help Print this help.
""" % {'prog': sys.argv[0]})
sys.exit(1)
def read_text_file(file_path):
with open(file_path, 'r', encoding='ascii') as f:
return f.read()
def read_binary_file(file_path):
with open(file_path, 'rb') as f:
return f.read()
def format_error(error_response):
status_code = error_response.status_code
content_type = error_response.headers.get('Content-Type', '')
if content_type.startswith('application/json'):
error_json = error_response.json()
api_code = error_json.get('code')
message = error_json.get('message')
else:
api_code = None
message = error_response.text
if api_code is not None:
return f'{status_code} ({api_code}) {message}'
return f'{status_code} {message}'
def main():
try:
optlist, _ = getopt.gnu_getopt(sys.argv[1:],
'm:t:s:h',
['model=', 'token=', 'scene=', 'help'])
except getopt.GetoptError as ex:
print('Command line arguments parsing error: ' + str(ex))
usage()
scene_name = 'test-scene'
token_path = None
model_path = None
for opt, arg in optlist:
if opt in ('-h', '--help'):
usage()
elif opt in ('-m', '--model'):
model_path = arg
elif opt in ('-s', '--scene'):
scene_name = arg
elif opt in ('-t', '--token'):
token_path = arg
else:
assert False, 'unhandled option'
if model_path is None:
print('Path to the .zip archive with the model is missing')
usage()
if token_path is None:
print('Path to the authorization token file is missing')
usage()
(username, token) = read_text_file(token_path).strip().split(' ')
url = '{0}/scenes/{1}/import-upload-init'.format(
SHAPESPARK_ROOT_URL, scene_name)
response = requests.post(url,auth=(username, token), verify=True)
if response.status_code != 200:
raise Exception('POST import-upload-init failed: ' +
format_error(response))
put_url = response.json()['uploadUrl']
data = read_binary_file(model_path)
response = requests.put(put_url, data=data)
if response.status_code != 200:
raise Exception('PUT failed: {0}, {1}'.format(
response.status_code, response.text))
url = '{0}/scenes/{1}/import-upload-done'.format(
SHAPESPARK_ROOT_URL, scene_name)
response = requests.post(url,auth=(username, token), verify=True)
if response.status_code != 200:
raise Exception('POST import-upload-done failed: ' +
format_error(response))
else:
print('Model uploaded for import. Wait for the import to finish at {0}'
.format(response.json()['watchUrl']))
# Get a list of users scenes (additional permissions may be required):
url = '{0}/scenes/'.format(SHAPESPARK_ROOT_URL)
response = requests.get(url,auth=(username, token), verify=True)
if response.status_code != 200:
raise Exception('GET a list of scenes failed: ' +
format_error(response))
for scene in response.json():
print((' Scene: {0}\n\t watchUrl: {1}\n\t sceneUrl: {2}\n\t ' +
'assetsUrl: {3}').format(scene.get('name'),
scene.get('watchUrl', 'none'),
scene.get('sceneUrl', 'none'),
scene.get('assetsUrl', 'none')))
main()