forked from animate1978/MB-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skeletonengine.py
243 lines (205 loc) · 10.1 KB
/
skeletonengine.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
# 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 #####
#
# ManuelbastioniLAB - Copyright (C) 2015-2018 Manuel Bastioni
import logging
import os
import bpy
import mathutils
from . import algorithms, utils, file_ops, object_ops
from .utils import get_object_parent
logger = logging.getLogger(__name__)
# ------------------------------------------------------------------------
# Skeleton Engine
# ------------------------------------------------------------------------
class SkeletonEngine:
armature_modifier_name = "mbastlab_armature"
def __init__(self, obj_body, character_config, rigging_type):
self.has_data = False
self.data_path = file_ops.get_data_path()
#characters_config = file_ops.get_configuration()
#character_config = characters_config[character_identifier]
if obj_body:
self.body_name = obj_body.name
self.joints_filename = character_config["joints_base_file"]
self.joints_offset_filename = character_config["joints_offset_file"]
if rigging_type == "base":
self.skeleton_template_name = "MBLab_skeleton_base_fk"
self.groups_filename = character_config["vertexgroup_base_file"]
elif rigging_type == "ik":
self.skeleton_template_name = "MBLab_skeleton_base_ik"
self.groups_filename = character_config["vertexgroup_base_file"]
elif rigging_type == "muscle":
self.skeleton_template_name = "MBLab_skeleton_muscle_fk"
self.groups_filename = character_config["vertexgroup_muscle_file"]
elif rigging_type == "muscle_ik":
self.skeleton_template_name = "MBLab_skeleton_muscle_ik"
self.groups_filename = character_config["vertexgroup_muscle_file"]
skeleton_name = character_config["name"]+"_skeleton"
joints_data_path = os.path.join(self.data_path, "joints", self.joints_filename)
joints_offset_data_path = os.path.join(self.data_path, "joints", self.joints_offset_filename)
vgroup_data_path = os.path.join(self.data_path, "vgroups", self.groups_filename)
self.lib_filepath = file_ops.get_blendlibrary_path()
self.joints_database = file_ops.load_json_data(joints_data_path, "Joints data")
self.joints_offset_database = file_ops.load_json_data(joints_offset_data_path, "Joints offset data")
if self.check_skeleton(obj_body):
obj_armat = get_object_parent(obj_body)
else:
obj_armat = file_ops.import_object_from_lib(
self.lib_filepath, self.skeleton_template_name, skeleton_name)
if obj_armat is not None:
self.store_z_axis()
# TODO doesn't look like armature_visibility is used
# anywhere
#self.armature_visibility = [x for x in obj_armat.layers]
self.armature_name = obj_armat.name
self.align_bones_z_axis()
obj_body.parent = obj_armat
self.has_data = True
self.load_groups(vgroup_data_path)
self.add_armature_modifier()
@staticmethod
def check_skeleton(obj_body):
obj_parent = get_object_parent(obj_body)
return obj_parent and obj_parent.type == 'ARMATURE'
def add_armature_modifier(self):
if self.has_data:
obj = self.get_body()
armat = self.get_armature()
parameters = {"object": armat}
object_ops.new_modifier(obj, self.armature_modifier_name, 'ARMATURE', parameters)
def move_up_armature_modifier(self):
if self.has_data:
obj = self.get_body()
armature_modifier = algorithms.get_modifier(obj, self.armature_modifier_name)
if armature_modifier:
algorithms.move_up_modifier(obj, armature_modifier)
def apply_armature_modifier(self):
if self.has_data:
obj = self.get_body()
armature_modifier = algorithms.get_modifier(obj, self.armature_modifier_name)
if armature_modifier:
algorithms.apply_modifier(obj, armature_modifier)
def apply_pose_as_rest_pose(self):
if self.has_data:
armat = self.get_armature()
obj = self.get_body()
algorithms.select_and_change_mode(armat, 'POSE')
bpy.ops.pose.armature_apply()
algorithms.apply_auto_align_bones(armat)
algorithms.update_bendy_bones(armat)
algorithms.select_and_change_mode(obj, 'OBJECT')
@staticmethod
def error_msg(path):
logger.error("Database file not found: %s", file_ops.simple_path(path))
def store_z_axis(self):
logger.info("Importing temporary original skeleton to store z axis")
native_armature = file_ops.import_object_from_lib(
self.lib_filepath, self.skeleton_template_name, "temp_armature")
if native_armature:
self.armature_z_axis = algorithms.get_all_bones_z_axis(native_armature)
object_ops.remove_object(native_armature)
def align_bones_z_axis(self):
target_armature = self.get_armature()
if target_armature:
algorithms.select_and_change_mode(target_armature, 'EDIT')
edit_bones = algorithms.get_edit_bones(target_armature)
for e_bone in edit_bones:
if e_bone.name in self.armature_z_axis:
z_axis = self.armature_z_axis[e_bone.name]
e_bone.align_roll(z_axis)
algorithms.select_and_change_mode(target_armature, 'POSE')
def load_groups(self, filepath, use_weights=True, clear_all=True):
if self.has_data:
obj = self.get_body()
g_data = file_ops.load_json_data(filepath, "Vertgroups data")
if clear_all:
algorithms.remove_vertgroups_all(obj)
if g_data:
group_names = sorted(g_data.keys())
for group_name in group_names:
new_group = algorithms.new_vertgroup(obj, group_name)
for vert_data in g_data[group_name]:
if use_weights:
if isinstance(vert_data, list):
new_group.add([vert_data[0]], vert_data[1], 'REPLACE')
else:
logger.info("Error: wrong format for vert weight")
else:
if isinstance(vert_data, int):
new_group.add([vert_data], 1.0, 'REPLACE')
else:
logger.info("Error: wrong format for vert group")
logger.info("Group loaded from %s", file_ops.simple_path(filepath))
else:
logger.warning("Vgroup file problem %s", file_ops.simple_path(filepath))
def get_body(self):
if self.has_data:
return file_ops.get_object_by_name(self.body_name)
return None
def get_armature(self):
if self.has_data:
return file_ops.get_object_by_name(self.armature_name)
return None
def __bool__(self):
armat = self.get_armature()
body = self.get_body()
return body and armat
@staticmethod
def calculate_joint_location(obj, vertsindex_list):
joint_verts_coords = []
for v_idx in vertsindex_list:
vert = obj.data.vertices[v_idx]
joint_verts_coords.append(vert.co)
return algorithms.average_center(joint_verts_coords)
def fit_joints(self):
armat = self.get_armature()
body = self.get_body()
if armat and body:
algorithms.set_object_visible(armat)
logger.debug("Fitting armature %s", armat.name)
armat.data.use_mirror_x = False
current_active_obj = algorithms.get_active_object()
algorithms.select_and_change_mode(armat, "EDIT")
edit_bones = algorithms.get_edit_bones(armat)
for e_bone in edit_bones:
tail_name = "".join((e_bone.name, "_tail"))
head_name = "".join((e_bone.name, "_head"))
if tail_name in self.joints_database:
tail_location = self.calculate_joint_location(body, self.joints_database[tail_name])
if self.joints_offset_database:
if tail_name in self.joints_offset_database:
tail_delta = mathutils.Vector(self.joints_offset_database[tail_name])
tail_location += tail_delta
e_bone.tail = tail_location
if head_name in self.joints_database:
head_location = self.calculate_joint_location(body, self.joints_database[head_name])
if self.joints_offset_database:
if head_name in self.joints_offset_database:
head_delta = mathutils.Vector(self.joints_offset_database[head_name])
head_location += head_delta
e_bone.head = head_location
algorithms.select_and_change_mode(armat, "OBJECT")
self.align_bones_z_axis()
algorithms.apply_auto_align_bones(armat)
algorithms.update_bendy_bones(armat)
algorithms.set_active_object(current_active_obj)