Import a 3D Model and automatically assign and export animations
TypeScript
Try to experiment with how exporting mirror animations will look
Scott Petrovic282aeec2422c
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.tsindex bf4e70b..a4c90a7 100644--- a/src/lib/EventListeners.ts+++ b/src/lib/EventListeners.ts@@ -255,9 +255,9 @@ export class EventListeners {this.bootstrap.ui.dom_export_button?.addEventListener('click', () => {const all_clips = this.bootstrap.animations_listing_step.animation_clips()- const animations_to_export: number[] = this.bootstrap.animations_listing_step.get_animation_indices_to_export()+ const export_selections = this.bootstrap.animations_listing_step.get_animation_export_selections()- this.bootstrap.file_export_step.set_animation_clips_to_export(all_clips, animations_to_export)+ this.bootstrap.file_export_step.set_animation_clips_to_export(all_clips, export_selections)this.bootstrap.file_export_step.export(this.bootstrap.animations_listing_step.active_skinned_meshes(),'exported-model',diff --git a/src/lib/processes/animations-listing/AnimationSearch.ts b/src/lib/processes/animations-listing/AnimationSearch.tsindex 8b624d4..e5143be 100644--- a/src/lib/processes/animations-listing/AnimationSearch.ts+++ b/src/lib/processes/animations-listing/AnimationSearch.ts@@ -3,6 +3,7 @@ import { SkeletonType } from '../../enums/SkeletonType'import { RigConfig } from '../../RigConfig'import { type AnimationWithState } from './interfaces/AnimationWithState'import { type TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair'+import { type AnimationExportSelection, type AnimationMirrorExportMode } from './interfaces/AnimationExportSelection'export class AnimationSearch extends EventTarget {private all_animations: AnimationWithState[] = []@@ -50,6 +51,7 @@ export class AnimationSearch extends EventTarget {return animations.map((pair) => {const animation_with_state = pair.display_animation_clip as unknown as AnimationWithStateanimation_with_state.isChecked = false+ animation_with_state.mirror_export_mode = 'none'animation_with_state.metadata = pair.metadata // enhanced searching/display with custom animationsreturn animation_with_state})@@ -57,7 +59,7 @@ export class AnimationSearch extends EventTarget {private setup_event_listeners (): void {this.setup_filter_listener()- this.setup_checkbox_listeners()+ this.setup_row_state_listeners()this.setup_theme_change_listener()}@@ -84,31 +86,47 @@ export class AnimationSearch extends EventTarget {})}- private setup_checkbox_listeners (): void {+ private setup_row_state_listeners (): void {if (this.animation_list_container === null) {return}- // Add event listener to the container for checkbox changes (event delegation)+ // Add event listener to the container for checkbox/radio changes (event delegation)this.animation_list_container.addEventListener('change', (event) => {const target = event.target as HTMLInputElement- if (target?.type === 'checkbox') {- this.save_current_checkbox_states()+ if (target?.type === 'checkbox' || this.is_mirror_mode_radio_input(target)) {+ if (target?.type === 'checkbox') {+ const row_element = target.closest('.anim-item, .anim-custom-item') as HTMLElement | null+ if (row_element !== null) {+ row_element.setAttribute('data-selected', target.checked ? 'true' : 'false')+ }+ }++ this.save_current_row_states()// If in "selected only" mode, re-render to remove unchecked animations immediately- if (this.show_selected_only) {+ if (this.show_selected_only && target?.type === 'checkbox') {const filter_text = this.filter_input?.value.toLowerCase() ?? ''this.render_filtered_animations(filter_text)}}// emit an event to notify other parts of the application that export options have changed- this.custom_event = new CustomEvent('export-options-changed', { detail: { selectedAnimations: this.get_selected_animation_indices() } })+ this.custom_event = new CustomEvent('export-options-changed', {+ detail: {+ selectedAnimations: this.get_selected_animation_indices(),+ exportAnimationCount: this.get_selected_export_animation_count()+ }+ })this.dispatchEvent(this.custom_event)})}- private save_current_checkbox_states (): void {+ private is_mirror_mode_radio_input (target: HTMLInputElement | null): boolean {+ return target?.type === 'radio' && target.name.startsWith('mirror-export-mode-')+ }++ private save_current_row_states (): void {if (this.animation_list_container === null) {return}@@ -122,6 +140,23 @@ export class AnimationSearch extends EventTarget {this.all_animations[animation_index].isChecked = input.checked}})++ const mirror_mode_radios = this.animation_list_container.querySelectorAll('input[type="radio"][name^="mirror-export-mode-"]')+ mirror_mode_radios.forEach((radio) => {+ const input = radio as HTMLInputElement+ if (!input.checked) {+ return+ }++ const animation_index_str = input.getAttribute('data-animation-index')+ const animation_index = animation_index_str !== null ? parseInt(animation_index_str) : NaN+ if (isNaN(animation_index) || animation_index < 0 || animation_index >= this.all_animations.length) {+ return+ }++ const mode = input.value as AnimationMirrorExportMode+ this.all_animations[animation_index].mirror_export_mode = mode+ })}/* animations that are shown on UI after filtering */@@ -193,16 +228,31 @@ export class AnimationSearch extends EventTarget {? '<span class="anim-custom-badge" title="Custom animation" aria-label="Custom animation">C</span>': ''+ const mirror_export_mode = animation_clip.mirror_export_mode ?? 'none'const 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; position: relative;">+ <div class="${is_custom_animation ? 'anim-custom-item' : 'anim-item'}" data-animation-index="${original_index}" data-selected="${was_checked ? 'true' : 'false'}">+ <button class="secondary-button play anim-play-button" data-action="play-animation" data-index="${original_index}" aria-label="Play ${this.animation_name_clean(animation_clip.name)}">${custom_animation_badge_html}- <div class="anim-preview-placeholder"${preview_data_src_attribute} style="pointer-events: none;"></div>- <label class="styled-checkbox">+ <div class="anim-preview-placeholder"${preview_data_src_attribute}></div>+ </button>++ <div class="anim-row-content">+ <label class="styled-checkbox anim-row-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>- </button>++ <fieldset class="anim-mirror-segmented" aria-label="Mirror export mode for ${this.animation_name_clean(animation_clip.name)}">+ <input type="radio" id="anim-${original_index}-mirror-none" name="mirror-export-mode-${original_index}" value="none" data-animation-index="${original_index}" ${mirror_export_mode === 'none' ? 'checked' : ''}>+ <label for="anim-${original_index}-mirror-none">None</label>++ <input type="radio" id="anim-${original_index}-mirror-mirrored" name="mirror-export-mode-${original_index}" value="mirrored" data-animation-index="${original_index}" ${mirror_export_mode === 'mirrored' ? 'checked' : ''}>+ <label for="anim-${original_index}-mirror-mirrored">Mirrored</label>++ <input type="radio" id="anim-${original_index}-mirror-both" name="mirror-export-mode-${original_index}" value="both" data-animation-index="${original_index}" ${mirror_export_mode === 'both' ? 'checked' : ''}>+ <label for="anim-${original_index}-mirror-both">Both</label>+ </fieldset>+ </div></div>`// append the entire item HTML to the DOM element@@ -243,8 +293,8 @@ export class AnimationSearch extends EventTarget {video.className = 'anim-preview'const src = placeholder.getAttribute('data-src') ?? ''video.src = src- video.width = 100- video.height = 120+ video.width = 76+ video.height = 56video.loop = truevideo.muted = truevideo.playsInline = true // tells mobile browsers to play inline instead of going fullscreen@@ -281,6 +331,35 @@ export class AnimationSearch extends EventTarget {.filter(index => index !== -1)}+ public get_selected_animation_export_selections (): AnimationExportSelection[] {+ return this.all_animations+ .map((animation, index) => {+ if (animation.isChecked !== true) {+ return null+ }++ return {+ animation_index: index,+ mirror_export_mode: animation.mirror_export_mode ?? 'none'+ } as AnimationExportSelection+ })+ .filter((selection): selection is AnimationExportSelection => selection !== null)+ }++ public get_selected_export_animation_count (): number {+ return this.all_animations.reduce((count, animation) => {+ if (animation.isChecked !== true) {+ return count+ }++ if (animation.mirror_export_mode === 'both') {+ return count + 2+ }++ return count + 1+ }, 0)+ }+public clear_filter (): void {if (this.filter_input !== null) {this.filter_input.value = ''@@ -301,8 +380,8 @@ export class AnimationSearch extends EventTarget {// Update all checkboxes in the UIthis.update_all_checkboxes_in_ui(new_state)- // Save the checkbox states to ensure they're synced with the UI- this.save_current_checkbox_states()+ // Save row state to ensure inputs are synced with backing animation state+ this.save_current_row_states()// If in "selected only" mode, re-render to update the displayed animationsif (this.show_selected_only) {@@ -311,7 +390,12 @@ export class AnimationSearch extends EventTarget {}// Emit event to notify that export options have changed- this.custom_event = new CustomEvent('export-options-changed', { detail: { selectedAnimations: this.get_selected_animation_indices() } })+ this.custom_event = new CustomEvent('export-options-changed', {+ detail: {+ selectedAnimations: this.get_selected_animation_indices(),+ exportAnimationCount: this.get_selected_export_animation_count()+ }+ })this.dispatchEvent(this.custom_event)}diff --git a/src/lib/processes/animations-listing/StepAnimationsListing.ts b/src/lib/processes/animations-listing/StepAnimationsListing.tsindex 40aa08f..6adfebe 100644--- a/src/lib/processes/animations-listing/StepAnimationsListing.ts+++ b/src/lib/processes/animations-listing/StepAnimationsListing.ts@@ -15,6 +15,7 @@ import { Utility } from '../../Utilities.ts'import { type ThemeManager } from '../../ThemeManager.ts'import { AnimationSearch } from './AnimationSearch.ts'import { type AnimationClipMetadata, type TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair.ts'+import { type AnimationExportSelection } from './interfaces/AnimationExportSelection.ts'// Note: EventTarget is a built-ininterface and do not need to import itexport class StepAnimationsListing extends EventTarget {@@ -287,7 +288,7 @@ export class StepAnimationsListing extends EventTarget {this.animation_search?.addEventListener('export-options-changed', () => {// update the count for the download buttonif (this.ui.dom_animation_count != null) {- this.ui.dom_animation_count.innerHTML = this.animation_search?.get_selected_animation_indices().length.toString() ?? '0'+ this.ui.dom_animation_count.innerHTML = this.animation_search?.get_selected_export_animation_count().toString() ?? '0'}})@@ -429,8 +430,13 @@ export class StepAnimationsListing extends EventTarget {this.ui.dom_animation_clip_list.addEventListener('click', (event) => {this.update_download_button_enabled()- if ((event.target != null) && (event.target as HTMLElement).tagName === 'BUTTON') {- const animation_index_str = (event.target as HTMLElement).getAttribute('data-index')+ if (event.target != null) {+ const play_button = (event.target as HTMLElement).closest('button[data-action="play-animation"]')+ if (play_button === null) {+ return+ }++ const animation_index_str = play_button.getAttribute('data-index')if (animation_index_str != null) {const animation_index: number = Number(animation_index_str)this.play_animation(animation_index)@@ -525,4 +531,12 @@ export class StepAnimationsListing extends EventTarget {}return this.animation_search.get_selected_animation_indices()}++ public get_animation_export_selections (): AnimationExportSelection[] {+ if (this.animation_search === null) {+ return []+ }++ return this.animation_search.get_selected_animation_export_selections()+ }}diff --git a/src/lib/processes/animations-listing/interfaces/AnimationExportSelection.ts b/src/lib/processes/animations-listing/interfaces/AnimationExportSelection.tsnew file mode 100644index 0000000..04bfcfb--- /dev/null+++ b/src/lib/processes/animations-listing/interfaces/AnimationExportSelection.ts@@ -0,0 +1,6 @@+export type AnimationMirrorExportMode = 'none' | 'mirrored' | 'both'++export interface AnimationExportSelection {+ animation_index: number+ mirror_export_mode: AnimationMirrorExportMode+}diff --git a/src/lib/processes/animations-listing/interfaces/AnimationWithState.ts b/src/lib/processes/animations-listing/interfaces/AnimationWithState.tsindex c7d32ad..104eddd 100644--- a/src/lib/processes/animations-listing/interfaces/AnimationWithState.ts+++ b/src/lib/processes/animations-listing/interfaces/AnimationWithState.ts@@ -1,8 +1,10 @@import { type AnimationClip } from 'three'import { type AnimationClipMetadata } from './TransformedAnimationClipPair'+import { type AnimationMirrorExportMode } from './AnimationExportSelection'export interface AnimationWithState extends AnimationClip {isChecked?: boolean+ mirror_export_mode?: AnimationMirrorExportModename: stringmetadata: AnimationClipMetadata}diff --git a/src/lib/processes/export-to-file/StepExportToFile.ts b/src/lib/processes/export-to-file/StepExportToFile.tsindex 9aa6777..0e26955 100644--- a/src/lib/processes/export-to-file/StepExportToFile.ts+++ b/src/lib/processes/export-to-file/StepExportToFile.ts@@ -3,21 +3,56 @@ import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter.js'import { type AnimationClip, Scene, type SkinnedMesh, type Object3D } from 'three'import { type DownloadSettings, ExportContents } from './DownloadSettings.ts'import { ExportBoneNamingService } from './ExportBoneNamingService.ts'+import { AnimationUtility } from '../animations-listing/AnimationUtility.ts'+import { type AnimationExportSelection } from '../animations-listing/interfaces/AnimationExportSelection.ts'// Note: EventTarget is a built-in interface and do not need to import itexport class StepExportToFile extends EventTarget {private readonly ui: UI = UI.getInstance()private animation_clips_to_export: AnimationClip[] = []- public set_animation_clips_to_export (all_animations_clips: AnimationClip[], animation_checkboxes: number[]): void {+ public set_animation_clips_to_export (all_animations_clips: AnimationClip[], export_selections: AnimationExportSelection[]): void {this.animation_clips_to_export = []- 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)+ export_selections.forEach((selection) => {+ const source_clip: AnimationClip | undefined = all_animations_clips[selection.animation_index]+ if (source_clip === undefined) {+ return+ }++ if (selection.mirror_export_mode === 'none') {+ this.animation_clips_to_export.push(source_clip.clone())+ return+ }++ if (selection.mirror_export_mode === 'mirrored') {+ this.animation_clips_to_export.push(this.create_mirrored_clip(source_clip))+ return+ }++ // Export both normal and mirrored variants.+ this.animation_clips_to_export.push(source_clip.clone())+ this.animation_clips_to_export.push(this.create_mirrored_clip(source_clip))})}+ private create_mirrored_clip (source_clip: AnimationClip): AnimationClip {+ const mirrored_clip = AnimationUtility.deep_clone_animation_clip(source_clip)+ mirrored_clip.name = `${mirrored_clip.name}_mirror`++ AnimationUtility.apply_animation_mirroring([+ {+ original_animation_clip: AnimationUtility.deep_clone_animation_clip(mirrored_clip),+ display_animation_clip: mirrored_clip,+ metadata: {+ source_type: 'default-library',+ tags: []+ }+ }+ ])++ return mirrored_clip+ }+public export (skinned_meshes: SkinnedMesh[],filename: string = 'exported_model',diff --git a/src/styles.css b/src/styles.cssindex bb03d50..65aa98c 100644--- a/src/styles.css+++ b/src/styles.css@@ -256,6 +256,7 @@ button.secondary-button.playmargin-right: 0;border: 1px solid var(--bg-tertiary);transform: scale(1.0);+ flex-grow: 0;}button.secondary-button.play:hover {border: 1px solid var(--text-primary);@@ -528,16 +529,14 @@ option {#animations-items{max-height: calc(100vh - 22rem);- display: inline-flex;+ display: flex;overflow: auto;;- flex-direction: row;+ flex-direction: column;margin-bottom: 1rem;background: var(--bg-alternate);border: 1px solid var(--bg-tertiary);- gap: 0.25rem;- flex-wrap: wrap;+ gap: 0.2rem;overflow-x: hidden;- width: 24rem; /* creates 3 column of animation items */}#animations-items.create-page {@@ -546,7 +545,7 @@ option {}#animations-items div {- display: inline-flex;+ display: flex;padding: 0;}/* have every other div child be a different background color */@@ -559,15 +558,15 @@ option {.anim-item, .anim-custom-item {display: flex;- flex-direction: column;- justify-content: space-between;+ flex-direction: row;+ align-items: stretch;+ justify-content: flex-start;padding: 0;transition: box-shadow 0.2s ease-in-out;- /* specify dimensions helps grid layout without jumbling elements */- width: 103px;- height: 160px;+ width: 100%;+ min-height: 64px;}.anim-custom-item {@@ -577,12 +576,104 @@ option {.anim-preview-label {- font-size: 90%;+ font-size: 88%;cursor: pointer;- line-height: 110%;+ line-height: 120%;text-align: left;}+.anim-play-button {+ margin: 0;+ border-radius: 0;+ width: 84px;+ min-width: 84px;+ height: 64px;+ border-right: 1px solid var(--bg-tertiary);+ position: relative;+ overflow: hidden;+}++.anim-play-button .anim-preview-placeholder,+.anim-play-button .anim-preview {+ width: 76px;+ height: 56px;+ margin: auto;+ border-radius: 2px;+ background: var(--bg-primary);+ object-fit: cover;+}++.anim-row-content {+ display: flex;+ flex: 1;+ flex-direction: column;+ align-items: flex-start;+ justify-content: center;+ gap: 0.2rem;+ padding: 0.2rem 0.3rem;+}++.anim-row-checkbox {+ margin: 0;+ width: 100%;+ min-width: 0;+}++.anim-row-checkbox .anim-preview-label {+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+}++.anim-mirror-segmented {+ display: inline-flex;+ align-items: center;+ border: 1px solid var(--bg-tertiary);+ border-radius: 999px;+ padding: 0.1rem;+ margin: 0 0 0 1.75rem;+ gap: 0.1rem;+ white-space: nowrap;+ flex-direction: row;+}++.anim-item[data-selected="false"] .anim-mirror-segmented,+.anim-custom-item[data-selected="false"] .anim-mirror-segmented {+ display: none;+}++.anim-mirror-segmented input[type="radio"] {+ position: absolute;+ opacity: 0;+ width: 0;+ height: 0;+ pointer-events: none;+}++.anim-mirror-segmented label {+ cursor: pointer;+ border-radius: 999px;+ padding: 0.2rem 0.45rem;+ font-size: 0.74rem;+ color: var(--text-alternate);+ transition: background-color 0.2s ease, color 0.2s ease;+}++.anim-mirror-segmented input[type="radio"]:checked + label {+ background: var(--button-primary-bg);+ color: var(--text-primary);+}++.anim-mirror-segmented label:hover {+ background: color-mix(in srgb, var(--bg-tertiary) 80%, transparent);+}++@media (max-width: 980px) {+ #animations-items {+ width: 100%;+ }+}+input[type="file"] {display: none;