Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Show how many animations are going to be downloaded as selections change

Scott Petrovic0f9c8b33f5d3
diff --git a/src/index.html b/src/index.html
index bd72868..10a1e82 100644
--- a/src/index.html
+++ b/src/index.html
@@ -249,7 +249,7 @@
<div id="animations-listing">
- <!-- Filter textbox for animations -->
+ <!-- Filter textbox for animations -->
<input type="text" id="animation-filter" placeholder="Filter animations...">
@@ -295,7 +295,7 @@
<button id="export-button" title="Export animations. Make sure to select the animations you want to export first.">
<span class="button-icon-group">
<span class="material-symbols-outlined">save_alt</span>
- <span>Download</span>
+ <span>Download <span id="animation-selection-count">(0)</span></span>
</span>
</button>
<a id="download-hidden-link" href="#" style="display:none"></a>
diff --git a/src/lib/AnimationSearch.ts b/src/lib/AnimationSearch.ts
index 2ed1912..971b5ec 100644
--- a/src/lib/AnimationSearch.ts
+++ b/src/lib/AnimationSearch.ts
@@ -7,7 +7,7 @@ export interface AnimationWithState extends AnimationClip {
name: string
}
-export class AnimationSearch {
+export class AnimationSearch extends EventTarget {
private all_animations: AnimationWithState[] = []
private readonly filter_input: HTMLInputElement | null = null
private readonly animation_list_container: HTMLElement | null = null
@@ -15,7 +15,10 @@ export class AnimationSearch {
private readonly theme_manager: ThemeManager
private readonly skeleton_type: SkeletonType
+ private export_options_changed_event: CustomEvent | null = null
+
constructor (filter_input_id: string, animation_list_container_id: string, theme_manager: ThemeManager, skeleton_type: SkeletonType) {
+ super()
this.filter_input = document.querySelector(`#${filter_input_id}`)
this.animation_list_container = document.querySelector(`#${animation_list_container_id}`)
this.theme_manager = theme_manager
@@ -71,6 +74,10 @@ export class AnimationSearch {
if (target?.type === 'checkbox') {
this.save_current_checkbox_states()
}
+
+ // emit an event to notify other parts of the application that export options have changed
+ this.export_options_changed_event = new CustomEvent('export-options-changed', { detail: { selectedAnimations: this.get_selected_animation_indices() } })
+ this.dispatchEvent(this.export_options_changed_event)
})
}
@@ -201,10 +208,18 @@ export class AnimationSearch {
return input.replace(/_/g, ' ')
}
+ /**
+ * Gets the list of filtered animations. Returns all animations if no filtering
+ * @returns An array of selected animations.
+ */
public get_selected_animations (): AnimationWithState[] {
return this.all_animations.filter(animation => animation.isChecked === true)
}
+ /**
+ * Gets the list of animations that are checked to be exported
+ * @returns An array of selected animation indices.
+ */
public get_selected_animation_indices (): number[] {
return this.all_animations
.map((animation, index) => (animation.isChecked === true) ? index : -1)
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.ts
index 665e15b..c51f0f4 100644
--- a/src/lib/EventListeners.ts
+++ b/src/lib/EventListeners.ts
@@ -105,7 +105,9 @@ export class EventListeners {
this.bootstrap.ui.dom_export_button?.addEventListener('click', () => {
const all_clips = this.bootstrap.animations_listing_step.animation_clips()
- this.bootstrap.file_export_step.set_animation_clips_to_export(all_clips)
+ const animations_to_export: number[] = this.bootstrap.animations_listing_step.get_animation_indices_to_export()
+
+ this.bootstrap.file_export_step.set_animation_clips_to_export(all_clips, animations_to_export)
this.bootstrap.file_export_step.export(this.bootstrap.weight_skin_step.final_skinned_meshes(), 'exported-model')
})
diff --git a/src/lib/UI.ts b/src/lib/UI.ts
index d3f0d0c..8fa8a72 100644
--- a/src/lib/UI.ts
+++ b/src/lib/UI.ts
@@ -1,8 +1,3 @@
-import { type AnimationClip } from 'three'
-import { AnimationSearch, type AnimationWithState } from './AnimationSearch'
-import { ThemeManager } from './ThemeManager'
-import { SkeletonType } from './enums/SkeletonType'
-
export class UI {
dom_current_step_index: HTMLElement | null = null
dom_current_step_element: HTMLElement | null = null
@@ -11,9 +6,6 @@ export class UI {
dom_load_model_button: HTMLButtonElement | null = null
dom_load_model_debug_checkbox: HTMLInputElement | null = null
- // Animation search functionality
- private animation_search: AnimationSearch | null = null
-
// load skeleton UI
dom_rotate_model_x_button: HTMLButtonElement | null = null
dom_rotate_model_y_button: HTMLButtonElement | null = null
@@ -66,6 +58,9 @@ export class UI {
dom_extend_arm_button: HTMLButtonElement | null = null
dom_a_pose_correction_options: HTMLElement | null = null
dom_export_button_hidden_link: HTMLElement | null = null
+ dom_animation_count: HTMLElement | null = null
+
+
dom_build_version: HTMLElement | null = null
constructor () {
@@ -143,6 +138,7 @@ export class UI {
// UI for exporting the animation
this.dom_export_button_hidden_link = document.querySelector('#download-hidden-link')
+ this.dom_animation_count = document.querySelector('#animation-selection-count')
}
public hide_all_elements (): void {
@@ -162,33 +158,4 @@ export class UI {
this.dom_skinned_mesh_animation_tools.style.display = 'none'
}
}
-
- public build_animation_clip_ui (animation_clips_to_load: AnimationClip[], theme_manager: ThemeManager, skeleton_type: SkeletonType): void {
- // Initialize AnimationSearch if not already done
- if (this.animation_search === null) {
- this.animation_search = new AnimationSearch('animation-filter', 'animations-items', theme_manager, skeleton_type)
- }
-
- // Use the animation search class to handle the UI
- this.animation_search.initialize_animations(animation_clips_to_load)
- }
-
- public get_animated_selected_elements (): NodeListOf<Element> {
- // this needs to be called ad-hoc as selections might change
- return document.querySelectorAll('#animations-items input[type="checkbox"]')
- }
-
- public get_selected_animations (): AnimationWithState[] {
- if (this.animation_search === null) {
- return []
- }
- return this.animation_search.get_selected_animations()
- }
-
- public get_selected_animation_indices (): number[] {
- if (this.animation_search === null) {
- return []
- }
- return this.animation_search.get_selected_animation_indices()
- }
}
diff --git a/src/lib/processes/StepAnimationsListing.ts b/src/lib/processes/StepAnimationsListing.ts
index 7554d70..2a0051d 100644
--- a/src/lib/processes/StepAnimationsListing.ts
+++ b/src/lib/processes/StepAnimationsListing.ts
@@ -10,6 +10,8 @@ import { AnimationClip, AnimationMixer, Quaternion, Vector3, type SkinnedMesh, t
import { SkeletonType } from '../enums/SkeletonType.ts'
import { Utility } from '../Utilities.ts'
import { ThemeManager } from '../ThemeManager.ts'
+import { AnimationSearch, AnimationWithState } from '../AnimationSearch.ts'
+
// Note: EventTarget is a built-ininterface and do not need to import it
export class StepAnimationsListing extends EventTarget {
@@ -27,6 +29,9 @@ export class StepAnimationsListing extends EventTarget {
private _added_event_listeners: boolean = false
+ // Animation search functionality
+ public animation_search: AnimationSearch | null = null
+
// -z will bring hip bone down
private hip_bone_offset: Vector3 = new Vector3(0, 0, 0) // -z will bring hip bone down. Helps set new base hip position
private hip_bone_scale_factor_z: number = 1.0 // this is used to scale the hip bone position for animations
@@ -190,10 +195,20 @@ export class StepAnimationsListing extends EventTarget {
console.log('all animations loaded:', this.animation_clips_loaded)
// create user interface with all available animation clips
- this.ui.build_animation_clip_ui(this.animation_clips_loaded, this.theme_manager, this.skeleton_type)
+ this.build_animation_clip_ui(this.animation_clips_loaded, this.theme_manager, this.skeleton_type)
+
+ // add event listener to listem for checkbox changes when we change
+ // the amount of animations to export
+ this.animation_search?.addEventListener('export-options-changed', () => {
+ // update the count for the download button
+ this.ui.dom_animation_count.innerHTML = this.animation_search?.get_selected_animation_indices().length.toString() ?? '0'
+ this.ui.dom_animation_count.innerHTML = ' (' + this.ui.dom_animation_count.innerHTML + ')'
+ })
+
this.play_animation(0) // play the first animation by default
}
+
private apply_hip_bone_offset (animation_clips: AnimationClip[]): void {
// update the position keyframes for the hips bone
// this will be used for animations like falling
@@ -298,7 +313,7 @@ export class StepAnimationsListing extends EventTarget {
private update_download_button_enabled (): void {
// see if any of the "export" checkboxes are active. if not we need to disable the "Download" button
- const animation_checkboxes = this.ui.get_animated_selected_elements()
+ const animation_checkboxes = this.get_animated_selected_elements()
const is_any_checkbox_checked: boolean = Array.from(animation_checkboxes).some((checkbox) => {
return checkbox.checked === true
})
@@ -336,4 +351,33 @@ export class StepAnimationsListing extends EventTarget {
// helps ensure we don't add event listeners multiple times
this.has_added_event_listeners = true
}
+
+ public build_animation_clip_ui (animation_clips_to_load: AnimationClip[], theme_manager: ThemeManager, skeleton_type: SkeletonType): void {
+ // Initialize AnimationSearch if not already done
+ if (this.animation_search === null) {
+ this.animation_search = new AnimationSearch('animation-filter', 'animations-items', theme_manager, skeleton_type)
+ }
+
+ // Use the animation search class to handle the UI
+ this.animation_search.initialize_animations(animation_clips_to_load)
+ }
+
+ public get_animated_selected_elements (): NodeListOf<Element> {
+ // this needs to be called ad-hoc as selections might change
+ return document.querySelectorAll('#animations-items input[type="checkbox"]')
+ }
+
+ public get_filtered_animations_list (): AnimationWithState[] {
+ if (this.animation_search === null) {
+ return []
+ }
+ return this.animation_search.get_selected_animations()
+ }
+
+ public get_animation_indices_to_export (): number[] {
+ if (this.animation_search === null) {
+ return []
+ }
+ return this.animation_search.get_selected_animation_indices()
+ }
}
diff --git a/src/lib/processes/StepExportToFile.ts b/src/lib/processes/StepExportToFile.ts
index e968b1d..610b312 100644
--- a/src/lib/processes/StepExportToFile.ts
+++ b/src/lib/processes/StepExportToFile.ts
@@ -7,16 +7,12 @@ export class StepExportToFile extends EventTarget {
private readonly ui: UI = new UI()
private animation_clips_to_export: AnimationClip[] = []
- public set_animation_clips_to_export (all_animations_clips: AnimationClip[]): void {
+ public set_animation_clips_to_export (all_animations_clips: AnimationClip[], animation_checkboxes: number[]): void {
this.animation_clips_to_export = []
- const animation_checkboxes = this.ui.get_animated_selected_elements()
- animation_checkboxes.forEach((checkbox) => {
- if (checkbox.checked === true) {
- const animation_index = checkbox.getAttribute('value')
- const original_clip: AnimationClip = all_animations_clips[animation_index]
- const cloned_clip: AnimationClip = original_clip.clone()
- this.animation_clips_to_export.push(cloned_clip)
- }
+ animation_checkboxes.forEach((indx) => {
+ const original_clip: AnimationClip = all_animations_clips[indx]
+ const cloned_clip: AnimationClip = original_clip.clone()
+ this.animation_clips_to_export.push(cloned_clip)
})
}