Import a 3D Model and automatically assign and export animations
TypeScript
Make download settings consistent between all the pages by turning download button into more of a mount to populate from typescript
Scott Petrovica4376db9fa90
diff --git a/src/Mesh2MotionEngine.ts b/src/Mesh2MotionEngine.tsindex 27aa4c3..80a1c0a 100644--- a/src/Mesh2MotionEngine.ts+++ b/src/Mesh2MotionEngine.ts@@ -705,6 +705,11 @@ export class Mesh2MotionEngine {DOMUtilities.populate_animation_player(animation_player_mount)}+ const download_control_mount = document.querySelector('#download-control-mount')+ if (download_control_mount instanceof HTMLElement) {+ DOMUtilities.populate_download_control(download_control_mount)+ }+const settings_dropdown_mount = document.querySelector('#settings-dropdown-mount')if (settings_dropdown_mount instanceof HTMLElement) {DOMUtilities.populate_settings_dropdown(settings_dropdown_mount)diff --git a/src/create.html b/src/create.htmlindex 6d159ff..4e78348 100644--- a/src/create.html+++ b/src/create.html@@ -454,97 +454,7 @@style="display: none"/>- <div class="download-combo">- <button- id="export-button"- data-tippy-content="Exporting will combine all selected animations into a single downloadable file."- >- <span class="button-icon-group">- <img src="images/icons/download.svg" alt="Download" width="16" height="16" />-- <span- >Download- <span id="animation-selection-count">0</span></span- >- </span>- </button>-- <div- id="download-settings-popup"- class="download-settings-popup-container"- >- <button- id="download-settings-toggle"- class="download-split-toggle"- type="button"- aria-haspopup="dialog"- aria-expanded="false"- aria-controls="download-settings"- data-tippy-content="Download options"- aria-label="Open download options"- >- <img src="images/icons/arrow-dropdown.svg" alt="dropdown" width="16" height="16" />- </button>-- <div- id="download-settings"- class="download-settings-panel"- role="dialog"- aria-label="Download settings"- hidden- >- <span class="download-settings-label">Format</span>- <fieldset- id="download-export-format-group"- class="toggle"- aria-label="Export format"- >- <input- type="radio"- id="export-format-glb"- name="export-format"- value="glb"- checked- />- <label for="export-format-glb">GLB</label>-- <input- type="radio"- id="export-format-fbx"- name="export-format"- value="fbx"- />- <label for="export-format-fbx">FBX</label>- </fieldset>-- <span class="download-settings-label">Exported Bone Names</span>- <fieldset- id="download-bone-naming-group"- class="toggle"- aria-label="Bone naming structure"- >- <input- type="radio"- id="bone-naming-default"- name="bone-naming-structure"- value="default"- checked- />- <label for="bone-naming-default">Default</label>-- <input- type="radio"- id="bone-naming-mixamo"- name="bone-naming-structure"- value="mixamo"- />- <label for="bone-naming-mixamo">Mixamo</label>- </fieldset>- </div>- </div>- </div>-- <a id="download-hidden-link" href="#" style="display: none"></a>+ <div id="download-control-mount"></div></div></div></span>diff --git a/src/index.html b/src/index.htmlindex 363e923..ceaffe5 100644--- a/src/index.html+++ b/src/index.html@@ -163,70 +163,7 @@</button><input id="import-animations-input" type="file" accept=".glb" multiple style="display:none" />- <div class="download-combo">- <button id="export-button" data-tippy-content="Exporting will combine all selected animations into a single downloadable file.">- <span class="button-icon-group">- <img src="images/icons/download.svg" alt="Download" width="16" height="16" />- <span>Download <span id="animation-selection-count">0</span></span>- </span>- </button>-- <div id="download-settings-popup" class="download-settings-popup-container">- <button- id="download-settings-toggle"- class="download-split-toggle"- type="button"- aria-haspopup="dialog"- aria-expanded="false"- aria-controls="download-settings"- data-tippy-content="Download options"- aria-label="Open download options"- >-- <img src="images/icons/arrow-dropdown.svg" alt="dropdown" width="16" height="16" />-- </button>-- <div id="download-settings" class="download-settings-panel" role="dialog" aria-label="Download settings" hidden>- <span class="download-settings-header">Download Options</span>-- <div id="download-bone-naming-section" class="options-container">- <span class="download-settings-label">Bone Names</span>- <fieldset id="download-bone-naming-group" class="toggle" aria-label="Bone naming structure">- <input type="radio" id="bone-naming-default" name="bone-naming-structure" value="default" checked>- <label for="bone-naming-default">Default</label>-- <input type="radio" id="bone-naming-mixamo" name="bone-naming-structure" value="mixamo">- <label for="bone-naming-mixamo">Mixamo</label>- </fieldset>- </div>-- <div class="options-container">- <span class="download-settings-label">Format</span>- <fieldset id="download-export-format-group" class="toggle" aria-label="Export format">- <input type="radio" id="export-format-glb" name="export-format" value="glb" checked>- <label for="export-format-glb">GLB</label>-- <input type="radio" id="export-format-fbx" name="export-format" value="fbx">- <label for="export-format-fbx">FBX</label>- </fieldset>- </div>-- <div class="options-container">- <span class="download-settings-label">Contents</span>- <fieldset id="download-export-contents-group" class="toggle" aria-label="Export contents">- <input type="radio" id="export-contents-full" name="export-contents" value="full" checked>- <label for="export-contents-full">Default</label>-- <input type="radio" id="export-contents-skeleton" name="export-contents" value="skeleton">- <label for="export-contents-skeleton">Skeleton + Animations</label>- </fieldset>- </div>- </div>- </div>- </div>-- <a id="download-hidden-link" href="#" style="display:none"></a>+ <div id="download-control-mount"></div></div>diff --git a/src/lib/DOMUtilities.ts b/src/lib/DOMUtilities.tsindex 6f1143d..9ccd703 100644--- a/src/lib/DOMUtilities.ts+++ b/src/lib/DOMUtilities.ts@@ -20,6 +20,12 @@ interface TopNavLinksConfig {github_icon_src: string}+interface DownloadControlConfig {+ export_button_id: string+ download_icon_src: string+ tooltip: string+}+export class DOMUtilities {static readonly top_nav_links: TopNavLinksConfig = {support_href: 'https://support.mesh2motion.org',@@ -44,6 +50,12 @@ export class DOMUtilities {solid_background_enabled: false}+ static readonly download_control_defaults: DownloadControlConfig = {+ export_button_id: 'export-button',+ download_icon_src: 'images/icons/download.svg',+ tooltip: 'Exporting will combine all selected animations into a single downloadable file.'+ }+/*** Render shared top-right navigation links into the provided mount element.*/@@ -129,6 +141,114 @@ export class DOMUtilities {`}+ /**+ * Render the shared export/download button, settings split toggle, and hidden link.+ * Page-specific button IDs and icon paths are resolved from the current route.+ */+ static populate_download_control (mount: HTMLElement): void {+ const control_config = DOMUtilities.get_download_control_config()++ mount.innerHTML = `+ <div class="download-combo">+ <button id="${control_config.export_button_id}" data-tippy-content="${control_config.tooltip}">+ <span class="button-icon-group">+ <img src="${control_config.download_icon_src}" alt="Download" width="16" height="16" />+ <span>Download <span id="animation-selection-count">0</span></span>+ </span>+ </button>++ ${DOMUtilities.get_download_settings_markup()}+ </div>++ <a id="download-hidden-link" href="#" style="display:none"></a>+ `+ }++ private static get_download_settings_markup (): string {+ const dropdown_icon_src = DOMUtilities.get_download_settings_icon_src()++ return `+ <div id="download-settings-popup" class="download-settings-popup-container">+ <button+ id="download-settings-toggle"+ class="download-split-toggle"+ type="button"+ aria-haspopup="dialog"+ aria-expanded="false"+ aria-controls="download-settings"+ data-tippy-content="Download options"+ aria-label="Open download options"+ >+ <img src="${dropdown_icon_src}" alt="dropdown" width="16" height="16" />+ </button>++ <div id="download-settings" class="download-settings-panel" role="dialog" aria-label="Download settings" hidden>+ <span class="download-settings-header">Download Options</span>++ <div id="download-bone-naming-section" class="options-container">+ <span class="download-settings-label">Bone Names</span>+ <fieldset id="download-bone-naming-group" class="toggle" aria-label="Bone naming structure">+ <input type="radio" id="bone-naming-default" name="bone-naming-structure" value="default" checked>+ <label for="bone-naming-default">Default</label>++ <input type="radio" id="bone-naming-mixamo" name="bone-naming-structure" value="mixamo">+ <label for="bone-naming-mixamo">Mixamo</label>+ </fieldset>+ </div>++ <div class="options-container">+ <span class="download-settings-label">Format</span>+ <fieldset id="download-export-format-group" class="toggle" aria-label="Export format">+ <input type="radio" id="export-format-glb" name="export-format" value="glb" checked>+ <label for="export-format-glb">GLB</label>++ <input type="radio" id="export-format-fbx" name="export-format" value="fbx">+ <label for="export-format-fbx">FBX</label>+ </fieldset>+ </div>++ <div class="options-container">+ <span class="download-settings-label">Contents</span>+ <fieldset id="download-export-contents-group" class="toggle" aria-label="Export contents">+ <input type="radio" id="export-contents-full" name="export-contents" value="full" checked>+ <label for="export-contents-full">Default</label>++ <input type="radio" id="export-contents-skeleton" name="export-contents" value="skeleton">+ <label for="export-contents-skeleton">Skeleton + Animations</label>+ </fieldset>+ </div>+ </div>+ </div>+ `+ }++ private static get_download_control_config (): DownloadControlConfig {+ const defaults = DOMUtilities.download_control_defaults++ if (DOMUtilities.is_retarget_page()) {+ return {+ export_button_id: 'export-retargeting-button',+ download_icon_src: '../images/icons/download.svg',+ tooltip: defaults.tooltip+ }+ }++ return defaults+ }++ private static get_download_settings_icon_src (): string {+ if (DOMUtilities.is_retarget_page()) {+ return '../images/icons/arrow-dropdown.svg'+ }++ return 'images/icons/arrow-dropdown.svg'+ }++ private static is_retarget_page (): boolean {+ const path_name = window.location.pathname.toLowerCase()+ return path_name.includes('/retarget/')+ }+/*** Render shared nav settings dropdown markup into the provided mount element.*/diff --git a/src/retarget/RetargetAnimationListing.ts b/src/retarget/RetargetAnimationListing.tsindex b235902..72da041 100644--- a/src/retarget/RetargetAnimationListing.ts+++ b/src/retarget/RetargetAnimationListing.ts@@ -7,6 +7,8 @@ import { type TransformedAnimationClipPair } from '../lib/processes/animations-limport { AnimationRetargetService } from './AnimationRetargetService.ts'import { StepExportRetargetedAnimations } from './steps/StepExportRetargetedAnimations.ts'import { UI } from '../lib/UI.ts'+import { DownloadSettings } from '../lib/processes/export-to-file/DownloadSettings.ts'+import { ModalDialog } from '../lib/ModalDialog.ts'/*** RetargetAnimationListing - Handles animation listing and playback specifically for retargeting workflow@@ -17,6 +19,7 @@ export class RetargetAnimationListing extends EventTarget {private readonly animation_player: AnimationPlayerprivate readonly animation_loader: AnimationLoader = new AnimationLoader()private readonly step_export_retargeted_animations: StepExportRetargetedAnimations = new StepExportRetargetedAnimations()+ private readonly download_settings: DownloadSettingsprivate animation_clips_loaded: TransformedAnimationClipPair[] = []private animation_mixer: AnimationMixer = new AnimationMixer(new Object3D())private readonly ui: UI = UI.getInstance()@@ -29,9 +32,10 @@ export class RetargetAnimationListing extends EventTarget {private export_button: HTMLButtonElement | null = null- constructor (theme_manager: ThemeManager) {+ constructor (theme_manager: ThemeManager, download_settings: DownloadSettings) {super()this.theme_manager = theme_manager+ this.download_settings = download_settingsthis.animation_player = new AnimationPlayer()}@@ -233,7 +237,11 @@ export class RetargetAnimationListing extends EventTarget {)// configure the export out step with retargeting info- this.step_export_retargeted_animations.export('retargeted_animations')+ this.step_export_retargeted_animations.export('retargeted_animations', this.download_settings)+ .catch((error: unknown) => {+ const error_message = error instanceof Error ? error.message : 'Unknown export error'+ new ModalDialog('Retarget export failed', error_message).show()+ })})// event listener for animation view mode tabs (Library vs Selected)diff --git a/src/retarget/index.html b/src/retarget/index.htmlindex 1553c6d..d798704 100644--- a/src/retarget/index.html+++ b/src/retarget/index.html@@ -8,6 +8,7 @@<!-- Stylesheets--><link href="../styles.css" rel="stylesheet" crossorigin="anonymous" />+ <link rel="stylesheet" href="../radio-button-group.css" crossorigin="anonymous" /><link rel="stylesheet" href="../animation-player.css" crossorigin="anonymous" /><link rel="stylesheet" href="../settings-dropdown.css" crossorigin="anonymous" /><link rel="stylesheet" href="./retarget.css" crossorigin="anonymous" />@@ -193,16 +194,7 @@<div style="display: flex; gap: 0.5rem;"><button class="no-style-button" id="back_to_bone_map_button" >‹ Back</button>- <!-- I renamed the button ID since we need to process the animations differently -->- <button id="export-retargeting-button" data-tippy-content="Exporting will combine all animations into a single GLB file.">- <span class="button-icon-group">-- <img src="../images/icons/download.svg" alt="Help" width="16" height="16" />-- <span>Download <span id="animation-selection-count">0</span></span>- </span>- </button>- <a id="download-hidden-link" href="#" style="display:none"></a>+ <div id="download-control-mount"></div></div>diff --git a/src/retarget/retarget.ts b/src/retarget/retarget.tsindex c85c816..82746ab 100644--- a/src/retarget/retarget.ts+++ b/src/retarget/retarget.ts@@ -88,6 +88,7 @@ class RetargetModule {// animation service keeps track of shared data across classesAnimationRetargetService.getInstance().set_source_armature(source_armature)AnimationRetargetService.getInstance().set_skeleton_type(skeleton_type)+ this.mesh2motion_engine.download_settings.update_download_settings_ui_visibility(skeleton_type)console.log('Source skeleton loaded:', skeleton_type)@@ -140,9 +141,13 @@ class RetargetModule {// load the animation listing step and start itthis.animation_listing_step = new RetargetAnimationListing(- this.mesh2motion_engine.get_theme_manager()+ this.mesh2motion_engine.get_theme_manager(),+ this.mesh2motion_engine.download_settings)this.animation_listing_step.begin()+ this.mesh2motion_engine.download_settings.update_download_settings_ui_visibility(+ AnimationRetargetService.getInstance().get_skeleton_type()+ )this.mesh2motion_engine.show_animation_player(true)this.animation_listing_step.load_and_apply_default_animation_to_skinned_mesh()diff --git a/src/retarget/steps/StepExportRetargetedAnimations.ts b/src/retarget/steps/StepExportRetargetedAnimations.tsindex 277ed0e..563e135 100644--- a/src/retarget/steps/StepExportRetargetedAnimations.ts+++ b/src/retarget/steps/StepExportRetargetedAnimations.ts@@ -1,11 +1,17 @@-import { type Scene, type AnimationClip } from 'three'+import { Group, Scene, type AnimationClip, type Object3D, type SkinnedMesh } from 'three'import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter'+import { FBXExporter } from '@comfyorg/fbx-exporter-three'import { AnimationRetargetService } from '../AnimationRetargetService'import { AnimationUtility } from '../../lib/processes/animations-listing/AnimationUtility'import { type AnimationExportSelection } from '../../lib/processes/animations-listing/interfaces/AnimationExportSelection'+import { type DownloadSettings, ExportContents, ExportFormat } from '../../lib/processes/export-to-file/DownloadSettings'+import { ExportBoneNamingService } from '../../lib/processes/export-to-file/ExportBoneNamingService'++const FBX_EXPORT_SCALE = 100export class StepExportRetargetedAnimations extends EventTarget {public animation_clips_to_export: AnimationClip[] = []+ private readonly fbx_exporter: FBXExporter = new FBXExporter()public set_animation_clips_to_export (all_animations_clips: AnimationClip[], export_selections: AnimationExportSelection[]): void {this.animation_clips_to_export = []@@ -31,25 +37,91 @@ export class StepExportRetargetedAnimations extends EventTarget {})}- public export (filename = 'exported_model'): void {+ public async export (filename = 'exported_model', download_settings: DownloadSettings): Promise<void> {if (this.animation_clips_to_export.length === 0) {console.log('ERROR: No animation clips added to export')- return+ throw new Error('No animation clips added to export')}// Retarget all animation clips before export- let retargeted_clips: AnimationClip[] = []- retargeted_clips = this.animation_clips_to_export.map((clip) =>- AnimationRetargetService.getInstance().retarget_animation_clip(clip)- )+ const retargeted_clips: AnimationClip[] = this.animation_clips_to_export.map((clip) => {+ const retargeted_clip = AnimationRetargetService.getInstance().retarget_animation_clip(clip)+ retargeted_clip.name = retargeted_clip.name.replace(/\s+/g, '_')+ return retargeted_clip+ })console.log('Retargeted animation clips:', retargeted_clips)- const target_rig_scene: Scene = AnimationRetargetService.getInstance().get_target_armature()- this.export_glb(target_rig_scene, retargeted_clips, filename)- .then(() => {- console.log('Exported GLB successfully')+ const target_skinned_meshes: SkinnedMesh[] = AnimationRetargetService.getInstance().get_target_skinned_meshes()+ await this.export_with_settings(target_skinned_meshes, retargeted_clips, filename, download_settings)+ }++ public async export_with_settings (+ skinned_meshes: SkinnedMesh[],+ animations_to_export: AnimationClip[],+ file_name: string,+ download_settings: DownloadSettings+ ): Promise<void> {+ const export_scene = new Scene()+ const export_root_group = new Group()+ export_root_group.name = 'retarget-fbx-export-root'+ export_root_group.scale.setScalar(FBX_EXPORT_SCALE)++ const restore_bone_names = ExportBoneNamingService.apply_download_settings(+ skinned_meshes,+ animations_to_export,+ download_settings.bone_naming_structure()+ )++ const skeleton_only = download_settings.export_contents() === ExportContents.Skeleton+ const objects_to_export: Object3D[] = skeleton_only+ ? Array.from(new Set(skinned_meshes.map((mesh) => mesh.skeleton.bones[0])))+ : skinned_meshes++ const original_parents = new Map<Object3D, Object3D | null>()++ objects_to_export.forEach((object_to_export) => {+ original_parents.set(object_to_export, object_to_export.parent)++ if (download_settings.export_format() === ExportFormat.FBX) {+ export_root_group.add(object_to_export)+ return+ }++ export_scene.add(object_to_export)+ })++ if (download_settings.export_format() === ExportFormat.FBX) {+ export_scene.add(export_root_group)+ }++ try {+ await this.export_scene(export_scene, animations_to_export, file_name, download_settings.export_format())+ } finally {+ objects_to_export.forEach((exported_object) => {+ const original_parent = original_parents.get(exported_object)+ if (original_parent != null) {+ original_parent.add(exported_object)+ } else {+ export_scene.remove(exported_object)+ }})- .catch((error) => { console.log('Error exporting GLB:', error) })++ restore_bone_names()+ }+ }++ public async export_scene (+ exported_scene: Scene,+ animations_to_export: AnimationClip[],+ file_name: string,+ export_format: ExportFormat+ ): Promise<void> {+ if (export_format === ExportFormat.FBX) {+ await this.export_fbx(exported_scene, animations_to_export, file_name)+ return+ }++ await this.export_glb(exported_scene, animations_to_export, file_name)}public async export_glb (exported_scene: Scene, animations_to_export: AnimationClip[], file_name: string): Promise<void> {@@ -84,6 +156,45 @@ export class StepExportRetargetedAnimations extends EventTarget {})}+ public async export_fbx (exported_scene: Scene, animations_to_export: AnimationClip[], file_name: string): Promise<void> {+ const export_scene = new Group()+ export_scene.name = 'retarget-fbx-export-root'+ export_scene.scale.setScalar(FBX_EXPORT_SCALE)++ const original_parents = new Map<Group | Scene, Group | Scene | null>()+ const root_children = [...exported_scene.children]++ root_children.forEach((child) => {+ original_parents.set(child as Group | Scene, child.parent as Group | Scene | null)+ export_scene.add(child)+ })++ export_scene.animations = animations_to_export++ try {+ const result = await this.fbx_exporter.parseAsync(export_scene, {+ preset: 'unreal',+ includeAnimations: true,+ animations: animations_to_export,+ onlyVisible: false,+ embedTextures: true+ })++ this.save_uint8_array(result, `${file_name}.fbx`)+ } finally {+ export_scene.animations = []++ root_children.forEach((child) => {+ const original_parent = original_parents.get(child as Group | Scene)+ if (original_parent !== null && original_parent !== undefined) {+ original_parent.add(child)+ } else {+ exported_scene.add(child)+ }+ })+ }+ }+private save_file (blob: Blob, filename: string): void {const export_button_hidden_link: HTMLAnchorElement | null = document.querySelector('#download-hidden-link')if (export_button_hidden_link != null) {@@ -99,4 +210,8 @@ export class StepExportRetargetedAnimations extends EventTarget {private save_array_buffer (buffer: ArrayBuffer, filename: string): void {this.save_file(new Blob([buffer], { type: 'application/octet-stream' }), filename)}++ private save_uint8_array (buffer: Uint8Array, filename: string): void {+ this.save_file(new Blob([buffer], { type: 'application/octet-stream' }), filename)+ }}diff --git a/src/styles.css b/src/styles.cssindex fddccf6..c45c8f1 100644--- a/src/styles.css+++ b/src/styles.css@@ -350,7 +350,8 @@ button:disabled {align-items: center;}-.download-combo #export-button {+.download-combo #export-button,+.download-combo #export-retargeting-button {margin-right: 0;border-top-right-radius: 0;border-bottom-right-radius: 0;