Import a 3D Model and automatically assign and export animations
TypeScript
Move frame update loop to live preview to isolate functionality. Turn off and on if on the bone mapping step
Scott Petrovic2abed8435289
diff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.tsindex 0b08676..9b2a5a9 100644--- a/src/retarget/RetargetAnimationListing.ts+++ b/src/retarget/RetargetAnimationListing.ts@@ -1,7 +1,7 @@import { AnimationPlayer } from '../lib/processes/animations-listing/AnimationPlayer.ts'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 Object3DEventMap, type Group } from 'three'+import { type AnimationClip, AnimationMixer, type SkinnedMesh, Object3D, type Object3DEventMap, type Group, Scene } from 'three'import { SkeletonType } from '../lib/enums/SkeletonType.ts'import type { ThemeManager } from '../lib/ThemeManager.ts'import { type TransformedAnimationClipPair } from '../lib/processes/animations-listing/interfaces/TransformedAnimationClipPair.ts'@@ -65,9 +65,9 @@ export class RetargetAnimationListing extends EventTarget {return this.animation_clips_loaded.map(clip => clip.display_animation_clip)}- public load_and_apply_default_animation_to_skinned_mesh (retarget_meshes: Group<Object3DEventMap>): void {+ public load_and_apply_default_animation_to_skinned_mesh (retarget_meshes: Scene): void {// load the Group skinned mesh and convert to normal SkinnedMesh array- this.skinned_meshes_to_animate = retarget_meshes.children.filter((child) => {+ this.skinned_meshes_to_animate = retarget_meshes.children.filter((child: Object3D) => {return (child as SkinnedMesh).isSkinnedMesh}) as SkinnedMesh[]diff --git a/src/retarget/RetargetAnimationPreview.ts b/src/retarget/RetargetAnimationPreview.tsindex ab0778a..c47df30 100644--- a/src/retarget/RetargetAnimationPreview.ts+++ b/src/retarget/RetargetAnimationPreview.ts@@ -41,12 +41,23 @@ export class RetargetAnimationPreview extends EventTarget {console.log('Bone mappings changed, updating preview animation...')this.update_preview_animation()})++ this.setup_animation_loop()}/*** Start the preview system by loading the first available animation*/public async start_preview (): Promise<void> {+ this.is_preview_active = true++ // we have already loaded an animation and retargeted it, just play it again+ // this will happen when going back to bone mapping step from animation listing step+ if (this.retargeted_animation_clip !== null) {+ this.play_default_animation()+ return+ }+if (!this.step_bone_mapping.has_both_skeletons()) {console.log('Cannot start preview: both skeletons are required')return@@ -79,7 +90,6 @@ export class RetargetAnimationPreview extends EventTarget {// Load the first animation based on skeleton typeawait this.load_first_animation()- this.is_preview_active = trueconsole.log('Preview started successfully')}@@ -111,6 +121,12 @@ export class RetargetAnimationPreview extends EventTarget {return}+ // 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) {++ }+try {await new Promise<void>((resolve, reject) => {this.gltf_loader.load(@@ -174,28 +190,52 @@ export class RetargetAnimationPreview extends EventTarget {this.target_skinned_meshes)+ this.play_default_animation()+ }++ private play_default_animation (): void {+ if (this.retargeted_animation_clip === null) {+ console.error('retargeting animation clip is null while playing default animation.')+ return+ }++ if (this.animation_mixer === null) {+ console.error('Animation mixer is null while playing default animation.')+ return+ }+// Apply the retargeted animation to all target skinned meshesthis.target_skinned_meshes.forEach((skinned_mesh) => {- if (this.animation_mixer !== null && this.retargeted_animation_clip !== null) {- const action: AnimationAction = this.animation_mixer.clipAction(this.retargeted_animation_clip, skinned_mesh)- action.reset()- action.play() // should loop automatically- }+ const action: AnimationAction = this.animation_mixer.clipAction(this.retargeted_animation_clip, skinned_mesh)+ action.reset()+ action.play() // should loop automatically})-- console.log('Preview animation updated and playing')}- /**- * Update animation mixer on each frame- */- public update (delta_time: number): void {- if (this.animation_mixer === null || !this.is_preview_active) {- return+ private setup_animation_loop (): void {+ let last_time = performance.now()+ const animate = (): void => {+ requestAnimationFrame(animate)++ // calculate delta time and pass it into preview+ const current_time = performance.now()+ const delta_time = (current_time - last_time) / 1000 // Convert to seconds+ last_time = current_time++ if (this.is_preview_active) {+ this.animation_frame_logic(delta_time)+ }}+ animate()+ }++ private animation_frame_logic (delta_time: number): void {+ if (this.animation_mixer === null) returnthis.animation_mixer.update(delta_time)+ console.log('Animation preview loop playing!!')+// CRITICAL: Update the skeleton and skinned meshes after animation changes the bones// Why do I need this when I don't need it in the main Mesh2Motion engine?this.target_skinned_meshes.forEach((skinned_mesh) => {diff --git a/src/retarget/retarget.ts b/src/retarget/retarget.tsindex cc62203..835395f 100644--- a/src/retarget/retarget.ts+++ b/src/retarget/retarget.ts@@ -1,5 +1,5 @@import { Mesh2MotionEngine } from '../Mesh2MotionEngine.ts'-import { type Group, type Object3DEventMap, Vector3 } from 'three'+import { type Group, type Object3DEventMap, Scene, Vector3 } from 'three'import { StepLoadSourceSkeleton } from './steps/StepLoadSourceSkeleton.ts'import { StepLoadTargetModel } from './steps/StepLoadTargetModel.ts'import { StepBoneMapping } from './steps/StepBoneMapping.ts'@@ -14,7 +14,6 @@ class RetargetModule {private readonly retarget_animation_preview: RetargetAnimationPreviewprivate animation_listing_step: RetargetAnimationListing | null = null- private continue_button: HTMLButtonElement | nullprivate back_to_bone_map_button: HTMLButtonElement | null = nullconstructor () {@@ -43,9 +42,6 @@ class RetargetModule {this.mesh2motion_engine.get_scene(),this.step_bone_mapping)-- // Set up animation loop for preview updates- this.setup_animation_loop()}public init (): void {@@ -61,6 +57,7 @@ class RetargetModule {this.back_to_bone_map_button = document.getElementById('back_to_bone_map_button') as HTMLButtonElementconst bone_mapping_step = document.getElementById('bone-mapping-step')const animation_export_options = document.getElementById('skinned-step-animation-export-options')+ const continue_button = document.getElementById('continue-to-listing-button') as HTMLButtonElementthis.back_to_bone_map_button.onclick = () => {// Hide the skinned-step-animation-export-options ID and show the bone-mapping-step ID@@ -68,6 +65,11 @@ class RetargetModule {animation_export_options.style.display = 'none'bone_mapping_step.style.display = 'inline'}++ // TODO: stop the animation listing step++ // start the live preview again+ this.start_live_preview()}// Listen for source skeleton (Mesh2Motion) loaded@@ -75,72 +77,54 @@ class RetargetModule {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)- this.try_start_preview()+ this.start_live_preview()})// Listen for target model (user-uploaded) loaded- this.step_load_target_model.addEventListener('target-model-loaded', (event: Event) => {- const custom_event = event as CustomEvent- const retargetable_meshes = custom_event.detail.retargetable_meshes as Group<Object3DEventMap>+ this.step_load_target_model.addEventListener('target-model-loaded', (_event: Event) => {+ const retargetable_meshes: Scene | null = this.step_load_target_model.get_retargetable_meshes()// Set target skeleton data in bone mapping (uploaded mesh)this.step_bone_mapping.set_target_skeleton_data(retargetable_meshes)- this.try_start_preview()+ this.start_live_preview()// Show "Continue" button to proceed to animation listing- this.continue_button = document.getElementById('continue-to-listing-button') as HTMLButtonElement- this.continue_button.style.display = 'block'-- // move this continue onclick to the normal add event listener area to prevent multiple bindings- this.continue_button.onclick = () => {- // Hide the bone-mapping-step ID and show the skinned-step-animation-export-options ID- if (bone_mapping_step !== null && animation_export_options !== null) {- bone_mapping_step.style.display = 'none'- animation_export_options.style.display = 'inline'- }-- // load the animation listing step- this.animation_listing_step = new RetargetAnimationListing(- 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.load_and_apply_default_animation_to_skinned_mesh(retargetable_meshes)- }+ continue_button.style.display = 'block'})++ // next button to go to the animation listing step+ continue_button.onclick = () => {+ // Hide the bone-mapping-step ID and show the skinned-step-animation-export-options ID+ if (bone_mapping_step !== null && animation_export_options !== null) {+ bone_mapping_step.style.display = 'none'+ animation_export_options.style.display = 'inline'+ }++ // stop the live preview step from playing its animation+ this.retarget_animation_preview.stop_preview()++ // load the animation listing step and start it+ this.animation_listing_step = new RetargetAnimationListing(+ this.mesh2motion_engine.get_theme_manager(),+ this.step_bone_mapping+ )+ this.animation_listing_step.begin(this.step_load_source_skeleton.get_skeleton_type())++ const retargetable_meshes: Scene | null = this.step_load_target_model.get_retargetable_meshes()+ this.animation_listing_step.load_and_apply_default_animation_to_skinned_mesh(retargetable_meshes)+ }}- private try_start_preview (): void {+ private start_live_preview (): void {// Start preview when both skeletons are loadedif (this.step_bone_mapping.has_both_skeletons()) {console.log('Both skeletons loaded, starting animation preview...')+this.retarget_animation_preview.start_preview().catch((error) => {console.error('Failed to start preview:', error)})}}-- private setup_animation_loop (): void {- let last_time = performance.now()-- const animate = (): void => {- requestAnimationFrame(animate)-- const current_time = performance.now()- const delta_time = (current_time - last_time) / 1000 // Convert to seconds- last_time = current_time-- // Update animation preview- this.retarget_animation_preview.update(delta_time)-- // Update animation listing if it exists- if (this.animation_listing_step !== null) {- this.animation_listing_step.frame_change(delta_time)- }- }-- animate()- }}// Initialize when DOM is readydiff --git a/src/retarget/steps/StepBoneMapping.ts b/src/retarget/steps/StepBoneMapping.tsindex 9801284..2ee1cc8 100644--- a/src/retarget/steps/StepBoneMapping.ts+++ b/src/retarget/steps/StepBoneMapping.ts@@ -83,7 +83,7 @@ export class StepBoneMapping extends EventTarget {this.update_auto_map_button_visibility()}- public set_target_skeleton_data (skeleton_data: Scene): void {+ public set_target_skeleton_data (skeleton_data: Scene | null): void {this.target_skeleton_data = skeleton_dataconsole.log('Target skeleton data set in bone mapping:', this.target_skeleton_data)this.update_target_bones_list()diff --git a/src/retarget/steps/StepLoadTargetModel.ts b/src/retarget/steps/StepLoadTargetModel.tsindex 9a35a92..29d7452 100644--- a/src/retarget/steps/StepLoadTargetModel.ts+++ b/src/retarget/steps/StepLoadTargetModel.ts@@ -11,6 +11,8 @@ export class StepLoadTargetModel extends EventTarget {private file_input: HTMLInputElement | null = nullprivate load_model_button: HTMLLabelElement | null = null+ private retargetable_meshes: Scene | null = null+constructor (mesh2motion_engine: Mesh2MotionEngine) {super()this.mesh2motion_engine = mesh2motion_engine@@ -20,6 +22,10 @@ export class StepLoadTargetModel extends EventTarget {this.add_event_listeners()}+ public get_retargetable_meshes (): Scene | null {+ return this.retargetable_meshes+ }+private add_event_listeners (): void {// Get DOM elementsthis.file_input = document.getElementById('upload-file') as HTMLInputElement@@ -101,8 +107,9 @@ export class StepLoadTargetModel extends EventTarget {// Add skeleton helperthis.add_skeleton_helper(retargetable_meshes)- // Dispatch event with loaded model data- this.dispatch_target_model_loaded(retargetable_meshes)+ // Save the final retargetable meshes and dispatch event+ this.retargetable_meshes = retargetable_meshes+ this.dispatchEvent(new CustomEvent('target-model-loaded'))}}, { once: true })} catch (error) {@@ -211,10 +218,4 @@ export class StepLoadTargetModel extends EventTarget {camera_position})}-- private dispatch_target_model_loaded (retargetable_meshes: Scene): void {- this.dispatchEvent(new CustomEvent('target-model-loaded', {- detail: { retargetable_meshes }- }))- }}