util.py
11.8 KB
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
# -*- coding: UTF-8 -*-
import bpy
import math
import os
import sys
import copy
import pyutil
from bmesh_collision import bmesh_check_intersect_objects
armature = bpy.context.scene.objects.get('Armature.001')
dict_bones = {
"01_conf_direita": ['BnDedo.1.R', 'BnDedo.1.R.006', 'BnDedo.1.R.005', 'BnDedo.1.R.001', 'BnDedo.1.R.008', 'BnDedo.1.R.007', 'BnDedo.1.R.002', 'BnDedo.1.R.010', 'BnDedo.1.R.009', 'BnDedo.1.R.003', 'BnDedo.1.R.012', 'BnDedo.1.R.011', 'BnDedo.1.R.004', 'BnDedo.1.R.014', 'BnDedo.1.R.013'],
"02_conf_esquerda": ['BnDedo.1.L', 'BnDedo.1.L.006', 'BnDedo.1.L.005', 'BnDedo.1.L.001', 'BnDedo.1.L.008', 'BnDedo.1.L.007', 'BnDedo.1.L.002', 'BnDedo.1.L.010', 'BnDedo.1.L.009', 'BnDedo.1.L.003', 'BnDedo.1.L.012', 'BnDedo.1.L.011', 'BnDedo.1.L.004', 'BnDedo.1.L.014', 'BnDedo.1.L.013'],
"03_pa_direita": ['ik_FK.R', 'BnPolyV.R'],
"04_pa_esquerda": ['ik_FK.L', 'BnPolyV.L'],
"05_orient_direita": ['BnMao.R'],
"06_orient_esquerda": ['BnMao.L'],
"07_facial": ['BnPescoco', 'BnCabeca', 'BnSobrancCentro.L', 'BnSobrancCentro.R', 'BnSobrancLateral.L', 'BnSobrancLateral.R', 'BnPalpebSuper.L', 'BnPalpebInfe.L', 'BnSobrancCentro', 'BnLabioCentroSuper', 'BnBochecha.L', 'BnBochecha.R', 'BnLabioCentroInfer', 'BnBocaCanto.L', 'BnBocaCanto.R', 'BnMandibula', 'BnLingua', 'BnLingua.003', 'BnLingua.001', 'BnLingua.002', 'BnPalpebSuper.R', 'BnPalpebInfe.R', 'BnOlhosMira', 'BnOlhoMira.L', 'BnOlhoMira.R', 'BnOlho.L', 'BnOlho.R']
}
right_bones_conf = dict_bones["01_conf_direita"] + dict_bones["03_pa_direita"] + dict_bones["05_orient_direita"]
# Vetor com indices de cada bone do lado direito
rightBonesConf = [1, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66]
# Vetor com indices de cada bone do lado esquerdo
leftBonesConf = [0, 43, 44, 45, 46, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82]
# Movimento coçar - Índices de poses
cocar_mao_aberta_index = 55
cocar_mao_fechada_index = 24
cocar_orientation_index = 11
# Action expressão facial
facial_expression_id = '07_facial'
facial_expression_action = [facial_expression_id]
# Actions mão direita
conf_direita_id = '01_conf_direita'
pa_direita_id = '03_pa_direita'
orient_direita_id = '05_orient_direita'
right_hand_actions = [conf_direita_id, pa_direita_id, orient_direita_id]
# Actions mão esquerda
conf_esquerda_id = '02_conf_esquerda'
pa_esquerda_id = '04_pa_esquerda'
orient_esquerda_id = '06_orient_esquerda'
left_hand_actions = [conf_esquerda_id, pa_esquerda_id, orient_esquerda_id]
last_keyframe_dict = {}
def select_bones(bones = bpy.context.object.pose.bones):
if (isinstance(bones, int) or isinstance(bones, str)):
bpy.context.object.pose.bones[bones].bone.select = True
elif (isinstance(bones, list)):
for bone in bones:
bpy.context.object.pose.bones[bone].bone.select = True
elif (isinstance(bones, type(bpy.context.object.pose.bones))):
for pose_bone in bones:
pose_bone.bone.select = True
return
def deselect_bones(bones = bpy.context.object.pose.bones):
if (isinstance(bones, int) or isinstance(bones, str)):
bpy.context.object.pose.bones[bones].bone.select = False
elif (isinstance(bones, list)):
for bone in bones:
bpy.context.object.pose.bones[bone].bone.select = False
elif (isinstance(bones, type(bpy.context.object.pose.bones))):
for pose_bone in bones:
pose_bone.bone.select = False
return
def set_pose_mode():
bpy.ops.object.mode_set(mode = 'OBJECT')
bpy.ops.object.select_all(action = "DESELECT")
bpy.ops.object.mode_set(mode = 'POSE')
def delete_all_keyframes():
bpy.context.active_object.animation_data_clear()
for obj in bpy.data.objects:
obj.animation_data_clear()
return
# Função responsável por selecionar as pose-libs e setar os frames
def setPose(actions, parametesConf, positionFrames, bones, collisionFlag = True):
for x in range(len(positionFrames)):
for l in range(len(actions)):
action = actions[l]
select_bones(bones)
armature.pose_library = bpy.data.actions[action]
bpy.ops.poselib.apply_pose(pose_index = parametesConf[l])
deselect_bones(bones)
bpy.context.object.pose_library = None
for i in range(0, (len(bones))):
bone = armature.pose.bones[bones[i]]
validHandConf = action in [conf_direita_id, conf_esquerda_id] and "BnDedo" in bone.name
validPA = action in [pa_direita_id, pa_esquerda_id] and ("ik_FK" in bone.name or "BnPolyV" in bone.name)
validO = action in [orient_direita_id, orient_esquerda_id] and "BnMao" in bone.name
if (validHandConf or validPA or validO):
keyframe_insert(bone, 'location', positionFrames[x], collisionFlag and validPA, validO)
keyframe_insert(bone, 'rotation_quaternion', positionFrames[x], collisionFlag and validPA, validO)
def internal_keyframe_insert(bone, path, positionFrame, collisionFlag = True, rotationFlag = False):
# Limit hand depth
if ("ik_FK.R" == bone.name):
print("VERDADE", bone.location, positionFrame)
if ("ik_FK" in bone.name and bone.location.z < 1):
bone.location.z = 1
bone.bone.select = True
bone.keyframe_insert(data_path = path, index = -1, frame = positionFrame)
bone.bone.select = False
keyframe_id = bone.name + "_" + path
last_keyframe = last_keyframe_dict[keyframe_id] if keyframe_id in last_keyframe_dict else 0
last_keyframe_dict[keyframe_id] = positionFrame
if (rotationFlag and path == "rotation_quaternion"):
checkRotation(bone, positionFrame, last_keyframe)
if (collisionFlag):
checkCollision(bone, path, positionFrame, last_keyframe)
def keyframe_insert(bones, path, positionFrame, collisionFlag = True, rotationFlag = False):
if (isinstance(bones, list)):
for bone in bones:
if (isinstance(bone, int) or isinstance(bone, str)):
internal_keyframe_insert(bpy.context.object.pose.bones[bone], path, positionFrame, collisionFlag, rotationFlag)
else:
internal_keyframe_insert(bone, path, positionFrame, collisionFlag, rotationFlag)
else:
internal_keyframe_insert(bones, path, positionFrame, collisionFlag, rotationFlag)
def resetIKPosition(isRightHand):
armature.pose_library = bpy.data.actions[pa_direita_id if isRightHand else pa_esquerda_id]
bpy.ops.poselib.apply_pose(pose_index = 0)
def resetBnMaoPosition(isRightHand):
armature.pose_library = bpy.data.actions[orient_direita_id if isRightHand else orient_esquerda_id]
bpy.ops.poselib.apply_pose(pose_index = 0)
def get_bone_data_from_frame(bone, positionFrame, path):
scene = bpy.context.scene
frame_current = scene.frame_current
scene.frame_set(positionFrame)
result = copy.copy(bone.location) if path == 'location' else copy.copy(bone.rotation_quaternion)
scene.frame_set(frame_current)
return result
def checkRotation(bone, positionFrame, last_keyframe):
boneRQ = get_bone_data_from_frame(bone, positionFrame, 'rotation_quaternion')
isRightHand = ".R" in bone.name
resetBnMaoPosition(isRightHand)
valid_rotation = validate_rotation(bone, positionFrame, last_keyframe)
if (not valid_rotation):
new_rotation = boneRQ * (-1)
bone.rotation_quaternion = new_rotation
bone.keyframe_insert(data_path = 'rotation_quaternion', index = -1, frame = positionFrame)
def checkCollision(bone, path, positionFrame, last_keyframe):
if (last_keyframe == positionFrame):
return
isRightHand = ".R" in bone.name
resetIKPosition(isRightHand)
handCollisionFrame = check_hand_collision(last_keyframe, positionFrame)
if (handCollisionFrame != -1):
handle_collision(bone, path, positionFrame, handCollisionFrame)
return
bodyCollisionFrame = check_body_collision(isRightHand, last_keyframe, positionFrame)
if (bodyCollisionFrame != -1):
handle_collision(bone, path, positionFrame, bodyCollisionFrame)
return
def handle_collision(bone, path, positionFrame, collisionFrame, rollbackFrames = 0):
scene = bpy.context.scene
frame_current = scene.frame_current
scene.frame_set(collisionFrame - rollbackFrames)
bone.keyframe_insert(data_path = path, index = -1, frame = positionFrame)
bpy.context.scene.frame_set(frame_current)
def check_hand_collision(initFrame, endFrame):
return check_collision('right_hand_box', 'left_hand_box', initFrame, endFrame)
def check_collision(objName, otherObjName, initFrame, endFrame):
scene = bpy.context.scene
frame_current = scene.frame_current
startFrame = initFrame + int(math.fabs((endFrame - initFrame)/2))
collisionFrame = -1
for i in range(startFrame, endFrame + 1, 1):
scene.frame_set(i)
obj = scene.objects.get(objName)
otherObj = scene.objects.get(otherObjName)
if (bmesh_check_intersect_objects(obj, otherObj)):
pyutil.log("bla")
pyutil.log(i)
collisionFrame = i
break
scene.frame_set(frame_current)
return collisionFrame
def check_body_collision(isRightHand, initFrame, endFrame):
hand_box = 'right_hand_box' if isRightHand else 'left_hand_box'
body_box = 'body_box'
result = check_collision(hand_box, body_box, initFrame, endFrame)
return result
# Função que define as configurações de saida
def configure_output():
bpy.context.scene.frame_start = 0
bpy.context.scene.frame_current = bpy.context.scene.frame_start
bpy.context.scene.frame_end = bpy.context.scene.frame_start
bpy.context.scene.render.resolution_x = 640
bpy.context.scene.render.resolution_y = 480
bpy.context.scene.render.resolution_percentage = 100
bpy.context.scene.render.image_settings.file_format = 'H264'
bpy.context.scene.render.ffmpeg.format = 'MPEG4'
bpy.context.scene.render.ffmpeg.codec = 'H264'
# Otimização da renderização
bpy.context.scene.render.use_shadows = False
bpy.context.scene.render.use_raytrace = False
bpy.context.scene.render.use_envmaps = False
bpy.context.scene.render.use_motion_blur = False
bpy.context.scene.render.use_shadows = False
bpy.context.scene.render.tile_x = 320
bpy.context.scene.render.tile_y = 240
return
def render_sign(user_id, nome_sinal = "sinal", frame_final = bpy.context.scene.frame_end):
getcwd = os.path.dirname(os.path.abspath(__file__))
bpy.context.scene.render.filepath = getcwd + "/users/" + str(user_id) + "/"+ nome_sinal + "_"
bpy.context.scene.frame_end = frame_final
pyutil.log("Gerando Video... Frames: %i" % (frame_final))
bpy.ops.render.render(animation = True, write_still = False, layer = "", scene = "")
pyutil.file_rename("%s%0.4i-%0.4i.mp4" % (bpy.context.scene.render.filepath, bpy.context.scene.frame_start, bpy.context.scene.frame_end))
bpy.ops.wm.quit_blender()
return
def validate_rotation(bone, endFrame, startFrame = 0):
if (endFrame - startFrame == 1):
return True
rotFrames = [[]]
scene = bpy.context.scene
frame_current = scene.frame_current
for i in range(startFrame+1, endFrame+1, 1):
scene.frame_set(i)
rotFrames[-1] = bone.rotation_quaternion.to_euler()
rotFrames.append([])
rotFrames.remove([])
scene.frame_set(frame_current)
for k in range(1, len(rotFrames), 1):
for i in range(0, 3, 1):
if (math.fabs(rotFrames[k][i] - rotFrames[k-1][i])) > math.pi/3:
return False
return True
# Axis: "X", "Y" e "Z"
def apply_rotation(bone, axis, currentFrame, degree):
new_rotation = bone.rotation_quaternion.to_euler()
new_rotation.rotate_axis(axis, math.radians(degree))
new_rotation = new_rotation.to_quaternion()
bone.rotation_quaternion = new_rotation
keyframe_insert(bone, 'rotation_quaternion', currentFrame, False, True)