-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHEVA_Portal.py
446 lines (362 loc) · 13.9 KB
/
HEVA_Portal.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
#
# Created by scaled
#
#
# You can configure settings there:
#
ARMATURE_NAME = 'Girl_HEVA'
FACE_NAME = 'Girl_Body'
PORT = 9000
CAPTURE_FRAMERATE = 30
# Just code
import bpy
from pythonosc.dispatcher import Dispatcher
from pythonosc.osc_server import ThreadingOSCUDPServer
from threading import Thread
from time import perf_counter,sleep
from mathutils import Vector,Quaternion
import pyaudio
import wave
import datetime
def ShowMessageBox(message = "", title = "HEVA_Portal", icon = 'INFO'):
def draw(self, context):
self.layout.label(text=message)
bpy.context.window_manager.popup_menu(draw, title = title, icon = icon)
# Object varriables
FRAMERATE = bpy.context.scene.render.fps / bpy.context.scene.render.fps_base
object_body = bpy.data.objects[ARMATURE_NAME]
object_face = bpy.data.objects[FACE_NAME].data.shape_keys
player_pose = object_body.pose.bones
player_values = object_face.key_blocks
# Audio settings
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 48000
# Sound varriables
p = pyaudio.PyAudio()
stream = None
frames = []
# Animation varriables
action_name = ""
capture_start_frame = 0
objects_capture_last_frame = 0
capture = False
local_frame_current = 0
time_previous = 0
capture_frame = 0
co = Vector((0, 0))
objects_capture_ready = False
# VMC varriables
bones_to_send = {
'Head': player_pose['J_Bip_C_Head'],
'Neck': player_pose['J_Bip_C_Neck'],
'LeftEye': player_pose['J_Adj_L_FaceEye'],
'RightEye': player_pose['J_Adj_R_FaceEye'],
'A': player_values['MTH_A'],
'U': player_values['MTH_U'],
'E': player_values['MTH_E'],
'Fun': player_values['MTH_Fun'],
'Blink': player_values['EYE_Close'],
'Blink_L': player_values['EYE_Close_L'],
'Blink_R': player_values['EYE_Close_R'],
'BrowsDownUp': player_values['BRW_Up'],
#'BrowsDownUp': player_values['BRW_Fun'],
}
bones_to_apply1 = {
# 'Hips': [Vector(), Quaternion()],
# 'LeftUpperLeg': [Vector(), Quaternion()],
# 'RightUpperLeg': [Vector(), Quaternion()],
# 'LeftLowerLeg': [Vector(), Quaternion()],
# 'RightLowerLeg': [Vector(), Quaternion()],
# 'LeftFoot': [Vector(), Quaternion()],
# 'RightFoot': [Vector(), Quaternion()],
# 'Spine': [Vector(), Quaternion()],
# 'Chest': [Vector(), Quaternion()],
# 'Neck': [Vector(), Quaternion()],
'Head': [Vector(), Quaternion()],
'Neck': [Vector(), Quaternion()],
'LeftEye': [Vector(), Quaternion()],
'RightEye': [Vector(), Quaternion()],
# 'LeftShoulder': [Vector(), Quaternion()],
# 'RightShoulder': [Vector(), Quaternion()],
# 'LeftUpperArm': [Vector(), Quaternion()],
# 'RightUpperArm': [Vector(), Quaternion()],
# 'LeftLowerArm': [Vector(), Quaternion()],
# 'RightLowerArm': [Vector(), Quaternion()],
# 'LeftHand': [Vector(), Quaternion()],
# 'RightHand': [Vector(), Quaternion()],
}
bones_to_apply3 = {
'A': 0,
'U': 0,
'E': 0,
'Fun': 0,
'Blink': 0,
'Blink_L': 0,
'Blink_R': 0,
'BrowsDownUp': 0,
}
bones_to_apply1_list = list(bones_to_apply1)
bones_to_apply3_list = list(bones_to_apply3)
def heva_receiver_set_pos(address, *args):
global bones_to_send
global bones_to_apply1
bone_name = args[0]
if bone_name in bones_to_apply1:
bones_to_apply1[bone_name][0][0] = args[1]
bones_to_apply1[bone_name][0][1] = args[2]
bones_to_apply1[bone_name][0][2] = args[3]
bones_to_apply1[bone_name][1][0] = args[7]
bones_to_apply1[bone_name][1][1] = args[6]
bones_to_apply1[bone_name][1][2] = args[5]
bones_to_apply1[bone_name][1][3] = args[4]
def heva_receiver_set_val(address, *args):
global bones_to_send
global bones_to_apply3
#shape_key = bones_to_send[args[0]]
#shape_key.value = args[1]
bone_name = args[0]
if bone_name in bones_to_apply3:
bones_to_apply3[bone_name] = args[1]
def heva_receiver_set_keyframes(address, *args):
#bpy.context.window_manager.update_tag()
#bpy.context.scene.frame_current = this_frame
#if len(bpy.context.view_layer.depsgraph.updates) > 0:
# bpy.context.view_layer.depsgraph.update()
#bpy.context.view_layer.update()
#bpy.context.evaluated_depsgraph_get().update()
#for obj in bpy.context.scene.objects:
# obj.hide_render = obj.hide_render
#global object_body
#object_body.hide_render = not object_body.hide_render
#bpy.context.scene.frame_current += 1
#bpy.context.scene.frame_set(bpy.context.scene.frame_current)
global objects_capture_ready
objects_capture_ready = True
pass
heva_receiver = None
def init_osc_server():
dispatcher = Dispatcher()
dispatcher.map("/VMC/Ext/Bone/Pos", heva_receiver_set_pos)
dispatcher.map("/VMC/Ext/Blend/Val", heva_receiver_set_val)
dispatcher.map("/VMC/Ext/Blend/Apply", heva_receiver_set_keyframes)
try:
global heva_receiver
heva_receiver = ThreadingOSCUDPServer(("0.0.0.0", PORT), dispatcher)
heva_receiver_thread = Thread(target=heva_receiver.serve_forever)
heva_receiver_thread.start()
return True
except OSError:
ShowMessageBox("OSC Server Not started", icon='ERROR')
return False
def capture_start():
global capture
global capture_frame
global capture_start_frame
global local_frame_current
global FRAMERATE
global object_body
global object_face
global time_previous
global co
global objects_capture_last_frame
global objects_capture_ready
global CHUNK
global FORMAT
global CHANNELS
global RATE
global action_name
global p
global stream
global frames
capture_start_frame = bpy.context.scene.frame_current
local_frame_current = bpy.context.scene.frame_current / FRAMERATE
time_previous = perf_counter()
for object in {object_body, object_face}:
if object.animation_data and object.animation_data.nla_tracks:
for nla_track in object.animation_data.nla_tracks:
nla_track.mute=True
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
action_name = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
for bone_name in bones_to_apply1_list:
bone = bones_to_send[bone_name]
d
bone.keyframe_insert('location')
bone.keyframe_insert('rotation_quaternion')
rig_action = object_body.animation_data.action
for fcurve in rig_action.fcurves:
fcurve.mute = True
for bone_name in bones_to_apply3_list:
bone = bones_to_send[bone_name]
bone.keyframe_insert('value')
rig_action = object_face.animation_data.action
for fcurve in rig_action.fcurves:
fcurve.mute = True
def capture_stop():
global capture
global capture_frame
global capture_start_frame
global local_frame_current
global FRAMERATE
global object_body
global object_face
global time_previous
global co
global objects_capture_last_frame
global objects_capture_ready
global CHUNK
global FORMAT
global CHANNELS
global RATE
global action_name
global p
global stream
global frames
capture_frame = 0
for object in {object_body, object_face}:
if object.animation_data and object.animation_data.action:
rig_action = object.animation_data.action
rig_action.name = action_name + ' ' + object.name
#for fcurve in rig_action.fcurves:
# fcurve.mute = False
for fcurve in rig_action.fcurves:
fcurve.mute = False
strip = None
try:
strip = object.animation_data.nla_tracks.active.strips.new(action_name, capture_start_frame, rig_action)
except:
try:
strip = object.animation_data.nla_tracks.active.strips.new(action_name, capture_start_frame + 1, rig_action)
except:
object.animation_data.nla_tracks.new()
strip = object.animation_data.nla_tracks.active.strips.new(action_name, capture_start_frame, rig_action)
object.animation_data.action = None
strip.extrapolation='NOTHING'
for nla_track in object.animation_data.nla_tracks:
nla_track.mute=False
#this_frame = int(local_frame_current * FRAMERATE + 1)
#bpy.context.scene.frame_current = this_frame
stream.stop_stream()
stream.close()
filename = '//' + action_name + '.wav'
wf = wave.open(bpy.path.abspath(filename), 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
frames.clear()
bpy.context.scene.sequence_editor.sequences.new_sound(action_name, filename, 1, capture_start_frame)
class ModalTimerOperator(bpy.types.Operator):
"""Operator which runs its self from a timer"""
bl_idname = "wm.modal_timer_operator"
bl_label = "Modal Timer Operator"
_timer = None
def execute(self, context):
wm = context.window_manager
#self._timer = wm.event_timer_add(1/CAPTURE_FRAMERATE, window=context.window)
self._timer = wm.event_timer_add(0.5/CAPTURE_FRAMERATE, window=context.window)
wm.modal_handler_add(self)
return {'RUNNING_MODAL'}
def modal(self, context, event):
global capture
global capture_frame
global capture_start_frame
global local_frame_current
global FRAMERATE
global object_body
global object_face
global time_previous
global co
global objects_capture_last_frame
global objects_capture_ready
global CHUNK
global FORMAT
global CHANNELS
global RATE
global action_name
global p
global stream
global frames
local_frame_current += perf_counter() - time_previous
time_previous = perf_counter()
if event.value == 'RELEASE':
if event.type == 'ESC':
self.cancel(context)
return {'CANCELLED'}
elif event.type == 'F1':
capture = not capture
if capture:
capture_start()
else:
capture_stop()
#this_frame = int(local_frame_current * FRAMERATE + 1)
#bpy.context.scene.frame_current = this_frame
if objects_capture_ready:
objects_capture_ready = False
for bone_name in bones_to_apply1_list:
bone = bones_to_send[bone_name]
bone.location = bones_to_apply1[bone_name][0]
bone.rotation_quaternion = bones_to_apply1[bone_name][1]
for bone_name in bones_to_apply3_list:
bone = bones_to_send[bone_name]
bone.value = bones_to_apply3[bone_name]
if capture:
this_frame = int(local_frame_current * FRAMERATE + 1)
#data = stream.read(CHUNK)
#frames.append(data)
data_frames = stream.get_read_available()
if data_frames:
data = stream.read(data_frames)
frames.append(data)
if this_frame > objects_capture_last_frame:
objects_capture_last_frame = this_frame
## Animation engine
delta_time = perf_counter() - time_previous
time_previous = perf_counter()
local_frame_current += delta_time
#capture_frame += 1
co[0] = local_frame_current * FRAMERATE
for object in {object_body, object_face}:
if object.animation_data and object.animation_data.action:
object_fcurves = object.animation_data.action.fcurves
for fcurve in object_fcurves:
try:
co[1] = object.path_resolve(fcurve.data_path)[fcurve.array_index]
except:
co[1] = object.path_resolve(fcurve.data_path)
capture_frame = len(fcurve.keyframe_points)
fcurve.keyframe_points.add(1)
fcurve.keyframe_points[capture_frame].co = co
fcurve.keyframe_points[capture_frame].handle_left = co
fcurve.keyframe_points[capture_frame].handle_right = co
#bpy.context.scene.frame_current = int(local_frame_current * FRAMERATE + 1)
#bpy.context.scene.frame_set(int(local_frame_current * FRAMERATE + 1))
#bpy.context.scene.frame_current = int(local_frame_current * FRAMERATE + 1)
bpy.context.scene.frame_current = this_frame
return {'PASS_THROUGH'}
def cancel(self, context):
if capture:
capture_stop()
global heva_receiver
if self._timer:
wm = context.window_manager
wm.event_timer_remove(self._timer)
heva_receiver.shutdown()
heva_receiver.server_close()
p.terminate()
ShowMessageBox("Program terminated successfully!")
def register():
bpy.utils.register_class(ModalTimerOperator)
def unregister():
bpy.utils.unregister_class(ModalTimerOperator)
if __name__ == "__main__":
if init_osc_server():
register()
bpy.ops.wm.modal_timer_operator()
ShowMessageBox("Program is running! Yay!")