123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- import math
- from mathutils import Matrix
- def get_zero_mat3x3():
-
- return Matrix(([0, 0, 0], [0, 0, 0], [0, 0, 0]))
-
- def get_zero_mat4x4():
-
- return Matrix(([0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]))
- def get_id_mat3x3():
-
- return Matrix(([1, 0, 0], [0, 1, 0], [0, 0, 1]))
-
- def get_id_mat4x4():
-
- return Matrix(([1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]))
- def calc_scale_matrix(x_scale, y_scale, z_scale):
- scale_matrix = Matrix(( [x_scale, 0, 0, 0],
- [0, y_scale, 0, 0],
- [0, 0, z_scale, 0],
- [0, 0, 0, 1]
- ))
-
- return scale_matrix
- def calc_rotation_matrix(x_angle, y_angle, z_angle):
- x_rot_mat = Matrix(([1, 0, 0, 0],
- [0, math.cos(x_angle), -math.sin(x_angle), 0],
- [0, math.sin(x_angle), math.cos(x_angle), 0],
- [0, 0, 0, 1]))
- y_rot_mat = Matrix(([math.cos(y_angle), 0, math.sin(y_angle), 0],
- [0, 1, 0, 0],
- [-math.sin(y_angle), 0, math.cos(y_angle), 0],
- [0, 0, 0, 1]))
-
- z_rot_mat = Matrix(([math.cos(z_angle), -math.sin(z_angle), 0, 0],
- [math.sin(z_angle), math.cos(z_angle), 0, 0],
- [0, 0, 1, 0],
- [0, 0, 0, 1]))
-
- return z_rot_mat * y_rot_mat * x_rot_mat
- def calc_translation_matrix(x_translation, y_translation, z_translation):
- translation_matrix = Matrix(( [1, 0, 0, x_translation],
- [0, 1, 0, y_translation],
- [0, 0, 1, z_translation],
- [0, 0, 0, 1]
- ))
-
- return translation_matrix
- def interpolate(l_x, l_y, r_x, r_y, m_x, m_y, interp_type):
-
- result = 0
-
-
-
- if (r_x == None or r_y == None):
- result = l_y
- return result
-
-
- if (m_x == None):
-
-
- if (interp_type == "linear"):
- m_x = (((r_x - l_x) / (r_y - l_y)) * (m_y - r_y)) + r_x
- result = m_x
-
-
- if (m_y == None):
-
-
- if (interp_type == "linear"):
- m_y = (((r_y - l_y) / (r_x - l_x)) * (m_x - r_x)) + r_y
- result = m_y
-
- return result
- def find_left_right(anim_array, pos):
-
- l_val = 0
- l_val_pos = 0
- r_val = 0
- r_val_pos = 0
-
-
-
- for i in range(len(anim_array), -1, -1):
-
-
-
- if (i >= pos):
- continue
-
-
- if (anim_array[i] != None):
- l_val = anim_array[i]
- l_val_pos = i
- break
-
-
-
-
-
-
- if (pos == (len(anim_array) - 1)):
- return [l_val_pos, l_val, None, None]
-
-
-
- for i in range(len(anim_array)):
-
-
- if (i <= pos):
- continue
-
-
- if (anim_array[i] != None):
- r_val = anim_array[i]
- r_val_pos = i
- break
-
-
-
-
-
- if (i == (len(anim_array) - 1)):
- return [l_val_pos, l_val, None, None]
-
-
- return [l_val_pos, l_val, r_val_pos, r_val]
- def convert_angle_to_180(angle):
-
-
- if (angle >= -180 and angle <= 180):
- return angle
-
-
-
-
-
-
-
- if (angle > 0):
- opposite_spin_dir = -1
- else:
- opposite_spin_dir = 1
-
-
-
- while (abs(angle) > 180):
- angle = angle + (opposite_spin_dir * 360)
-
- return angle
- def convert_rot_anim_to_180(rot_array, csv_keyframe_numbers):
-
-
- rot_array_cp = [[], []]
-
-
- rot_array_kf = []
- for i in range(len(rot_array)):
- if (rot_array[i] != None):
- rot_array_kf.append(i)
-
-
- for i in range(len(rot_array_kf) - 1):
-
-
- l_kf_pos = rot_array_kf[i]
- l_kf_val = rot_array[l_kf_pos]
- r_kf_pos = rot_array_kf[i + 1]
- r_kf_val = rot_array[r_kf_pos]
-
-
- rot_array_cp[0].append(l_kf_pos)
- rot_array_cp[1].append(convert_angle_to_180(l_kf_val))
-
-
- if (r_kf_val > l_kf_val):
- rot_direction = 1
- else:
- rot_direction = -1
-
-
-
-
- angle_val = l_kf_val - convert_angle_to_180(l_kf_val)
- while (abs(r_kf_val - angle_val) > 180):
-
- angle_val = angle_val + (rot_direction * 180)
-
-
- angle_pos = interpolate(l_kf_pos, l_kf_val, r_kf_pos, r_kf_val, None, angle_val, "linear")
-
-
-
- lower_frame = int(angle_pos)
- upper_frame = int(angle_pos + 1)
-
-
- lower_frame_value = convert_angle_to_180(interpolate(l_kf_pos, l_kf_val, r_kf_pos, r_kf_val, lower_frame, None, "linear"))
- upper_frame_value = convert_angle_to_180(interpolate(l_kf_pos, l_kf_val, r_kf_pos, r_kf_val, upper_frame, None, "linear"))
-
-
-
-
- rot_array_cp[0].append(lower_frame)
- rot_array_cp[0].append(upper_frame)
-
- rot_array_cp[1].append(lower_frame_value)
- rot_array_cp[1].append(upper_frame_value)
-
-
-
- for i in range(len(rot_array_cp[0])):
- rot_array[rot_array_cp[0][i]] = rot_array_cp[1][i]
-
-
-
-
- for i in range(len(rot_array_cp[0])):
- value_found = False
- for j in range(len(csv_keyframe_numbers)):
- if (rot_array_cp[0][i] == csv_keyframe_numbers[j]):
- value_found = True
- break
- if (value_found == False):
- csv_keyframe_numbers.append(rot_array_cp[0][i])
|