forked from scaredyfish/blender-rhubarb-lipsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_blender_rhubarb.py
164 lines (124 loc) · 5.58 KB
/
op_blender_rhubarb.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
import bpy
from bpy.props import IntProperty, FloatProperty
import blf
import bgl
import io
import sys
import select
import subprocess
from threading import Thread
from queue import Queue, Empty
import json
import os
class RhubarbLipsyncOperator(bpy.types.Operator):
"""Run Rhubarb lipsync"""
bl_idname = "object.rhubarb_lipsync"
bl_label = "Rhubarb lipsync"
cue_prefix = 'Mouth_'
hold_frame_threshold = 4
@classmethod
def poll(cls, context):
return context.preferences.addons[__package__].preferences.executable_path and \
context.selected_pose_bones and \
context.object.pose_library.mouth_shapes.sound_file
def modal(self, context, event):
wm = context.window_manager
wm.progress_update(50)
try:
(stdout, stderr) = self.rhubarb.communicate(timeout=1)
try:
result = json.loads(stderr)
if result['type'] == 'progress':
print(result['log']['message'])
self.message = result['log']['message']
if result['type'] == 'failure':
self.report(type={'ERROR'}, message=result['reason'])
return {'CANCELLED'}
except ValueError:
pass
except TypeError:
pass
except json.decoder.JSONDecodeError:
pass
self.rhubarb.poll()
if self.rhubarb.returncode is not None:
wm.event_timer_remove(self._timer)
results = json.loads(stdout)
fps = context.scene.render.fps
lib = context.object.pose_library
last_frame = 0
prev_pose = 0
for cue in results['mouthCues']:
frame_num = round(cue['start'] * fps) + lib.mouth_shapes.start_frame
# add hold key if time since last key is large
if frame_num - last_frame > self.hold_frame_threshold:
print("hold frame: {0}".format(frame_num- self.hold_frame_threshold))
bpy.ops.poselib.apply_pose(pose_index=prev_pose)
self.set_keyframes(context, frame_num - self.hold_frame_threshold)
print("start: {0} frame: {1} value: {2}".format(cue['start'], frame_num , cue['value']))
mouth_shape = 'mouth_' + cue['value'].lower()
if mouth_shape in context.object.pose_library.mouth_shapes:
pose_index = context.object.pose_library.mouth_shapes[mouth_shape]
else:
pose_index = 0
bpy.ops.poselib.apply_pose(pose_index=pose_index)
self.set_keyframes(context, frame_num)
prev_pose = pose_index
last_frame = frame_num
wm.progress_end()
return {'FINISHED'}
return {'PASS_THROUGH'}
except subprocess.TimeoutExpired as ex:
return {'PASS_THROUGH'}
except json.decoder.JSONDecodeError:
print(stdout)
print("Error!!!")
wm.progress_end()
return {'CANCELLED'}
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
print(template.format(type(ex).__name__, ex.args))
wm.progress_end()
return {'CANCELLED'}
def set_keyframes(self, context, frame):
for bone in context.selected_pose_bones:
bone.keyframe_insert(data_path='location', frame=frame)
if bone.rotation_mode == 'QUATERNION':
bone.keyframe_insert(data_path='rotation_quaternion', frame=frame)
else:
bone.keyframe_insert(data_path='rotation_euler', frame=frame)
bone.keyframe_insert(data_path='scale', frame=frame)
def invoke(self, context, event):
preferences = context.preferences
addon_prefs = preferences.addons[__package__].preferences
inputfile = bpy.path.abspath(context.object.pose_library.mouth_shapes.sound_file)
dialogfile = bpy.path.abspath(context.object.pose_library.mouth_shapes.dialog_file)
recognizer = bpy.path.abspath(addon_prefs.recognizer)
executable = bpy.path.abspath(addon_prefs.executable_path)
# This is ugly, but Blender unpacks the zip without execute permission
os.chmod(executable, 0o744)
command = [executable, "-f", "json", "--machineReadable", "--extendedShapes", "GHX", "-r", recognizer, inputfile]
if dialogfile:
command.append("--dialogFile")
command.append(dialogfile )
self.rhubarb = subprocess.Popen(command,
stdout=subprocess.PIPE, universal_newlines=True)
wm = context.window_manager
self._timer = wm.event_timer_add(2, window=context.window)
wm.modal_handler_add(self)
wm.progress_begin(0, 100)
return {'RUNNING_MODAL'}
def execute(self, context):
return self.invoke(context, None)
def finished(self, context):
wm = context.window_manager
wm.event_timer_remove(self._timer)
def cancel(self, context):
wm = context.window_manager
wm.event_timer_remove(self._timer)
def register():
bpy.utils.register_class(RhubarbLipsyncOperator)
def unregister():
bpy.utils.unregister_class(RhubarbLipsyncOperator)
if __name__ == "__main__":
register()