Import a 3D Model and automatically assign and export animations
TypeScript
added glb imports
Lino Emmenegger878da432f12e
diff --git a/src/create.html b/src/create.htmlindex 97dc885..3342201 100644--- a/src/create.html+++ b/src/create.html@@ -371,6 +371,11 @@</label></div>+ <button class="secondary-button" id="import-animations-button" title="Import GLB animations" aria-label="Import GLB animations">+ <span class="material-symbols-outlined">upload</span>+ </button>+ <input id="import-animations-input" type="file" accept=".glb" multiple style="display:none" />+<!-- checkbox option to show skeleton --><div class="styled-checkbox icon-toggle">diff --git a/src/index.html b/src/index.htmlindex f01d817..cf06116 100644--- a/src/index.html+++ b/src/index.html@@ -153,6 +153,11 @@</label></div>+ <button class="secondary-button" id="import-animations-button" title="Import GLB animations" aria-label="Import GLB animations">+ <span class="material-symbols-outlined">upload</span>+ </button>+ <input id="import-animations-input" type="file" accept=".glb" multiple style="display:none" />+<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>diff --git a/src/lib/UI.ts b/src/lib/UI.tsindex 500c8d3..a1d7f9a 100644--- a/src/lib/UI.ts+++ b/src/lib/UI.ts@@ -78,6 +78,7 @@ export class UI {dom_total_time: HTMLElement | null = nulldom_import_animations_button: HTMLButtonElement | null = null+ dom_import_animations_input: HTMLInputElement | null = nulldom_extend_arm_range_input: HTMLInputElement | null = nulldom_extend_arm_numeric_input: HTMLInputElement | null = nulldom_a_pose_correction_options: HTMLElement | null = null@@ -166,7 +167,8 @@ export class UI {// UI Controls for working with animation list/selection and exportthis.dom_animation_clip_list = document.querySelector('#animations-items')this.dom_export_button = document.querySelector('#export-button')- this.dom_import_animations_button = document.querySelector('#mirror-animations-checkbox')+ this.dom_import_animations_button = document.querySelector('#import-animations-button')+ this.dom_import_animations_input = document.querySelector('#import-animations-input')this.dom_mirror_animations_checkbox = document.querySelector('#mirror-animations-checkbox')this.dom_reset_a_pose_button = document.querySelector('#reset-a-pose-button')diff --git a/src/lib/processes/animations-listing/AnimationLoader.ts b/src/lib/processes/animations-listing/AnimationLoader.tsindex 705a79e..7893802 100644--- a/src/lib/processes/animations-listing/AnimationLoader.ts+++ b/src/lib/processes/animations-listing/AnimationLoader.ts@@ -124,6 +124,62 @@ export class AnimationLoader extends EventTarget {})}+ /**+ * Loads animations from a local GLB file.+ */+ public async load_animations_from_file (+ file: File,+ skeleton_scale: number = 1.0+ ): Promise<TransformedAnimationClipPair[]> {+ const file_url = URL.createObjectURL(file)+ const file_total = file.size > 0 ? file.size : 1++ this.file_progress_map.clear()+ this.completed_files = 0+ this.total_files = 1+ this.file_progress_map.set(file.name, { loaded: 0, total: file_total })+ this.emit_enhanced_progress(file.name, 0, file_total)++ return await new Promise((resolve, reject) => {+ this.gltf_loader.load(+ file_url,+ (gltf: any) => {+ URL.revokeObjectURL(file_url)++ try {+ const animations = gltf.animations as AnimationClip[]+ if (animations === null || animations === undefined || animations.length === 0) {+ reject(new Error('No animations found in the GLB file.'))+ return+ }++ this.file_progress_map.set(file.name, { loaded: file_total, total: file_total })+ this.completed_files = 1+ this.emit_enhanced_progress(file.name, file_total, file_total)++ const processed_clips = this.process_loaded_animations(animations, skeleton_scale)+ resolve(processed_clips)+ } catch (error) {+ const error_message = error instanceof Error ? error.message : String(error)+ reject(new Error(`Failed to process animations from ${file.name}: ${error_message}`))+ }+ },+ (progress_event) => {+ if (progress_event.lengthComputable) {+ const total_bytes = progress_event.total > 0 ? progress_event.total : file_total+ this.file_progress_map.set(file.name, { loaded: progress_event.loaded, total: total_bytes })+ this.emit_enhanced_progress(file.name, progress_event.loaded, total_bytes)+ }+ },+ (error) => {+ URL.revokeObjectURL(file_url)+ const error_message = error instanceof Error ? error.message : String(error)+ reject(new Error(`Failed to load animation file ${file.name}: ${error_message}`))+ }+ )+ })+ }+/*** Gets the file paths for animations based on skeleton type*/diff --git a/src/lib/processes/animations-listing/AnimationSearch.ts b/src/lib/processes/animations-listing/AnimationSearch.tsindex 84792fc..dfa9c31 100644--- a/src/lib/processes/animations-listing/AnimationSearch.ts+++ b/src/lib/processes/animations-listing/AnimationSearch.ts@@ -35,6 +35,19 @@ export class AnimationSearch extends EventTarget {this.render_filtered_animations('')}+ public add_animations (animations: AnimationClip[]): void {+ const new_animations = animations.map(clip => {+ const animation_with_state: AnimationWithState = clip as any+ animation_with_state.name = clip.name+ animation_with_state.isChecked = false+ return animation_with_state+ })++ this.all_animations.push(...new_animations)+ const filter_text = this.filter_input?.value.toLowerCase() ?? ''+ this.render_filtered_animations(filter_text)+ }+private setup_event_listeners (): void {this.setup_filter_listener()this.setup_checkbox_listeners()diff --git a/src/lib/processes/animations-listing/StepAnimationsListing.ts b/src/lib/processes/animations-listing/StepAnimationsListing.tsindex 3aa9aeb..aa786c9 100644--- a/src/lib/processes/animations-listing/StepAnimationsListing.ts+++ b/src/lib/processes/animations-listing/StepAnimationsListing.ts@@ -1,5 +1,6 @@import { UI } from '../../UI.ts'import { AnimationPlayer } from './AnimationPlayer.ts'+import { ModalDialog } from '../../ModalDialog.ts'import {type AnimationClip, AnimationMixer, type SkinnedMesh, type AnimationAction, Object3D@@ -351,6 +352,29 @@ export class StepAnimationsListing extends EventTarget {this.play_animation(this.current_playing_index)})+ this.ui.dom_import_animations_button?.addEventListener('click', () => {+ this.ui.dom_import_animations_input?.click()+ })++ this.ui.dom_import_animations_input?.addEventListener('change', async (event) => {+ const input = event.target as HTMLInputElement+ const files = input.files+ if (files === null || files.length === 0) {+ return+ }++ for (const file of Array.from(files)) {+ const file_name = file.name.toLowerCase()+ if (!file_name.endsWith('.glb')) {+ new ModalDialog('Unsupported file type. Please select a GLB file.', 'Error').show()+ continue+ }+ await this.import_animation_glb(file)+ }++ input.value = ''+ })+// helps ensure we don't add event listeners multiple timesthis.has_added_event_listeners = true}@@ -382,4 +406,66 @@ export class StepAnimationsListing extends EventTarget {}return this.animation_search.get_selected_animation_indices()}++ /**+ * Load custom animations from imported skeleton storage+ */+ private load_custom_animations (animations: AnimationClip[]): void {+ this.animation_clips_loaded = []+ this.animation_mixer = new AnimationMixer(new Object3D())++ // Process the custom animations through the same pipeline+ const cloned_anims: AnimationClip[] = AnimationUtility.deep_clone_animation_clips(animations)++ // Clean track data+ AnimationUtility.clean_track_data(cloned_anims)++ // Apply skeleton scaling+ AnimationUtility.apply_skeleton_scale_to_position_keyframes(cloned_anims, this.skeleton_scale)++ // Add to animation clips loaded+ this.animation_clips_loaded.push(...cloned_anims.map(clip => ({+ original_animation_clip: clip,+ display_animation_clip: AnimationUtility.deep_clone_animation_clip(clip)+ })))++ console.log(`Loaded ${this.animation_clips_loaded.length} custom animations`)++ this.onAllAnimationsLoaded()+ this.play_animation(0)+ }++ private async import_animation_glb (file: File): Promise<void> {+ try {+ const new_animation_clips = await this.animation_loader.load_animations_from_file(file, this.skeleton_scale)+ if (new_animation_clips.length === 0) {+ new ModalDialog('No animations were found in that GLB file.', 'Error').show()+ return+ }++ this.animation_clips_loaded.push(...new_animation_clips)++ this.apply_rest_pose_rotation_corrections()+ this.rebuild_warped_animations()++ if (this.animation_search === null) {+ this.build_animation_clip_ui(+ this.animation_clips_loaded.map(clip => clip.display_animation_clip),+ this.theme_manager+ )+ } else {+ this.animation_search.add_animations(new_animation_clips.map(clip => clip.display_animation_clip))+ }++ this.update_filtered_animation_listing_ui()+ this.update_download_button_enabled()++ if (this.skinned_meshes_to_animate.length > 0) {+ this.play_animation(this.current_playing_index)+ }+ } catch (error) {+ console.error('Failed to import animations:', error)+ new ModalDialog('Failed to import animations from the GLB file.', 'Error').show()+ }+ }}