Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Start building the animations listing step for the retargeting. Try to re-use a lot of things from other pages

Scott Petrovic3eb3d178037e
diff --git a/src/Mesh2MotionEngine.ts b/src/Mesh2MotionEngine.ts
index e28b633..0a7277a 100644
--- a/src/Mesh2MotionEngine.ts
+++ b/src/Mesh2MotionEngine.ts
@@ -95,6 +95,10 @@ export class Mesh2MotionEngine {
this.setup_tooltips()
}
+ public get_theme_manager(): ThemeManager {
+ return this.theme_manager
+ }
+
/** Eventually make the scene its own singleton/manager class
* that we can inject into other classes that need it
*/
diff --git a/src/lib/processes/animations-listing/AnimationLoader.ts b/src/lib/processes/animations-listing/AnimationLoader.ts
index 3098756..fa465a4 100644
--- a/src/lib/processes/animations-listing/AnimationLoader.ts
+++ b/src/lib/processes/animations-listing/AnimationLoader.ts
@@ -38,7 +38,7 @@ export class AnimationLoader extends EventTarget {
*/
public async load_animations (
skeleton_type: SkeletonType,
- skeleton_scale: number
+ skeleton_scale: number = 1.0
): Promise<TransformedAnimationClipPair[]> {
const file_paths = this.get_animation_file_paths(skeleton_type)
diff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.ts
new file mode 100644
index 0000000..de90d05
--- /dev/null
+++ b/src/retarget/RetargetAnimationListing.ts
@@ -0,0 +1,182 @@
+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, Object3DEventMap, Group } from 'three'
+import { SkeletonType } from '../lib/enums/SkeletonType.ts'
+import { ThemeManager } from '../lib/ThemeManager.ts'
+import { type TransformedAnimationClipPair } from '../lib/processes/animations-listing/interfaces/TransformedAnimationClipPair.ts'
+
+/**
+ * RetargetAnimationListing - Handles animation listing and playback specifically for retargeting workflow
+ * Reuses AnimationPlayer, AnimationSearch, and AnimationLoader but tailored for retargeting needs
+ */
+export class RetargetAnimationListing extends EventTarget {
+ private readonly theme_manager: ThemeManager
+ private readonly animation_player: AnimationPlayer
+ private readonly animation_loader: AnimationLoader = new AnimationLoader()
+
+ private animation_clips_loaded: TransformedAnimationClipPair[] = []
+ private animation_mixer: AnimationMixer = new AnimationMixer(new Object3D())
+ private skinned_meshes_to_animate: SkinnedMesh[] = []
+ private skeleton_type: SkeletonType = SkeletonType.Human
+
+ private _added_event_listeners: boolean = false
+
+ public animation_search: AnimationSearch | null = null
+
+ constructor (theme_manager: ThemeManager) {
+ super()
+ this.theme_manager = theme_manager
+ this.animation_player = new AnimationPlayer()
+ }
+
+ public begin (skeleton_type: SkeletonType): void {
+ this.skeleton_type = skeleton_type
+
+ this.reset_step_data()
+
+ if (!this._added_event_listeners) {
+ this.add_event_listeners()
+ this._added_event_listeners = true
+ }
+ }
+
+ public reset_step_data (): void {
+ this.animation_clips_loaded = []
+ this.skinned_meshes_to_animate = []
+ this.animation_mixer = new AnimationMixer(new Object3D())
+ this.animation_player.clear_animation()
+ }
+
+ public mixer (): AnimationMixer {
+ return this.animation_mixer
+ }
+
+ public frame_change (delta_time: number): void {
+ this.mixer().update(delta_time)
+ this.animation_player.update(delta_time)
+ }
+
+ public animation_clips (): AnimationClip[] {
+ 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 {
+ // load the Group skinned mesh and convert to normal SkinnedMesh array
+ this.skinned_meshes_to_animate = retarget_meshes.children.filter((child) => {
+ return (child as SkinnedMesh).isSkinnedMesh
+ }) as SkinnedMesh[]
+
+ console.log(`Preparing to load animations for ${this.skinned_meshes_to_animate.length} skinned meshes`)
+
+ this.animation_loader.set_animations_file_path('../../animations/')
+ this.animation_clips_loaded = []
+ this.animation_mixer = new AnimationMixer(new Object3D())
+
+ this.animation_loader.load_animations(this.skeleton_type)
+ .then((loaded_clips: TransformedAnimationClipPair[]) => {
+ this.animation_clips_loaded = loaded_clips
+ this.on_all_animations_loaded()
+ })
+ .catch((error: Error) => {
+ console.error('Failed to load animations for retargeting:', error)
+ })
+ }
+
+ private on_all_animations_loaded (): void {
+ // Sort alphabetically
+ this.animation_clips_loaded.sort((a: TransformedAnimationClipPair, b: TransformedAnimationClipPair) => {
+ if (a.display_animation_clip.name < b.display_animation_clip.name) return -1
+ if (a.display_animation_clip.name > b.display_animation_clip.name) return 1
+ return 0
+ })
+
+ // Build animation UI
+ this.build_animation_clip_ui(
+ this.animation_clips_loaded.map(clip => clip.display_animation_clip)
+ )
+
+ // Update animation selection count when selections change
+ this.animation_search?.addEventListener('export-options-changed', () => {
+ const count_element = document.getElementById('animation-selection-count')
+ if (count_element !== null) {
+ count_element.textContent = this.animation_search?.get_selected_animation_indices().length.toString() ?? '0'
+ }
+ })
+
+ // Update animation listing count display
+ const listing_count_element = document.getElementById('animation-listing-count')
+ if (listing_count_element !== null) {
+ listing_count_element.textContent = this.animation_clips_loaded.length.toString()
+ }
+
+ console.log(`Loaded ${this.animation_clips_loaded.length} animations for retargeting`)
+ }
+
+ private build_animation_clip_ui (animation_clips: AnimationClip[]): void {
+ // Initialize AnimationSearch with the loaded clips
+ this.animation_search = new AnimationSearch(
+ 'animation-filter',
+ 'animations-items',
+ this.theme_manager,
+ this.skeleton_type
+ )
+
+ this.animation_search.initialize_animations(animation_clips)
+
+ // Add click event listeners to animation items for playback
+ const animations_container = document.getElementById('animations-items')
+ if (animations_container !== null) {
+ animations_container.addEventListener('click', (event) => {
+ const target = event.target as HTMLElement
+ const button = target.closest('.play') as HTMLButtonElement
+
+ if (button !== null) {
+ const index = parseInt(button.dataset.index ?? '-1')
+ if (index >= 0) {
+ this.play_animation(index)
+ }
+ }
+ })
+ }
+ }
+
+ private play_animation (index: number): void {
+ if (index < 0 || index >= this.animation_clips_loaded.length) {
+ console.warn('Invalid animation index:', index)
+ return
+ }
+
+ const animation_pair = this.animation_clips_loaded[index]
+ const display_clip = animation_pair.display_animation_clip
+
+ // Stop all current actions
+ this.animation_mixer.stopAllAction()
+
+ // Create new actions for each skinned mesh
+ const actions = this.skinned_meshes_to_animate.map((mesh) => {
+ const action = this.animation_mixer.clipAction(display_clip, mesh)
+ action.reset()
+ action.play()
+ return action
+ })
+
+ // Update the animation player UI
+ this.animation_player.set_animation(display_clip, actions)
+
+ console.log('Playing animation:', display_clip.name)
+ }
+
+ private add_event_listeners (): void {
+ // Add any retarget-specific event listeners here
+ // For now, keeping it minimal
+ }
+
+ public get_animation_player (): AnimationPlayer {
+ return this.animation_player
+ }
+
+ public get_selected_animation_indices (): number[] {
+ return this.animation_search?.get_selected_animation_indices() ?? []
+ }
+}
diff --git a/src/retarget/index.html b/src/retarget/index.html
index 8921cdc..07560cf 100644
--- a/src/retarget/index.html
+++ b/src/retarget/index.html
@@ -121,45 +121,10 @@
<div id="animations-items"> </div>
- <!-- slider to control arm extension (hide this feature until the base functionality is more stable) -->
- <div id="a-pose-correction-options">
- <div style="
- display: flex;
- flex-direction: row;
- align-items: center;
- gap: 1rem;
- justify-content: center;
- ">
- <label style="display:inline-flex">A-Pose Correction - Open Arms</label>
- <span class="material-symbols-outlined" data-tippy-content="If your model uses an A-Pose, this will help expand/contract arms for all animations">help</span>
-
- <button class="secondary-button" id="reset-a-pose-button">
- <img src="../images/icons/reset.svg" alt="Reset A-Pose" width="20" height="20" />
- </button>
- </div>
-
- <div style="display: flex; flex-direction: row; gap: 1rem; justify-content: flex-start; align-items: center;">
- <input type="number" id="extend-arm-numeric-input" name="arm-extend-input" value="0" min="-70" max="100" step="1">
- <span class="suffix-unit">%</span>
- <input id="extend-arm-range-input" style="flex-grow: 1;" type="range" min="-70" max="100" value="0" />
- </div>
-
- <hr />
-
- </div>
-
<div style="display: flex; gap: 0.5rem;">
<button class="secondary-button" id="back_to_bone_map_button" >&#x2039; Back</button>
- <!-- testing the mirroring options -->
- <div class="styled-checkbox icon-toggle">
- <input type="checkbox" id="mirror-animations-checkbox" name="mirror-animations" value="true" style="display:none;">
- <label for="mirror-animations-checkbox" data-tippy-content="Mirror animations" tabindex="0">
- <img src="../images/icons/mirror.svg" alt="Mirror animations" width="30" height="30" />
- </label>
- </div>
-
<!-- checkbox option to show skeleton -->
<div class="styled-checkbox icon-toggle">
diff --git a/src/retarget/retarget.css b/src/retarget/retarget.css
index 231c9e0..8987dd9 100644
--- a/src/retarget/retarget.css
+++ b/src/retarget/retarget.css
@@ -111,4 +111,12 @@ h1 {
background: var(--text-alternate);
padding: 0.25rem 0.5rem;
border-radius: 5px;
+}
+
+#animations-items {
+ max-height: calc(100vh - 14rem);
+}
+
+#animation-listing-count {
+ top: 1.4rem;
}
\ No newline at end of file
diff --git a/src/retarget/retarget.ts b/src/retarget/retarget.ts
index eefe114..5406fc5 100644
--- a/src/retarget/retarget.ts
+++ b/src/retarget/retarget.ts
@@ -4,6 +4,7 @@ import { StepLoadSourceSkeleton } from './steps/StepLoadSourceSkeleton.ts'
import { StepLoadTargetModel } from './steps/StepLoadTargetModel.ts'
import { StepBoneMapping } from './steps/StepBoneMapping.ts'
import { RetargetAnimationPreview } from './RetargetAnimationPreview.ts'
+import { RetargetAnimationListing } from './RetargetAnimationListing.ts'
class RetargetModule {
private readonly mesh2motion_engine: Mesh2MotionEngine
@@ -11,6 +12,7 @@ class RetargetModule {
private readonly step_load_target_model: StepLoadTargetModel
private readonly step_bone_mapping: StepBoneMapping
private readonly retarget_animation_preview: RetargetAnimationPreview
+ private animation_listing_step: RetargetAnimationListing | null = null
private continue_button: HTMLButtonElement | null
private back_to_bone_map_button: HTMLButtonElement | null = null
@@ -88,12 +90,19 @@ class RetargetModule {
// 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.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)
}
})
}