forked from animate1978/MB-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacerig.py
219 lines (179 loc) · 7.1 KB
/
facerig.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
# MB-Lab
# MB-Lab fork website : https://github.com/animate1978/MB-Lab
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import logging
import json
import os
import bpy
from . import algorithms
logger = logging.getLogger(__name__)
def populate_modifier(mod, m):
mod.active = m['active']
mod.blend_in = m['blend_in']
mod.blend_out = m['blend_out']
mod.influence = m['influence']
mod.mode = m['mode']
mod.mute = m['mute']
mod.poly_order = m['poly_order']
# type should be created when the modifier is created
#mod.type = m['type']
mod.use_additive = m['use_additive']
mod.use_influence = m['use_influence']
mod.coefficients[0] = m['coefficients'][0]
mod.coefficients[1] = m['coefficients'][1]
def populate_modifiers(modifiers, mlist):
i = 0
mod = modifiers[0]
for m in mlist:
if i == 0:
populate_modifier(mod, m)
i = i + 1
else:
mod = modifiers.new(m['type'])
populate_modifier(mod, m)
def populate_variable(v, var):
face_rig = bpy.data.objects[var['targets'][0]['id_name']]
v.name = var['name']
v.type = var['type']
# we have one target by default
v.targets[0].id = face_rig
v.targets[0].transform_space = var['targets'][0]['transform_space']
v.targets[0].transform_type = var['targets'][0]['transform_type']
v.targets[0].bone_target = var['targets'][0]['bone_target']
def add_rm_drivers(drivers, add=True):
# Iterate through each driver entry and create driver
mesh = algorithms.get_active_body()
mname = mesh.name
for k, v in drivers.items():
shape_name = v['data_path'].strip('key_blocks["').strip('"].value')
idx = bpy.data.objects[mname].data.shape_keys.key_blocks.find(shape_name)
if idx == -1:
logger.critical("%s shape key not found", shape_name)
continue
check = bpy.data.objects[mname].data.shape_keys.animation_data and \
bpy.data.objects[mname].data.shape_keys.animation_data.drivers.\
find(v['data_path'])
if check and add:
logger.critical("%s shape key already has animation data", shape_name)
continue
# NOTE: The call to driver_add adds a modifier of type GENERATOR
# automatically
if add:
driver = bpy.data.objects[mname].data.shape_keys.key_blocks[idx]. \
driver_add('value')
else:
rc = bpy.data.objects[mname].data.shape_keys.key_blocks[idx].\
driver_remove('value')
if not rc:
print("failed to removed: ", shape_name, "idx=", idx)
continue
# Populate the driver
driver.hide = v['hide']
driver.lock = v['lock']
driver.mute = v['mute']
driver.select = v['select']
populate_modifiers(driver.modifiers, v['modifiers'])
driver.driver.expression = v['driver']['expression']
driver.driver.is_valid = v['driver']['is_valid']
driver.driver.type = v['driver']['type']
driver.driver.use_self = v['driver']['use_self']
variables = v['driver']['variables']
for var in variables:
v = driver.driver.variables.new()
populate_variable(v, var)
def setup_face_rig():
# check if the face rig is already imported
if bpy.data.objects.find('MBLab_skeleton_face_rig') != -1:
logger.critical("MBLab_skeleton_face_rig is already imported")
return False
data_path = algorithms.get_data_path()
# Load the face rig
if not data_path:
logger.critical(
"%s not found. Please check your Blender addons directory. Might need to reinstall ManuelBastioniLab",
data_path)
return False
face_rig_blend = os.path.join(data_path, "humanoid_library.blend")
if not os.path.exists(face_rig_blend):
logger.critical("%s not found. Might need to reinstall ManuelBastioniLab", face_rig_blend)
return False
# append the rig
file_path = face_rig_blend+"\\"+"Collection\Face_Rig"
directory = face_rig_blend+"\\"+"Collection"
try:
bpy.ops.wm.append(filepath=file_path, filename="Face_Rig", directory=directory)
except RuntimeError as e:
logger.critical("%s", str(e))
return False
# Load face rig json file
json_file = os.path.join(data_path, "face_rig", "expression_drivers.json")
if not os.path.exists(json_file):
logger.critical("%s not found. Might need to reinstall ManuelBastioniLab", json_file)
return False
with open(json_file, 'r') as f:
drivers = json.load(f)
add_rm_drivers(drivers)
return True
def recursive_collection_delete(head):
for c in head.children:
recursive_collection_delete(c)
head.hide_select = False
head.hide_render = False
head.hide_viewport = False
for obj in head.all_objects:
obj.hide_select = False
obj.select_set(True)
bpy.ops.object.delete()
bpy.data.collections.remove(head)
def delete_face_rig():
# check if the face rig is already imported
facerig = bpy.data.objects.get('MBLab_skeleton_face_rig')
if not facerig:
logger.critical("face rig is not added due to missing facerig")
return False
# check if the face rig is already imported
phoneme = bpy.data.objects.get('MBLab_skeleton_phoneme_rig')
if not phoneme:
logger.critical("face rig is not added due to phoneme")
return False
data_path = algorithms.get_data_path()
# load face rig json file
json_file = os.path.join(data_path, "face_rig", "expression_drivers.json")
if not os.path.exists(json_file):
logger.critical("%s not found. Might need to reinstall ManuelBastioniLab", json_file)
return False
with open(json_file, 'r') as f:
drivers = json.load(f)
add_rm_drivers(drivers, add=False)
# store the original selection
orig_selection = {}
for ob in bpy.context.scene.objects:
orig_selection[ob.name] = ob.select_get()
ob.select_set(False)
# delete the face rig
facerig.select_set(True)
phoneme.select_set(True)
bpy.ops.object.delete()
c = bpy.data.collections.get('Face_Rig')
if c:
recursive_collection_delete(c)
# restore the original selection
for ob in bpy.context.scene.objects:
ob.select_set(orig_selection[ob.name])
return True