Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

small refactor with cleaning animation tracks

Scott Petrovicc30246e9f40a
diff --git a/src/lib/processes/animations-listing/AnimationLoader.ts b/src/lib/processes/animations-listing/AnimationLoader.ts
index 9797225..8db1c5f 100644
--- a/src/lib/processes/animations-listing/AnimationLoader.ts
+++ b/src/lib/processes/animations-listing/AnimationLoader.ts
@@ -242,7 +242,7 @@ export class AnimationLoader extends EventTarget {
RigConfig.by_skeleton_type(this.skeleton_type)?.position_tracking_bone_name
// Clean track data (remove position tracks except for specific cases)
- AnimationUtility.clean_track_data(cloned_animations, this.skeleton_type, position_tracking_bone_name)
+ AnimationUtility.clean_track_data(cloned_animations, position_tracking_bone_name)
// Apply skeleton scaling to position keyframes
AnimationUtility.apply_skeleton_scale_to_position_keyframes(cloned_animations, skeleton_scale)
diff --git a/src/lib/processes/animations-listing/AnimationUtility.ts b/src/lib/processes/animations-listing/AnimationUtility.ts
index 4320331..406d430 100644
--- a/src/lib/processes/animations-listing/AnimationUtility.ts
+++ b/src/lib/processes/animations-listing/AnimationUtility.ts
@@ -1,7 +1,6 @@
import { AnimationClip, Quaternion, Vector3, type KeyframeTrack, type QuaternionKeyframeTrack } from 'three'
import type { TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair'
import { RigConfig } from '../../RigConfig'
-import { SkeletonType } from '../../enums/SkeletonType'
export class AnimationUtility {
// when we scaled the skeleton itself near the beginning, we kept track of that
@@ -51,31 +50,26 @@ export class AnimationUtility {
/// Removes position tracks from animation clips, keeping only rotation tracks.
/// @param animation_clips - The animation clips to modify.
/// @param preserve_root_position - Whether to keep the root position track.
- static clean_track_data (animation_clips: AnimationClip[], skeleton_type: SkeletonType,
- position_tracking_bone_name: string | undefined ): void {
+ static clean_track_data (animation_clips: AnimationClip[], position_tracking_bone_name: string | undefined): void {
animation_clips.forEach((animation_clip: AnimationClip) => {
// does the animation clip name include "RM". This indicates a root motion clip and we can keep the root position track
const preserve_root_position: boolean = animation_clip.name.toLowerCase().endsWith('rm')
- // remove all position nodes except root
- let rotation_tracks: KeyframeTrack[]
- if (preserve_root_position) {
- rotation_tracks = animation_clip.tracks
- .filter((x: KeyframeTrack) => x.name.includes('quaternion') ||
- x.name.toLowerCase().includes(position_tracking_bone_name + '.position') ||
- x.name.toLowerCase().includes('root.position'))
- } else {
- rotation_tracks = animation_clip.tracks
- .filter((x: KeyframeTrack) => x.name.includes('quaternion') ||
- x.name.toLowerCase().includes(position_tracking_bone_name + '.position'))
- }
-
- animation_clip.tracks = rotation_tracks // update track data
+ // remove all position nodes except root and the position tracking bone (e.g. pelvis for human)
+ animation_clip.tracks = animation_clip.tracks.filter((track: KeyframeTrack) =>
+ AnimationUtility.is_rotation_or_position_tracking_track(track, position_tracking_bone_name) ||
+ (preserve_root_position && track.name.toLowerCase().includes('root.position')))
})
}
+ private static is_rotation_or_position_tracking_track (track: KeyframeTrack,
+ position_tracking_bone_name: string | undefined): boolean {
+ return track.name.includes('quaternion') ||
+ track.name.toLowerCase().includes(position_tracking_bone_name + '.position')
+ }
+
static apply_arm_extension_warp (animation_clips: TransformedAnimationClipPair[], percentage: number): void {
// loop through each animation clip to update the tracks
animation_clips.forEach((warped_clip: TransformedAnimationClipPair) => {
diff --git a/src/preview-generator/preview-generator.ts b/src/preview-generator/preview-generator.ts
index 8302ccc..05610e1 100644
--- a/src/preview-generator/preview-generator.ts
+++ b/src/preview-generator/preview-generator.ts
@@ -141,7 +141,7 @@ class PreviewGenerator {
const position_tracking_bone_name: string | undefined =
RigConfig.by_skeleton_type(SkeletonType.Human)?.position_tracking_bone_name
- AnimationUtility.clean_track_data(this.animation_clips, SkeletonType.Human, position_tracking_bone_name)
+ AnimationUtility.clean_track_data(this.animation_clips, position_tracking_bone_name)
}
const clip = this.animation_clips[0]
diff --git a/src/retarget/RetargetAnimationPreview.ts b/src/retarget/RetargetAnimationPreview.ts
index f46aafd..ae2804f 100644
--- a/src/retarget/RetargetAnimationPreview.ts
+++ b/src/retarget/RetargetAnimationPreview.ts
@@ -128,8 +128,7 @@ export class RetargetAnimationPreview extends EventTarget {
// Clean up the animation track data before using it
// This removes unnecessary position tracks and keeps only rotation data
- AnimationUtility.clean_track_data([this.current_animation_clip],
- skeleton_type, position_tracking_bone_name)
+ AnimationUtility.clean_track_data([this.current_animation_clip], position_tracking_bone_name)
}
this.update_preview_animation()
resolve()