Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

have animation service store the data for the skeleton type...and everything can call that

Scott Petrovic5d6cec9f0d94
diff --git a/src/retarget/AnimationRetargetService.ts b/src/retarget/AnimationRetargetService.ts
index c750875..131d41e 100644
--- a/src/retarget/AnimationRetargetService.ts
+++ b/src/retarget/AnimationRetargetService.ts
@@ -1,6 +1,10 @@
-import { AnimationClip, Euler, type Object3D, Quaternion, QuaternionKeyframeTrack, Vector3, VectorKeyframeTrack, type Scene, type Group, type SkinnedMesh } from 'three'
+import {
+ AnimationClip, Euler, type Object3D, Quaternion, QuaternionKeyframeTrack, Vector3,
+ VectorKeyframeTrack, type Scene, type Group, type SkinnedMesh
+} from 'three'
import { RetargetUtils } from './RetargetUtils.ts'
import { TargetBoneMappingType } from './steps/StepBoneMapping.ts'
+import { SkeletonType } from '../lib/enums/SkeletonType.ts'
/**
* Parsed animation track name containing bone name and property type
@@ -9,12 +13,24 @@ export interface TrackNameParts {
bone_name: string
property: string
}
-
// AnimationRetargetService - Shared service for retargeting animations from one skeleton to another
// Used by both RetargetAnimationPreview and RetargetAnimationListing
export class AnimationRetargetService {
private static instance: AnimationRetargetService | null = null
+ /**
+ * Get/set for the skeleton type. This will be the source of truth
+ * for other classes to grab this data
+ */
+ private skeleton_type: SkeletonType = SkeletonType.None
+ public set_skeleton_type (type: SkeletonType): void {
+ this.skeleton_type = type
+ }
+
+ public get_skeleton_type (): SkeletonType {
+ return this.skeleton_type
+ }
+
private constructor () {}
public static getInstance (): AnimationRetargetService {
diff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.ts
index 939797b..ce68dde 100644
--- a/src/retarget/RetargetAnimationListing.ts
+++ b/src/retarget/RetargetAnimationListing.ts
@@ -2,7 +2,7 @@ import { AnimationPlayer } from '../lib/processes/animations-listing/AnimationPl
import { AnimationSearch } from '../lib/processes/animations-listing/AnimationSearch.ts'
import { AnimationLoader } from '../lib/processes/animations-listing/AnimationLoader.ts'
import { type AnimationClip, AnimationMixer, type SkinnedMesh, Object3D, type Scene, type AnimationAction } from 'three'
-import { SkeletonType } from '../lib/enums/SkeletonType.ts'
+import type { SkeletonType } from '../lib/enums/SkeletonType.ts'
import type { ThemeManager } from '../lib/ThemeManager.ts'
import { type TransformedAnimationClipPair } from '../lib/processes/animations-listing/interfaces/TransformedAnimationClipPair.ts'
import { AnimationRetargetService } from './AnimationRetargetService.ts'
@@ -24,8 +24,6 @@ export class RetargetAnimationListing extends EventTarget {
private skinned_meshes_to_animate: SkinnedMesh[] = []
- private skeleton_type: SkeletonType = SkeletonType.Human
-
private _added_event_listeners: boolean = false
public animation_search: AnimationSearch | null = null
@@ -42,9 +40,7 @@ export class RetargetAnimationListing extends EventTarget {
this.step_bone_mapping = step_bone_mapping
}
- public begin (skeleton_type: SkeletonType): void {
- this.skeleton_type = skeleton_type
-
+ public begin (): void {
this.reset_step_data()
if (!this._added_event_listeners) {
@@ -99,7 +95,9 @@ export class RetargetAnimationListing extends EventTarget {
this.animation_clips_loaded = []
this.animation_mixer = new AnimationMixer(new Object3D())
- this.animation_loader.load_animations(this.skeleton_type)
+ // this animation loader comes from the Mesh2Motion engine, so still
+ // pass in the skeleton type this way
+ this.animation_loader.load_animations(AnimationRetargetService.getInstance().get_skeleton_type())
.then((loaded_clips: TransformedAnimationClipPair[]) => {
this.animation_clips_loaded = loaded_clips
this.on_all_animations_loaded()
@@ -165,7 +163,7 @@ export class RetargetAnimationListing extends EventTarget {
'animation-filter',
'animations-items',
this.theme_manager,
- this.skeleton_type
+ AnimationRetargetService.getInstance().get_skeleton_type()
)
this.animation_search.initialize_animations(animation_clips)
@@ -246,15 +244,19 @@ export class RetargetAnimationListing extends EventTarget {
)
// configure the export out step with retargeting info
- this.step_export_retargeted_animations.setup_retargeting(
- this.target_rig_scene,
- this.skinned_meshes_to_animate,
- this.step_bone_mapping.get_bone_mapping(),
- this.step_bone_mapping.get_target_mapping_template(),
- this.step_bone_mapping.get_source_armature(),
- this.step_bone_mapping.get_target_skeleton_data()
- )
- this.step_export_retargeted_animations.export('retargeted_animations')
+ if (this.target_rig_scene !== null) {
+ this.step_export_retargeted_animations.setup_retargeting(
+ this.target_rig_scene,
+ this.skinned_meshes_to_animate,
+ this.step_bone_mapping.get_bone_mapping(),
+ this.step_bone_mapping.get_target_mapping_template(),
+ this.step_bone_mapping.get_source_armature(),
+ this.step_bone_mapping.get_target_skeleton_data()
+ )
+ this.step_export_retargeted_animations.export('retargeted_animations')
+ } else {
+ console.error('Target rig scene is null, cannot export retargeted animations.')
+ }
})
}
diff --git a/src/retarget/RetargetAnimationPreview.ts b/src/retarget/RetargetAnimationPreview.ts
index fc7947d..ba1f6d0 100644
--- a/src/retarget/RetargetAnimationPreview.ts
+++ b/src/retarget/RetargetAnimationPreview.ts
@@ -113,7 +113,7 @@ export class RetargetAnimationPreview extends EventTarget {
*/
private async load_first_animation (): Promise<void> {
// get location of animation file to load that has preview animations
- const source_skeleton_type = this.step_bone_mapping.get_source_skeleton_type()
+ const source_skeleton_type = AnimationRetargetService.getInstance().get_skeleton_type()
const animation_file_path = RetargetUtils.get_animation_file_path(source_skeleton_type)
if (animation_file_path === null) {
@@ -124,7 +124,8 @@ export class RetargetAnimationPreview extends EventTarget {
// if we are coming back to this step and already loaded a default animation, skip reloading
// and just do the playing
if (this.current_animation_clip !== null) {
-
+ this.play_default_animation()
+ return
}
try {
diff --git a/src/retarget/retarget.ts b/src/retarget/retarget.ts
index ddd0e69..577099b 100644
--- a/src/retarget/retarget.ts
+++ b/src/retarget/retarget.ts
@@ -5,6 +5,7 @@ import { StepLoadTargetModel } from './steps/StepLoadTargetModel.ts'
import { StepBoneMapping } from './steps/StepBoneMapping.ts'
import { RetargetAnimationPreview } from './RetargetAnimationPreview.ts'
import { RetargetAnimationListing } from './RetargetAnimationListing.ts'
+import { AnimationRetargetService } from './AnimationRetargetService'
class RetargetModule {
private readonly mesh2motion_engine: Mesh2MotionEngine
@@ -83,8 +84,8 @@ class RetargetModule {
this.step_load_source_skeleton.addEventListener('skeleton-loaded', () => {
const source_armature = this.step_load_source_skeleton.get_loaded_source_armature()
const skeleton_type = this.step_load_source_skeleton.get_skeleton_type()
-
- this.step_bone_mapping.set_source_skeleton_data(source_armature, skeleton_type)
+ AnimationRetargetService.getInstance().set_skeleton_type(skeleton_type)
+ this.step_bone_mapping.set_source_skeleton_data(source_armature)
})
// Listen for target model (user-uploaded) loaded
@@ -118,7 +119,7 @@ class RetargetModule {
this.mesh2motion_engine.get_theme_manager(),
this.step_bone_mapping
)
- this.animation_listing_step.begin(this.step_load_source_skeleton.get_skeleton_type())
+ this.animation_listing_step.begin()
this.mesh2motion_engine.show_animation_player(true)
const retargetable_meshes: Scene | null = this.step_load_target_model.get_retargetable_meshes()