2022-01-18 20:16:49 +00:00
|
|
|
""" Converts currently active Action to an msh Animation """
|
2020-10-11 16:11:15 +00:00
|
|
|
|
|
|
|
import bpy
|
|
|
|
import math
|
|
|
|
from enum import Enum
|
|
|
|
from typing import List, Set, Dict, Tuple
|
|
|
|
from itertools import zip_longest
|
|
|
|
from .msh_model import *
|
|
|
|
from .msh_model_utilities import *
|
|
|
|
from .msh_utilities import *
|
|
|
|
from .msh_model_gather import *
|
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
from .msh_skeleton_utilities import *
|
|
|
|
|
2021-01-07 06:25:09 +00:00
|
|
|
from .crc import to_crc
|
2020-12-06 09:11:12 +00:00
|
|
|
|
2020-10-11 16:11:15 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
'''
|
|
|
|
Convert the active Action into an Animation. When exported SWBF anims, there is the issue
|
|
|
|
that all bones in the anim must be in the skeleton/basepose anim. We guarantee this by
|
|
|
|
only keying bones if they are in the armature's preserved skeleton (swbf_msh_skel) and
|
|
|
|
adding dummy frames if the bones are not in the armature.
|
2022-01-13 20:52:30 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
If a preserved skeleton is not present, we include only the keyed bones and add dummy frames for
|
|
|
|
the root (root_name)
|
|
|
|
'''
|
2022-01-13 20:52:30 +00:00
|
|
|
|
2020-11-30 01:10:16 +00:00
|
|
|
def extract_anim(armature: bpy.types.Armature, root_name: str) -> Animation:
|
2020-10-11 16:11:15 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
if not armature.animation_data or not armature.animation_data.action:
|
|
|
|
raise RuntimeError("Cannot export animation data without an active Action on armature!")
|
|
|
|
|
2020-10-11 16:11:15 +00:00
|
|
|
action = armature.animation_data.action
|
2022-01-13 20:52:30 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
|
2022-01-13 20:52:30 +00:00
|
|
|
# Set of bones to include in SKL2/animation stuff
|
2022-01-18 20:16:49 +00:00
|
|
|
keyable_bones = get_real_BONES(armature)
|
2022-01-13 20:52:30 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
# If it doesn't have a preserved skeleton, then we add the scene root.
|
|
|
|
# If it does have a preserved skeleton, any objects not animatable by blender (i.e. objects above the skeleton, scene root)
|
|
|
|
# will be included in the preserved skeleton
|
|
|
|
if not has_preserved_skeleton(armature):
|
|
|
|
keyable_bones.add(root_name)
|
2022-01-13 20:52:30 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
# Subset of above bones to key with dummy frames (all bones not in armature)
|
|
|
|
dummy_bones = set([keyable_bone for keyable_bone in keyable_bones if keyable_bone not in armature.data.bones])
|
2022-01-13 20:52:30 +00:00
|
|
|
|
|
|
|
|
2020-10-20 03:18:08 +00:00
|
|
|
anim = Animation();
|
|
|
|
|
2021-01-07 06:25:09 +00:00
|
|
|
root_crc = to_crc(root_name)
|
2020-12-05 05:53:59 +00:00
|
|
|
|
2020-10-20 03:18:08 +00:00
|
|
|
if not action:
|
|
|
|
framerange = Vector((0.0,1.0))
|
|
|
|
else:
|
|
|
|
framerange = action.frame_range
|
2020-10-11 16:11:15 +00:00
|
|
|
|
2020-10-20 03:18:08 +00:00
|
|
|
num_frames = math.floor(framerange.y - framerange.x) + 1
|
|
|
|
increment = (framerange.y - framerange.x) / (num_frames - 1)
|
2020-10-11 16:11:15 +00:00
|
|
|
|
2020-10-20 03:18:08 +00:00
|
|
|
anim.end_index = num_frames - 1
|
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
|
|
|
|
for keyable_bone in keyable_bones:
|
|
|
|
anim.bone_frames[to_crc(keyable_bone)] = ([], [])
|
|
|
|
|
2020-10-11 16:11:15 +00:00
|
|
|
|
2020-10-20 03:18:08 +00:00
|
|
|
for frame in range(num_frames):
|
2020-11-29 20:45:32 +00:00
|
|
|
|
2020-10-20 03:18:08 +00:00
|
|
|
frame_time = framerange.x + frame * increment
|
2020-10-11 16:11:15 +00:00
|
|
|
bpy.context.scene.frame_set(frame_time)
|
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
for keyable_bone in keyable_bones:
|
2020-11-29 20:45:32 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
bone_crc = to_crc(keyable_bone)
|
2020-11-29 20:45:32 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
if keyable_bone in dummy_bones:
|
2020-11-29 20:45:32 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
rframe = RotationFrame(frame, convert_rotation_space(Quaternion()))
|
|
|
|
tframe = TranslationFrame(frame, Vector((0.0,0.0,0.0)))
|
2020-11-29 20:45:32 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
else:
|
2020-10-12 00:52:34 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
bone = armature.pose.bones[keyable_bone]
|
2022-01-13 20:52:30 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
transform = bone.matrix
|
2020-10-11 16:11:15 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
if bone.parent:
|
|
|
|
transform = bone.parent.matrix.inverted() @ transform
|
|
|
|
|
|
|
|
loc, rot, _ = transform.decompose()
|
2020-10-16 18:56:03 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
rframe = RotationFrame(frame, convert_rotation_space(rot))
|
|
|
|
tframe = TranslationFrame(frame, convert_vector_space(loc))
|
2020-10-11 16:11:15 +00:00
|
|
|
|
2022-01-18 20:16:49 +00:00
|
|
|
anim.bone_frames[bone_crc][0].append(tframe)
|
|
|
|
anim.bone_frames[bone_crc][1].append(rframe)
|
2020-11-29 23:32:17 +00:00
|
|
|
|
2020-10-11 16:11:15 +00:00
|
|
|
|
2020-10-20 03:18:08 +00:00
|
|
|
return anim
|