-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.py
460 lines (396 loc) · 12.5 KB
/
func.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
import asyncio
from os import system, name
import json
from util import *
######################################
# plugin settings #
######################################
dev = "Sawaumi"
request_id = "VtextureSwap_v0.2.0 "
plugin_name = "VtextureSwap"
api_version = "1.0"
######################################
# functions #
######################################
def clear():
# for windows
if name == 'nt':
_ = system('cls')
# for mac and linux(here, os.name is 'posix')
else:
_ = system('clear')
async def get_auth_token(ws):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "AuthenticationTokenRequest",
"data": {
"pluginName": plugin_name,
"pluginDeveloper": dev,
}
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
auth_token = pack['data']['authenticationToken']
return auth_token
async def authorize(ws, auth_token):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "AuthenticationRequest",
"data": {
"pluginName": plugin_name,
"pluginDeveloper": dev,
"authenticationToken": auth_token
}
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
async def get_param_list(ws):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "InputParameterListRequest"
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
async def create_param(ws, parameterName, lower_bound, upper_bound, defaultValue, explanation):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": "SomeID",
"messageType": "ParameterCreationRequest",
"data": {
"parameterName": parameterName,
"explanation": explanation,
"min": lower_bound,
"max": upper_bound,
"defaultValue": defaultValue
}
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
async def get_param_value(ws, param):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": "SomeID",
"messageType": "ParameterValueRequest",
"data": {
"name": param
}
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
async def set_param_value(ws, param, value):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": "SomeID",
"messageType": "InjectParameterDataRequest",
"data": {
"parameterValues": [
{
"id": param,
"value": value
}
]
}
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
async def get_curr_model(ws):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "CurrentModelRequest"
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
async def list_avail_model(ws):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "AvailableModelsRequest"
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
async def get_api_state(ws):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "APIStateRequest",
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
async def get_states(ws):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "StatisticsRequest"
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
async def if_face_found(ws):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": "1.0",
"requestID": "SomeID",
"messageType": "FaceFoundRequest"
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
try:
data = pack["data"]["found"]
return data
except KeyError:
print(pack)
# return pack["data"]["found"]
async def get_vts_folder_info(ws):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "VTSFolderInfoRequest"
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
# authres = pack['data']
# what was this for?
return pack
async def get_curr_hotkeys(ws, model_id):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "HotkeysInCurrentModelRequest",
"data": {
"modelID": model_id,
}
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
async def exec_hotkey(ws, hotkey_id):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "HotkeyTriggerRequest",
"data": {
"hotkeyID": hotkey_id
}
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
# async def force_exec_hotkey(websocket, hkid):
# payload = {
# "apiName": "VTubeStudioPublicAPI",
# "apiVersion": api_version,
# "requestID": request_id,
# "messageType": "HotkeyTriggerRequest",
# "data": {
# "hotkeyID": hkid
# }
# }
# await websocket.send(json.dumps(payload))
# return
async def get_exp_state(ws, exp_file_name, if_include_detail=False):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "ExpressionStateRequest",
"data": {
"details": if_include_detail,
"expressionFile": exp_file_name,
}
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
async def if_exp_active(ws, exp_file_name):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "ExpressionStateRequest",
"data": {
"details": False,
"expressionFile": exp_file_name,
}
}
await ws.send(json.dumps(payload))
# logging.info("calling recv", exc_info=True)
json_data = await ws.recv()
pack = json.loads(json_data)
try:
data = pack["data"]["expressions"][0]["active"]
return data
except KeyError:
print(pack)
# return pack["data"]["expressions"][0]["active"]
async def load_model(ws, model_id):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "ModelLoadRequest",
"data": {
"modelID": model_id
}
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
async def move_model(ws, t, relativity, x, y, rot, size):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "MoveModelRequest",
"data": {
"timeInSeconds": t,
"valuesAreRelativeToModel": relativity,
"positionX": x,
"positionY": y,
"rotation": rot,
"size": size
}
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
async def get_model_art_mesh_list(ws):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "ArtMeshListRequest"
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
async def tint_art_mesh(ws, r=255, g=255, b=255, a=255,
if_tint_all=False, enable_rainbow=False,
if_mix_with_scene_lighting=True,
art_mesh_number="",
exact_array="", con_array="",
tag_exact_array="", tag_contain_array=""):
payload = {
"apiName": "VTubeStudioPublicAPI",
"apiVersion": api_version,
"requestID": request_id,
"messageType": "ColorTintRequest",
"data": {
"colorTint": {
"colorR": r,
"colorG": g,
"colorB": b,
"colorA": a,
"mixWithSceneLightingColor": if_mix_with_scene_lighting,
"jeb_": enable_rainbow
},
"artMeshMatcher": {
"tintAll": if_tint_all,
"artMeshNumber": art_mesh_number,
"nameExact": exact_array,
"nameContains": con_array,
"tagExact": tag_exact_array,
"tagContains": tag_contain_array
}
}
}
await ws.send(json.dumps(payload))
json_data = await ws.recv()
pack = json.loads(json_data)
return pack
async def spin(ws, x, y, s):
await move_model(ws, 0.2, False, x, y, 90, s)
await asyncio.sleep(0.1)
await move_model(ws, 0.2, False, x, y, 180, s)
await asyncio.sleep(0.1)
await move_model(ws, 0.2, False, x, y, 270, s)
await asyncio.sleep(0.1)
await move_model(ws, 0.2, False, x, y, 360, s)
await asyncio.sleep(0.1)
async def fade_away(ws, if_tint_all=False, con_array=None):
if con_array is None:
con_array = [""]
for i in range(16):
await tint_art_mesh(ws, a=255 - i * 17, if_tint_all=if_tint_all, con_array=con_array)
await asyncio.sleep(0.02)
# print("faded away")
# print(con_array)
async def fade_back(ws, if_tint_all=False, con_array=None):
if con_array is None:
con_array = [""]
for i in range(16):
await tint_art_mesh(ws, a=i * 17, if_tint_all=if_tint_all, con_array=con_array)
await asyncio.sleep(0.02)
# print("faded back")
# print(con_array)
async def swap_texture(ws, src_path, dst_path, temp_path):
for src, dst, temp in zip(src_path, dst_path, temp_path):
swap(src, dst, temp)
await asyncio.sleep(2)
await exec_hotkey(ws, "ReloadModelTexture")
print("texture_swapped")
async def swap_debug(ws, src_path, dst_path, temp_path):
for src, dst, temp in zip(src_path, dst_path, temp_path):
print(src, dst, temp)
async def idle_texture(ws, face_is_found, texture_idled):
if face_is_found and not texture_idled:
print("back from idle")
await fade_back(ws, if_tint_all=True)
texture_idled = True
elif not face_is_found and texture_idled:
print("gone idle")
await fade_away(ws, if_tint_all=True)
texture_idled = False
return texture_idled
async def hair_loss(ws, hair_loss_triggered, hair_loss_toggle):
if hair_loss_triggered and not hair_loss_toggle:
print("removed hair")
await fade_away(ws, con_array=["ce", "fa", "qian", "hfa", "tding", "ying"])
hair_loss_toggle = True
elif not hair_loss_triggered and hair_loss_toggle:
print("added hair")
await fade_back(ws, con_array=["ce", "fa", "qian", "hfa", "tding", "ying"])
hair_loss_toggle = False
return hair_loss_toggle