Import a 3D Model and automatically assign and export animations
TypeScript
Start adding metadata. Have custom animations appear slightly different for now
Scott Petrovic8449e2e7cf99
diff --git a/src/lib/processes/animations-listing/AnimationLoader.ts b/src/lib/processes/animations-listing/AnimationLoader.tsindex 2e73d18..4a199f1 100644--- a/src/lib/processes/animations-listing/AnimationLoader.ts+++ b/src/lib/processes/animations-listing/AnimationLoader.ts@@ -2,7 +2,7 @@ import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'import { type AnimationClip } from 'three'import { AnimationUtility } from './AnimationUtility.ts'import { SkeletonType } from '../../enums/SkeletonType.ts'-import { type TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair.ts'+import { type AnimationClipMetadata, type TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair.ts'import { LoadError, NoAnimationsError } from './AnimationImportErrors.ts'export interface AnimationLoadProgress {@@ -24,6 +24,13 @@ export class AnimationLoader extends EventTarget {private completed_files: number = 0private total_files: number = 0+ private create_default_metadata (): AnimationClipMetadata {+ return {+ source_type: 'default-library',+ tags: []+ }+ }+/*** Sets the base path for animation files*/@@ -78,7 +85,14 @@ export class AnimationLoader extends EventTarget {this.completed_files++// Process the loaded animations- const processed_clips = this.process_loaded_animations(gltf.animations as AnimationClip[], skeleton_scale)+ const processed_clips = this.process_loaded_animations(+ gltf.animations as AnimationClip[],+ skeleton_scale,+ {+ source_type: 'default-library',+ tags: []+ }+ )loaded_clips.push(...processed_clips)completed_loads++@@ -136,7 +150,8 @@ export class AnimationLoader extends EventTarget {*/public async load_animations_from_file (file: File,- skeleton_scale: number = 1.0+ skeleton_scale: number = 1.0,+ metadata_override: Partial<AnimationClipMetadata> = {}): Promise<TransformedAnimationClipPair[]> {const file_url = URL.createObjectURL(file)const file_total = file.size > 0 ? file.size : 1@@ -166,7 +181,11 @@ export class AnimationLoader extends EventTarget {this.completed_files = 1this.emit_enhanced_progress(file.name, file_total, file_total)- const processed_clips = this.process_loaded_animations(animations, skeleton_scale)+ const processed_clips = this.process_loaded_animations(+ animations,+ skeleton_scale,+ metadata_override+ )resolve(processed_clips)} catch (error) {// Emit final progress to hide the loader UI@@ -229,7 +248,8 @@ export class AnimationLoader extends EventTarget {*/public process_loaded_animations (raw_animations: AnimationClip[],- skeleton_scale: number+ skeleton_scale: number,+ metadata_override: Partial<AnimationClipMetadata> = {}): TransformedAnimationClipPair[] {// Deep clone the animations to avoid modifying originalsconst cloned_animations = AnimationUtility.deep_clone_animation_clips(raw_animations)@@ -243,7 +263,11 @@ export class AnimationLoader extends EventTarget {// Create the transformed pairsreturn cloned_animations.map(clip => ({original_animation_clip: clip,- display_animation_clip: AnimationUtility.deep_clone_animation_clip(clip)+ display_animation_clip: AnimationUtility.deep_clone_animation_clip(clip),+ metadata: {+ ...this.create_default_metadata(),+ ...metadata_override+ }}))}diff --git a/src/lib/processes/animations-listing/AnimationSearch.ts b/src/lib/processes/animations-listing/AnimationSearch.tsindex abe2a63..df0b251 100644--- a/src/lib/processes/animations-listing/AnimationSearch.ts+++ b/src/lib/processes/animations-listing/AnimationSearch.ts@@ -1,7 +1,7 @@-import { type AnimationClip } from 'three'import { type ThemeManager } from '../../ThemeManager'import { SkeletonType } from '../../enums/SkeletonType'import { type AnimationWithState } from './interfaces/AnimationWithState'+import { type TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair'export class AnimationSearch extends EventTarget {private all_animations: AnimationWithState[] = []@@ -23,14 +23,14 @@ export class AnimationSearch extends EventTarget {this.setup_event_listeners()}- public initialize_animations (animations: AnimationClip[]): void {+ public initialize_animations (animations: TransformedAnimationClipPair[]): void {// Convert to animations with state trackingthis.all_animations = this.map_animations_to_state(animations)this.render_filtered_animations('')}- public add_animations (animations: AnimationClip[]): void {+ public add_animations (animations: TransformedAnimationClipPair[]): void {const new_animations = this.map_animations_to_state(animations)this.all_animations.push(...new_animations)@@ -38,10 +38,17 @@ export class AnimationSearch extends EventTarget {this.render_filtered_animations(filter_text)}- private map_animations_to_state (animations: AnimationClip[]): AnimationWithState[] {- return animations.map((clip) => {- const animation_with_state = clip as unknown as AnimationWithState+ /**+ * Convert the animation listing data to a format+ * that works for what the UI needs+ * @param animations The list of animations to convert to a format with state for the UI+ * @returns A list of animations with state for the UI to track which animations are selected for export and filtering+ */+ private map_animations_to_state (animations: TransformedAnimationClipPair[]): AnimationWithState[] {+ return animations.map((pair) => {+ const animation_with_state = pair.display_animation_clip as unknown as AnimationWithStateanimation_with_state.isChecked = false+ animation_with_state.metadata = pair.metadata // enhanced searching/display with custom animationsreturn animation_with_state})}@@ -55,7 +62,7 @@ export class AnimationSearch extends EventTarget {private setup_theme_change_listener (): void {// rebuild animation previews so we have the correct themethis.theme_manager.addEventListener('theme-changed', (new_theme) => {- this.render_filtered_animations(this.filter_input?.value || '')+ this.render_filtered_animations(this.filter_input?.value ?? '')})}@@ -124,7 +131,6 @@ export class AnimationSearch extends EventTarget {return animation.name.toLowerCase().includes(filter_text)})-// Clear and rebuild the animation listthis.animation_list_container.innerHTML = ''@@ -134,7 +140,6 @@ export class AnimationSearch extends EventTarget {return}-this.filtered_animations_list.forEach((animation_clip) => {if (this.animation_list_container == null) {return@@ -174,18 +179,29 @@ export class AnimationSearch extends EventTarget {const anim_name: string = animation_clip.nameconst theme_name: string = this.theme_manager.get_current_theme()+ const is_custom_animation = animation_clip.metadata?.source_type === 'custom-import'++ // build out inner HTML for animation item+ let animation_entry_html = `<div class="${is_custom_animation ? 'anim-custom-item' : 'anim-item'}">+ <button class="secondary-button play" data-index="${original_index}" style="display: flex; flex-direction:column">`++ // custom animations won't have a video preview, so make it appear a bit different+ if (is_custom_animation) {+ animation_entry_html += '<div class="anim-preview-placeholder" style="pointer-events: none;">'+ } else {+ animation_entry_html += `<div class="anim-preview-placeholder" data-src="../animpreviews/${preview_folder}/${theme_name}_${anim_name}.webm" style="pointer-events: none;">`+ }++ animation_entry_html += `<div class="anim-item">+ <button class="secondary-button play" data-index="${original_index}" style="display: flex; flex-direction:column"></button>+ <label class="styled-checkbox">+ <input type="checkbox" name="${animation_clip.name}" value="${original_index}" ${checked_attribute}>+ <span class="anim-preview-label">${this.animation_name_clean(animation_clip.name)}</span>+ </label>+ </div>`- // Use a placeholder for the video preview, to be replaced by IntersectionObserver- this.animation_list_container.innerHTML +=- `<div class="anim-item">- <button class="secondary-button play" data-index="${original_index}" style="display: flex; flex-direction:column">- <div class="anim-preview-placeholder" data-src="../animpreviews/${preview_folder}/${theme_name}_${anim_name}.webm" style="pointer-events: none;"></div>- </button>- <label class="styled-checkbox">- <input type="checkbox" name="${animation_clip.name}" value="${original_index}" ${checked_attribute}>- <span class="anim-preview-label">${this.animation_name_clean(animation_clip.name)}</span>- </label>- </div>`+ // append the entire item HTML to the DOM element+ this.animation_list_container.innerHTML += animation_entry_html})// only so many WebM videos can be playing at the same timediff --git a/src/lib/processes/animations-listing/CustomAnimationImporter.ts b/src/lib/processes/animations-listing/CustomAnimationImporter.tsindex 0d94943..ea3a316 100644--- a/src/lib/processes/animations-listing/CustomAnimationImporter.ts+++ b/src/lib/processes/animations-listing/CustomAnimationImporter.ts@@ -4,7 +4,7 @@ import { type AnimationClip, type SkinnedMesh } from 'three'import { type AnimationLoader } from './AnimationLoader.ts'import { NoAnimationsError, IncompatibleSkeletonError, LoadError } from './AnimationImportErrors.ts'import CustomAnimationValidation from './CustomAnimationValidation.ts'-import { type TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair.ts'+import { AnimationClipMetadata, type TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair.ts'/*** Handles the importing of custom animations from GLB files.@@ -107,10 +107,18 @@ export class CustomAnimationImporter extends EventTarget {}private async import_animation_glb (file: File): Promise<{ success: boolean, clipCount: number }> {+ // tell the animation data these are custom animations to help differentiate for custom video previews+ // tags are unused right now, but I want to eventually use them to help filtering+ const metadata_override: Partial<AnimationClipMetadata> = {+ source_type: 'custom-import',+ tags: []+ }+try {const new_animation_clips = await this.animation_loader.load_animations_from_file(file,- this.skeleton_scale+ this.skeleton_scale,+ metadata_override)// Validate custom animations against our target skeletondiff --git a/src/lib/processes/animations-listing/StepAnimationsListing.ts b/src/lib/processes/animations-listing/StepAnimationsListing.tsindex ac154fd..3581c3f 100644--- a/src/lib/processes/animations-listing/StepAnimationsListing.ts+++ b/src/lib/processes/animations-listing/StepAnimationsListing.ts@@ -13,7 +13,7 @@ import { SkeletonType } from '../../enums/SkeletonType.ts'import { Utility } from '../../Utilities.ts'import { type ThemeManager } from '../../ThemeManager.ts'import { AnimationSearch } from './AnimationSearch.ts'-import { type TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair.ts'+import { type AnimationClipMetadata, type TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair.ts'// Note: EventTarget is a built-ininterface and do not need to import itexport class StepAnimationsListing extends EventTarget {@@ -142,6 +142,18 @@ export class StepAnimationsListing extends EventTarget {return this.animation_clips_loaded.map(clip => clip.display_animation_clip)}+ public get_animation_metadata (index: number): AnimationClipMetadata | null {+ if (index < 0 || index >= this.animation_clips_loaded.length) {+ return null+ }++ return this.animation_clips_loaded[index].metadata+ }++ public is_animation_custom (index: number): boolean {+ return this.get_animation_metadata(index)?.source_type === 'custom-import'+ }+public load_and_apply_default_animation_to_skinned_mesh (final_skinned_meshes: SkinnedMesh[]): void {this.skinned_meshes_to_animate = final_skinned_meshes@@ -182,7 +194,7 @@ export class StepAnimationsListing extends EventTarget {// create user interface with all available animation clipsthis.build_animation_clip_ui(- this.animation_clips_loaded.map(clip => clip.display_animation_clip),+ this.animation_clips_loaded,this.theme_manager)@@ -392,7 +404,7 @@ export class StepAnimationsListing extends EventTarget {this.play_animation(this.current_playing_index)}- public build_animation_clip_ui (animation_clips_to_load: AnimationClip[], theme_manager: ThemeManager): void {+ public build_animation_clip_ui (animation_clips_to_load: TransformedAnimationClipPair[], theme_manager: ThemeManager): void {// Initialize AnimationSearch if not already done// we could switch skeleton types using navigation, so need to re-create in case this happensthis.animation_search = new AnimationSearch('animation-filter', 'animations-items', theme_manager, this.skeleton_type)diff --git a/src/lib/processes/animations-listing/interfaces/AnimationWithState.ts b/src/lib/processes/animations-listing/interfaces/AnimationWithState.tsindex e5b322e..c7d32ad 100644--- a/src/lib/processes/animations-listing/interfaces/AnimationWithState.ts+++ b/src/lib/processes/animations-listing/interfaces/AnimationWithState.ts@@ -1,6 +1,8 @@-import { type AnimationClip } from "three"+import { type AnimationClip } from 'three'+import { type AnimationClipMetadata } from './TransformedAnimationClipPair'export interface AnimationWithState extends AnimationClip {isChecked?: booleanname: string+ metadata: AnimationClipMetadata}diff --git a/src/lib/processes/animations-listing/interfaces/TransformedAnimationClipPair.ts b/src/lib/processes/animations-listing/interfaces/TransformedAnimationClipPair.tsindex 152adff..534d827 100644--- a/src/lib/processes/animations-listing/interfaces/TransformedAnimationClipPair.ts+++ b/src/lib/processes/animations-listing/interfaces/TransformedAnimationClipPair.ts@@ -1,4 +1,11 @@-import { type AnimationClip } from "three"+import { type AnimationClip } from 'three'++export type AnimationSourceType = 'default-library' | 'custom-import'++export interface AnimationClipMetadata {+ source_type: AnimationSourceType+ tags: string[]+}export interface TransformedAnimationClipPair {/**@@ -14,4 +21,9 @@ export interface TransformedAnimationClipPair {* and downloaded by the user.*/display_animation_clip: AnimationClip++ /**+ * Extendable metadata for this animation clip.+ */+ metadata: AnimationClipMetadata}diff --git a/src/marketing/marketing.css b/src/marketing/marketing.cssindex 28b1003..30161d2 100644--- a/src/marketing/marketing.css+++ b/src/marketing/marketing.css@@ -39,5 +39,5 @@ button.active-button {}#animation-listing-count {- top: 1.4rem;+ top: 1.87rem;}\ No newline at end of filediff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.tsindex c10c110..231d2d1 100644--- a/src/retarget/RetargetAnimationListing.ts+++ b/src/retarget/RetargetAnimationListing.ts@@ -122,9 +122,7 @@ export class RetargetAnimationListing extends EventTarget {})// Build animation UI- this.build_animation_clip_ui(- this.animation_clips_loaded.map(clip => clip.display_animation_clip)- )+ this.build_animation_clip_ui(this.animation_clips_loaded)// Update animation selection count when selections changethis.animation_search?.addEventListener('export-options-changed', () => {@@ -144,7 +142,7 @@ export class RetargetAnimationListing extends EventTarget {console.log(`Loaded ${this.animation_clips_loaded.length} animations for retargeting`)}- private build_animation_clip_ui (animation_clips: AnimationClip[]): void {+ private build_animation_clip_ui (animation_clips: TransformedAnimationClipPair[]): void {// Initialize AnimationSearch with the loaded clipsthis.animation_search = new AnimationSearch('animation-filter',diff --git a/src/retarget/retarget.css b/src/retarget/retarget.cssindex f8a3834..43585e2 100644--- a/src/retarget/retarget.css+++ b/src/retarget/retarget.css@@ -118,7 +118,7 @@ h1 {}#animation-listing-count {- top: 1.4rem;+ top: 1.87rem;}#auto-bone-map-match {diff --git a/src/styles.css b/src/styles.cssindex 46be741..a36fcb8 100644--- a/src/styles.css+++ b/src/styles.css@@ -407,7 +407,7 @@ option {-.anim-item {+.anim-item, .anim-custom-item {display: flex;flex-direction: column;justify-content: space-between;@@ -420,6 +420,12 @@ option {height: 160px;}+.anim-custom-item {+ background: var(--bg-primary);+ border: 1px solid var(--bg-tertiary);+}++.anim-preview-label {font-size: 90%;cursor: pointer;