* Added "Root Motion" option to enable root motion on export (vs. stationary root at world origin!)
* Removed performance debugging code
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from typing import Dict, Iterable
|
from typing import Dict, Iterable
|
||||||
|
|
||||||
from bpy.types import Action
|
from bpy.types import Action
|
||||||
|
from mathutils import Matrix
|
||||||
|
|
||||||
from .data import *
|
from .data import *
|
||||||
from ..helpers import *
|
from ..helpers import *
|
||||||
@@ -19,13 +20,7 @@ class PsaBuilderOptions(object):
|
|||||||
self.should_trim_timeline_marker_sequences = True
|
self.should_trim_timeline_marker_sequences = True
|
||||||
self.sequence_name_prefix = ''
|
self.sequence_name_prefix = ''
|
||||||
self.sequence_name_suffix = ''
|
self.sequence_name_suffix = ''
|
||||||
|
self.root_motion = False
|
||||||
|
|
||||||
class PsaBuilderPerformance:
|
|
||||||
def __init__(self):
|
|
||||||
self.frame_set_duration = datetime.timedelta()
|
|
||||||
self.key_build_duration = datetime.timedelta()
|
|
||||||
self.key_add_duration = datetime.timedelta()
|
|
||||||
|
|
||||||
|
|
||||||
class PsaBuilder(object):
|
class PsaBuilder(object):
|
||||||
@@ -53,7 +48,6 @@ class PsaBuilder(object):
|
|||||||
raise RuntimeError(f'Invalid FPS source "{options.fps_source}"')
|
raise RuntimeError(f'Invalid FPS source "{options.fps_source}"')
|
||||||
|
|
||||||
def build(self, context, options: PsaBuilderOptions) -> Psa:
|
def build(self, context, options: PsaBuilderOptions) -> Psa:
|
||||||
performance = PsaBuilderPerformance()
|
|
||||||
active_object = context.view_layer.objects.active
|
active_object = context.view_layer.objects.active
|
||||||
|
|
||||||
if active_object.type != 'ARMATURE':
|
if active_object.type != 'ARMATURE':
|
||||||
@@ -201,40 +195,38 @@ class PsaBuilder(object):
|
|||||||
frame_count = frame_max - frame_min + 1
|
frame_count = frame_max - frame_min + 1
|
||||||
|
|
||||||
for frame in range(frame_count):
|
for frame in range(frame_count):
|
||||||
with Timer() as t:
|
context.scene.frame_set(frame_min + frame)
|
||||||
context.scene.frame_set(frame_min + frame)
|
|
||||||
performance.frame_set_duration += t.duration
|
|
||||||
|
|
||||||
for pose_bone in pose_bones:
|
for pose_bone in pose_bones:
|
||||||
with Timer() as t:
|
key = Psa.Key()
|
||||||
key = Psa.Key()
|
|
||||||
|
if pose_bone.parent is not None:
|
||||||
pose_bone_matrix = pose_bone.matrix
|
pose_bone_matrix = pose_bone.matrix
|
||||||
|
pose_bone_parent_matrix = pose_bone.parent.matrix
|
||||||
|
pose_bone_matrix = pose_bone_parent_matrix.inverted() @ pose_bone_matrix
|
||||||
|
else:
|
||||||
|
if options.root_motion:
|
||||||
|
# Export root motion
|
||||||
|
pose_bone_matrix = armature.matrix_world @ pose_bone.matrix
|
||||||
|
else:
|
||||||
|
pose_bone_matrix = pose_bone.matrix
|
||||||
|
|
||||||
if pose_bone.parent is not None:
|
location = pose_bone_matrix.to_translation()
|
||||||
pose_bone_parent_matrix = pose_bone.parent.matrix
|
rotation = pose_bone_matrix.to_quaternion().normalized()
|
||||||
pose_bone_matrix = pose_bone_parent_matrix.inverted() @ pose_bone_matrix
|
|
||||||
|
|
||||||
location = pose_bone_matrix.to_translation()
|
if pose_bone.parent is not None:
|
||||||
rotation = pose_bone_matrix.to_quaternion().normalized()
|
rotation.conjugate()
|
||||||
|
|
||||||
if pose_bone.parent is not None:
|
key.location.x = location.x
|
||||||
rotation.x = -rotation.x
|
key.location.y = location.y
|
||||||
rotation.y = -rotation.y
|
key.location.z = location.z
|
||||||
rotation.z = -rotation.z
|
key.rotation.x = rotation.x
|
||||||
|
key.rotation.y = rotation.y
|
||||||
|
key.rotation.z = rotation.z
|
||||||
|
key.rotation.w = rotation.w
|
||||||
|
key.time = 1.0 / psa_sequence.fps
|
||||||
|
|
||||||
key.location.x = location.x
|
psa.keys.append(key)
|
||||||
key.location.y = location.y
|
|
||||||
key.location.z = location.z
|
|
||||||
key.rotation.x = rotation.x
|
|
||||||
key.rotation.y = rotation.y
|
|
||||||
key.rotation.z = rotation.z
|
|
||||||
key.rotation.w = rotation.w
|
|
||||||
key.time = 1.0 / psa_sequence.fps
|
|
||||||
performance.key_build_duration += t.duration
|
|
||||||
|
|
||||||
with Timer() as t:
|
|
||||||
psa.keys.append(key)
|
|
||||||
performance.key_add_duration += t.duration
|
|
||||||
|
|
||||||
psa_sequence.bone_count = len(pose_bones)
|
psa_sequence.bone_count = len(pose_bones)
|
||||||
psa_sequence.track_time = frame_count
|
psa_sequence.track_time = frame_count
|
||||||
|
|||||||
@@ -65,6 +65,12 @@ def should_use_original_sequence_names_updated(_, context):
|
|||||||
|
|
||||||
|
|
||||||
class PsaExportPropertyGroup(PropertyGroup):
|
class PsaExportPropertyGroup(PropertyGroup):
|
||||||
|
root_motion: BoolProperty(
|
||||||
|
name='Root Motion',
|
||||||
|
options=set(),
|
||||||
|
default=False,
|
||||||
|
description='When set, the root bone will be transformed as it appears in the scene',
|
||||||
|
)
|
||||||
sequence_source: EnumProperty(
|
sequence_source: EnumProperty(
|
||||||
name='Source',
|
name='Source',
|
||||||
options=set(),
|
options=set(),
|
||||||
@@ -165,6 +171,9 @@ class PsaExportOperator(Operator, ExportHelper):
|
|||||||
# SOURCE
|
# SOURCE
|
||||||
layout.prop(pg, 'sequence_source', text='Source')
|
layout.prop(pg, 'sequence_source', text='Source')
|
||||||
|
|
||||||
|
# ROOT MOTION
|
||||||
|
layout.prop(pg, 'root_motion', text='Root Motion')
|
||||||
|
|
||||||
# SELECT ALL/NONE
|
# SELECT ALL/NONE
|
||||||
row = layout.row(align=True)
|
row = layout.row(align=True)
|
||||||
row.label(text='Select')
|
row.label(text='Select')
|
||||||
@@ -297,6 +306,7 @@ class PsaExportOperator(Operator, ExportHelper):
|
|||||||
options.should_trim_timeline_marker_sequences = pg.should_trim_timeline_marker_sequences
|
options.should_trim_timeline_marker_sequences = pg.should_trim_timeline_marker_sequences
|
||||||
options.sequence_name_prefix = pg.sequence_name_prefix
|
options.sequence_name_prefix = pg.sequence_name_prefix
|
||||||
options.sequence_name_suffix = pg.sequence_name_suffix
|
options.sequence_name_suffix = pg.sequence_name_suffix
|
||||||
|
options.root_motion = pg.root_motion
|
||||||
|
|
||||||
builder = PsaBuilder()
|
builder = PsaBuilder()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user