-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPM_API_Request.py
212 lines (164 loc) · 7.31 KB
/
RPM_API_Request.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
import os
import bpy
import requests
def rename_armature(context, url_params_list):
if len(url_params_list) > 1:
print()
print(url_params_list)
print()
postfix = []
if 'quality=low' in url_params_list:
postfix.append('Low')
elif 'quality=medium' in url_params_list:
postfix.append('Med')
elif 'quality=high' in url_params_list:
postfix.append('Hi')
if 'meshLod=0' in url_params_list:
postfix.append('LOD0')
elif 'meshLod=1' in url_params_list:
postfix.append('LOD1')
elif 'meshLod=2' in url_params_list:
postfix.append('LOD2')
if 'textureSizeLimit=256' in url_params_list:
postfix.append('limit256')
elif 'textureSizeLimit=512' in url_params_list:
postfix.append('limit512')
elif 'textureSizeLimit=1024' in url_params_list:
postfix.append('limit1024')
if 'textureAtlas=256' in url_params_list:
postfix.append('atlas256')
elif 'textureAtlas=512' in url_params_list:
postfix.append('atlas512')
elif 'textureAtlas=1024' in url_params_list:
postfix.append('atlas1024')
context.object.name = context.scene.RPM.avatar_name + '_' + '_'.join(postfix)
elif len(url_params_list) <= 1:
context.object.name = context.scene.RPM.avatar_name
class RPM_OT_Request(bpy.types.Operator):
bl_idname = "rpm.make_request"
bl_label = "Download Avatar"
bl_description = "Make Request to RPM API to get avatar .glb file"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
if context.scene.RPM.avatar_id_type == 'shortcode':
avatar_id = context.scene.RPM.avatar_shortcode
else:
avatar_id = context.scene.RPM.avatar_id
url_id = f"https://api.readyplayer.me/v1/avatars/{avatar_id}.glb"
url_params_list = []
url_params_string = ''
quality = context.scene.RPM.quality
# ?quality=low (meshLod=2, textureSizeLimit=256, textureAtlas=256, morphTargets=none)
# ?quality=medium (meshLod=1, textureSizeLimit=512, textureAtlas=512, morphTargets=none)
# ?quality=high (meshLod=0, textureSizeLimit=1024, textureAtlas=1024, morphTargets=none)
meshLod = context.scene.RPM.meshLod
# ?meshLod=0 (highest quality) (default)
# ?meshLod=1 (medium quality)
# ?meshLod=2 (lowest quality)
textureSizeLimit = context.scene.RPM.textureSizeLimit
# ?textureSizeLimit=256
# ?textureSizeLimit=512
# ?textureSizeLimit=1024 (default)
textureAtlas = context.scene.RPM.textureAtlas
# ?textureAtlas=none (default)
# ?textureAtlas=256
# ?textureAtlas=512
# ?textureAtlas=1024
morphTargets = context.scene.RPM.morphTargets
# ?morphTargets=none
# ?morphTargets=default
# ?morphTargets=ARKit
# ?morphTargets=Oculus Visemes
# + or any morph targets, separated with comma
# The default value is "Default,ARKit,Oculus Visemes"
textureChannels = []
textureChannels_string = ''
# ?textureChannels=baseColor,normal,metallicRoughness,emissive,occlusion,none (default)
# ?quality
if quality != "not_set":
url_params_list.append(f"quality={quality}")
# ?meshLod
if meshLod != 0 or len(url_params_list) > 0:
url_params_list.append(f"meshLod={meshLod}")
# ?textureSizeLimit
if textureSizeLimit != '1024' or len(url_params_list) > 0:
url_params_list.append(f"textureSizeLimit={textureSizeLimit}")
# ?textureAtlas
if textureAtlas != "none" or len(url_params_list) > 0:
url_params_list.append(f"textureAtlas={textureAtlas}")
# ?morphTargets
if morphTargets != "all":
if morphTargets == "custom":
morphTargets = context.scene.RPM.custom_morph_targets_textarea
if context.scene.RPM.custom_morph_targets_enable_ARKit:
morphTargets += ",ARKit"
if context.scene.RPM.custom_morph_targets_enable_Oculus_Visemes:
morphTargets += ",Oculus Visemes"
url_params_list.append(f"morphTargets={morphTargets}")
else:
url_params_list.append(f"morphTargets={morphTargets}")
# ?textureChannels
if context.scene.RPM.baseColor:
textureChannels.append("baseColor")
if context.scene.RPM.normal:
textureChannels.append("normal")
if context.scene.RPM.metallicRoughness:
textureChannels.append("metallicRoughness")
if context.scene.RPM.emissive:
textureChannels.append("emissive")
if context.scene.RPM.occlusion:
textureChannels.append("occlusion")
if context.scene.RPM.none:
textureChannels = []
textureChannels.append("none")
if len(textureChannels) > 0:
textureChannels_string = "textureChannels=" + ",".join(textureChannels)
# ?pose
if context.scene.RPM.pose == "T":
url_params_list.append("pose=T")
# ?useDracoMeshCompression
if context.scene.RPM.useDracoMeshCompression:
url_params_list.append("useDracoMeshCompression=true")
url_params_string = "&".join(url_params_list) + f"{'&' if len(textureChannels_string) > 0 else '' }" + textureChannels_string
url = url_id + f"{'?' if len(url_params_string) > 0 else ''}{url_params_string}"
print('--------------------------------------------')
print()
print(f'Making request to Ready Player Me API')
print()
print(f'{url}')
print()
print(f'quality: {quality}')
print(f'meshLod: {meshLod}')
print(f'textureSizeLimit: {textureSizeLimit}')
print(f'textureAtlas: {textureAtlas}')
print(f'morphTargets: {morphTargets}')
print()
print(f'textureChannels: {textureChannels if len(textureChannels) > 0 else "All"}')
print()
response = requests.get(url)
if response.status_code == 200:
self.report({"INFO"}, "Avatar downloaded successfully")
# Check if the blend file has been saved
if bpy.data.filepath == "":
self.report({"ERROR"}, "Please save the blend file before downloading the avatar")
return {"FINISHED"}
# Get the path of the blend file
blend_path = bpy.path.abspath("//")
print(f'blend_path: {blend_path}')
# Save the file
with open(f"{blend_path}avatar.glb", "wb") as f:
f.write(response.content)
# Import the file as glTF
bpy.ops.import_scene.gltf(filepath=f"{blend_path}avatar.glb")
# Delete the file after importing
os.remove(f"{blend_path}avatar.glb")
# Rename the armature by adding the params if any
rename_armature(context, url_params_list)
elif response.status_code == 404:
self.report({"ERROR"}, "The requested avatar is not available")
else:
self.report({"ERROR"}, "Avatar could not be downloaded")
return {"FINISHED"}