Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

small refactor for target armature with retargeting

Scott Petrovic50312a0282a0
diff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.ts
index 00ccb7b..3b9c39f 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 Scene, type AnimationAction } from 'three'
+import { type AnimationClip, AnimationMixer, type SkinnedMesh, Object3D, type AnimationAction } from 'three'
import type { ThemeManager } from '../lib/ThemeManager.ts'
import { type TransformedAnimationClipPair } from '../lib/processes/animations-listing/interfaces/TransformedAnimationClipPair.ts'
import { AnimationRetargetService } from './AnimationRetargetService.ts'
@@ -29,7 +29,6 @@ export class RetargetAnimationListing extends EventTarget {
private is_animations_active: boolean = false
- private target_rig_scene: Scene | null = null
private export_button: HTMLButtonElement | null = null
constructor (theme_manager: ThemeManager, step_bone_mapping: StepBoneMapping) {
@@ -74,14 +73,11 @@ export class RetargetAnimationListing extends EventTarget {
this.is_animations_active = false
}
- public load_and_apply_default_animation_to_skinned_mesh (retarget_meshes: Scene): void {
- // we will need this later when exporting
- this.target_rig_scene = retarget_meshes
-
+ public load_and_apply_default_animation_to_skinned_mesh (): void {
// load the Group skinned mesh and convert to normal SkinnedMesh array
// the skinned meshes might be buried deep in the hierarchy, so traverse the scene
const skinned_meshes: SkinnedMesh[] = []
- retarget_meshes.traverse((child: Object3D) => {
+ AnimationRetargetService.getInstance().get_target_armature().traverse((child: Object3D) => {
if ((child as SkinnedMesh).isSkinnedMesh) {
skinned_meshes.push(child as SkinnedMesh)
}
@@ -240,17 +236,12 @@ export class RetargetAnimationListing extends EventTarget {
)
// configure the export out step with retargeting info
- 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_skeleton_data()
- )
- this.step_export_retargeted_animations.export('retargeted_animations')
- } else {
- console.error('Target rig scene is null, cannot export retargeted animations.')
- }
+ this.step_export_retargeted_animations.setup_retargeting(
+ this.skinned_meshes_to_animate,
+ this.step_bone_mapping.get_bone_mapping(),
+ this.step_bone_mapping.get_target_skeleton_data()
+ )
+ this.step_export_retargeted_animations.export('retargeted_animations')
})
}
diff --git a/src/retarget/retarget.ts b/src/retarget/retarget.ts
index 9f09984..c0e1c2a 100644
--- a/src/retarget/retarget.ts
+++ b/src/retarget/retarget.ts
@@ -95,13 +95,13 @@ class RetargetModule {
// Listen for target model (user-uploaded) loaded
this.step_load_target_model.addEventListener('target-model-loaded', (_event: Event) => {
- const retargetable_armature: Scene | null = this.step_load_target_model.get_retargetable_meshes()
+ const temp_target_armature: Scene | null = this.step_load_target_model.get_retargetable_meshes()
- if (retargetable_armature == null) {
+ if (temp_target_armature == null) {
console.error('No retargetable meshes found in the uploaded model.')
return
}
- AnimationRetargetService.getInstance().set_target_armature(retargetable_armature)
+ AnimationRetargetService.getInstance().set_target_armature(temp_target_armature)
// Set target skeleton data in bone mapping (uploaded mesh)
this.step_bone_mapping.target_armature_updated()
this.start_live_preview()
@@ -132,14 +132,8 @@ class RetargetModule {
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()
-
- if (retargetable_meshes !== null) {
- this.animation_listing_step.load_and_apply_default_animation_to_skinned_mesh(retargetable_meshes)
- this.animation_listing_step.start_preview()
- } else {
- console.error('Retargetable meshes are null while processing click button.')
- }
+ this.animation_listing_step.load_and_apply_default_animation_to_skinned_mesh()
+ this.animation_listing_step.start_preview()
}
}
diff --git a/src/retarget/steps/StepExportRetargetedAnimations.ts b/src/retarget/steps/StepExportRetargetedAnimations.ts
index 4438d94..7cff5b4 100644
--- a/src/retarget/steps/StepExportRetargetedAnimations.ts
+++ b/src/retarget/steps/StepExportRetargetedAnimations.ts
@@ -10,14 +10,12 @@ export class StepExportRetargetedAnimations extends EventTarget {
// target_mapping_type is now managed by AnimationRetargetService singleton
private target_skeleton_data: Scene | null = null
private target_skinned_meshes: SkinnedMesh[] = []
- private target_rig_scene: Scene | null = null
- public setup_retargeting (target_rig_scene: Scene, meshes: SkinnedMesh[], bone_mapping: Map<string, string>,
+ public setup_retargeting (meshes: SkinnedMesh[], bone_mapping: Map<string, string>,
skeleton_data: Scene | null): void {
this.bone_mapping = bone_mapping
this.target_skeleton_data = skeleton_data
this.target_skinned_meshes = meshes
- this.target_rig_scene = target_rig_scene
}
public set_animation_clips_to_export (all_animations_clips: AnimationClip[], animation_checkboxes: number[]): void {
@@ -35,11 +33,6 @@ export class StepExportRetargetedAnimations extends EventTarget {
return
}
- if (this.target_rig_scene == null) {
- console.log('ERROR: target rig scene is null, cannot export')
- return
- }
-
// Retarget all animation clips before export
let retargeted_clips: AnimationClip[] = []
retargeted_clips = this.animation_clips_to_export.map((clip) =>
@@ -51,7 +44,8 @@ export class StepExportRetargetedAnimations extends EventTarget {
)
console.log('Retargeted animation clips:', retargeted_clips)
- this.export_glb(this.target_rig_scene, retargeted_clips, filename)
+ const target_rig_scene: Scene = AnimationRetargetService.getInstance().get_target_armature()
+ this.export_glb(target_rig_scene, retargeted_clips, filename)
.then(() => {
console.log('Exported GLB successfully')
})