Import a 3D Model and automatically assign and export animations
TypeScript
Give different FBX output formats to see what they might do
Scott Petroviccb986bd44447
diff --git a/src/lib/DOMUtilities.ts b/src/lib/DOMUtilities.tsindex 9ccd703..ac473b4 100644--- a/src/lib/DOMUtilities.ts+++ b/src/lib/DOMUtilities.ts@@ -203,7 +203,27 @@ export class DOMUtilities {<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>+ <label for="export-format-fbx">FBX 7.4</label>+ </fieldset>+ </div>++ <div id="download-fbx-preset-section" class="options-container" hidden>+ <span class="download-settings-label">FBX Preset</span>+ <fieldset id="download-fbx-preset-group" class="toggle" aria-label="FBX export preset">+ <input type="radio" id="fbx-preset-unreal" name="fbx-export-preset" value="unreal">+ <label for="fbx-preset-unreal">Unreal</label>++ <input type="radio" id="fbx-preset-blender" name="fbx-export-preset" value="blender" checked>+ <label for="fbx-preset-blender">Blender</label>++ <input type="radio" id="fbx-preset-threejs" name="fbx-export-preset" value="threejs">+ <label for="fbx-preset-threejs">Three.js</label>++ <input type="radio" id="fbx-preset-unity" name="fbx-export-preset" value="unity">+ <label for="fbx-preset-unity">Unity</label>++ <input type="radio" id="fbx-preset-maya" name="fbx-export-preset" value="maya">+ <label for="fbx-preset-maya">Maya</label></fieldset></div>diff --git a/src/lib/processes/export-to-file/DownloadSettings.ts b/src/lib/processes/export-to-file/DownloadSettings.tsindex 1534545..2aba3b0 100644--- a/src/lib/processes/export-to-file/DownloadSettings.ts+++ b/src/lib/processes/export-to-file/DownloadSettings.ts@@ -15,10 +15,19 @@ export enum ExportFormat {FBX = 'fbx'}+export enum FbxExportPreset {+ Unreal = 'unreal',+ Blender = 'blender',+ ThreeJS = 'threejs',+ Unity = 'unity',+ Maya = 'maya'+}+export class DownloadSettings extends EventTarget {private selected_bone_naming_structure: BoneNamingStructure = BoneNamingStructure.Defaultprivate selected_export_contents: ExportContents = ExportContents.Fullprivate selected_export_format: ExportFormat = ExportFormat.GLB+ private selected_fbx_export_preset: FbxExportPreset = FbxExportPreset.Blenderprivate dom_download_settings_popup: HTMLElement | null = nullprivate dom_download_settings_toggle: HTMLButtonElement | null = nullprivate dom_download_settings_panel: HTMLElement | null = null@@ -29,10 +38,14 @@ export class DownloadSettings extends EventTarget {private dom_export_contents_full_radio: HTMLInputElement | null = nullprivate dom_export_format_group: HTMLElement | null = nullprivate dom_export_format_glb_radio: HTMLInputElement | null = null+ private dom_fbx_preset_section: HTMLElement | null = null+ private dom_fbx_preset_group: HTMLElement | null = null+ private dom_fbx_preset_blender_radio: HTMLInputElement | null = nullconstructor () {super()this.initialize_dom_elements()+ this.update_fbx_preset_ui_visibility()this.add_event_listeners()}@@ -48,6 +61,10 @@ export class DownloadSettings extends EventTarget {return this.selected_export_format}+ public fbx_export_preset (): FbxExportPreset {+ return this.selected_fbx_export_preset+ }+// The download settings popup is available for all skeleton types. The bone naming// options only apply to the human skeleton, so that section is shown/hidden here while// the export contents options remain available regardless of skeleton type.@@ -68,12 +85,19 @@ export class DownloadSettings extends EventTarget {this.dom_export_format_glb_radio.checked = true}+ this.selected_fbx_export_preset = FbxExportPreset.Blender+ if (this.dom_fbx_preset_blender_radio !== null) {+ this.dom_fbx_preset_blender_radio.checked = true+ }+const is_human_skeleton = skeleton_type === SkeletonType.Human// Only the bone naming section is human-only; the popup itself is always available.if (this.dom_bone_naming_section !== null) {this.dom_bone_naming_section.style.display = is_human_skeleton ? '' : 'none'}++ this.update_fbx_preset_ui_visibility()}private initialize_dom_elements (): void {@@ -87,6 +111,9 @@ export class DownloadSettings extends EventTarget {this.dom_export_contents_full_radio = document.querySelector('#export-contents-full')this.dom_export_format_group = document.querySelector('#download-export-format-group')this.dom_export_format_glb_radio = document.querySelector('#export-format-glb')+ this.dom_fbx_preset_section = document.querySelector('#download-fbx-preset-section')+ this.dom_fbx_preset_group = document.querySelector('#download-fbx-preset-group')+ this.dom_fbx_preset_blender_radio = document.querySelector('#fbx-preset-blender')}private add_event_listeners (): void {@@ -164,9 +191,50 @@ export class DownloadSettings extends EventTarget {this.dispatchEvent(new CustomEvent('export-format-changed', {detail: { exportFormat: this.selected_export_format }}))++ this.update_fbx_preset_ui_visibility()+ })++ this.dom_fbx_preset_group?.addEventListener('change', (event: Event) => {+ const selected_radio = event.target as HTMLInputElement | null++ if (selected_radio === null || selected_radio.name !== 'fbx-export-preset') {+ return+ }++ switch (selected_radio.value) {+ case FbxExportPreset.Unreal:+ this.selected_fbx_export_preset = FbxExportPreset.Unreal+ break+ case FbxExportPreset.ThreeJS:+ this.selected_fbx_export_preset = FbxExportPreset.ThreeJS+ break+ case FbxExportPreset.Unity:+ this.selected_fbx_export_preset = FbxExportPreset.Unity+ break+ case FbxExportPreset.Maya:+ this.selected_fbx_export_preset = FbxExportPreset.Maya+ break+ default:+ this.selected_fbx_export_preset = FbxExportPreset.Blender+ break+ }++ this.dispatchEvent(new CustomEvent('fbx-export-preset-changed', {+ detail: { fbxExportPreset: this.selected_fbx_export_preset }+ }))})}+ private update_fbx_preset_ui_visibility (): void {+ if (this.dom_fbx_preset_section === null) {+ return+ }++ const should_show_fbx_preset = this.selected_export_format === ExportFormat.FBX+ this.dom_fbx_preset_section.hidden = !should_show_fbx_preset+ }+private set_popup_visibility (is_visible: boolean): void {if (this.dom_download_settings_panel !== null) {this.dom_download_settings_panel.hidden = !is_visiblediff --git a/src/lib/processes/export-to-file/StepExportToFile.ts b/src/lib/processes/export-to-file/StepExportToFile.tsindex f29532a..9d18797 100644--- a/src/lib/processes/export-to-file/StepExportToFile.ts+++ b/src/lib/processes/export-to-file/StepExportToFile.ts@@ -2,7 +2,7 @@ import { UI } from '../../UI.ts'import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter.js'import { FBXExporter } from '@comfyorg/fbx-exporter-three'import { type AnimationClip, Scene, type SkinnedMesh, type Object3D } from 'three'-import { type DownloadSettings, ExportContents, ExportFormat } from './DownloadSettings.ts'+import { type DownloadSettings, ExportContents, ExportFormat, type FbxExportPreset } from './DownloadSettings.ts'import { ExportBoneNamingService } from './ExportBoneNamingService.ts'import { AnimationUtility } from '../animations-listing/AnimationUtility.ts'import { type AnimationExportSelection } from '../animations-listing/interfaces/AnimationExportSelection.ts'@@ -88,7 +88,13 @@ export class StepExportToFile extends EventTarget {console.log('SKINNED MESH DATA TO EXPORT:', skinned_meshes)console.log('animations to export', export_clips)- return this.export_scene(export_scene, export_clips, filename, download_settings.export_format())+ return this.export_scene(+ export_scene,+ export_clips,+ filename,+ download_settings.export_format(),+ download_settings.fbx_export_preset()+ ).then(() => {// Move the exported objects back to their original parentsobjects_to_export.forEach((exported_object) => {@@ -117,10 +123,11 @@ export class StepExportToFile extends EventTarget {exported_scene: Scene,animations_to_export: AnimationClip[],file_name: string,- export_format: ExportFormat+ export_format: ExportFormat,+ fbx_export_preset: FbxExportPreset): Promise<void> {if (export_format === ExportFormat.FBX) {- await this.export_fbx(exported_scene, animations_to_export, file_name)+ await this.export_fbx(exported_scene, animations_to_export, file_name, fbx_export_preset)return}@@ -159,12 +166,17 @@ export class StepExportToFile extends EventTarget {})}- public async export_fbx (exported_scene: Scene, animations_to_export: AnimationClip[], file_name: string): Promise<void> {+ public async export_fbx (+ exported_scene: Scene,+ animations_to_export: AnimationClip[],+ file_name: string,+ fbx_export_preset: FbxExportPreset+ ): Promise<void> {exported_scene.animations = animations_to_exporttry {const result = await this.fbx_exporter.parseAsync(exported_scene, {- preset: 'unreal',+ preset: fbx_export_preset,includeAnimations: true,animations: animations_to_export,onlyVisible: false,diff --git a/src/retarget/steps/StepExportRetargetedAnimations.ts b/src/retarget/steps/StepExportRetargetedAnimations.tsindex d352837..020fa86 100644--- a/src/retarget/steps/StepExportRetargetedAnimations.ts+++ b/src/retarget/steps/StepExportRetargetedAnimations.ts@@ -4,7 +4,7 @@ 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 { type DownloadSettings, ExportContents, ExportFormat, type FbxExportPreset } from '../../lib/processes/export-to-file/DownloadSettings'import { ExportBoneNamingService } from '../../lib/processes/export-to-file/ExportBoneNamingService'export class StepExportRetargetedAnimations extends EventTarget {@@ -80,7 +80,13 @@ export class StepExportRetargetedAnimations extends EventTarget {})try {- await this.export_scene(export_scene, animations_to_export, file_name, download_settings.export_format())+ await this.export_scene(+ export_scene,+ animations_to_export,+ file_name,+ download_settings.export_format(),+ download_settings.fbx_export_preset()+ )} finally {objects_to_export.forEach((exported_object) => {const original_parent = original_parents.get(exported_object)@@ -99,10 +105,11 @@ export class StepExportRetargetedAnimations extends EventTarget {exported_scene: Scene,animations_to_export: AnimationClip[],file_name: string,- export_format: ExportFormat+ export_format: ExportFormat,+ fbx_export_preset: FbxExportPreset): Promise<void> {if (export_format === ExportFormat.FBX) {- await this.export_fbx(exported_scene, animations_to_export, file_name)+ await this.export_fbx(exported_scene, animations_to_export, file_name, fbx_export_preset)return}@@ -141,12 +148,17 @@ export class StepExportRetargetedAnimations extends EventTarget {})}- public async export_fbx (exported_scene: Scene, animations_to_export: AnimationClip[], file_name: string): Promise<void> {+ public async export_fbx (+ exported_scene: Scene,+ animations_to_export: AnimationClip[],+ file_name: string,+ fbx_export_preset: FbxExportPreset+ ): Promise<void> {exported_scene.animations = animations_to_exporttry {const result = await this.fbx_exporter.parseAsync(exported_scene, {- preset: 'unreal',+ preset: fbx_export_preset,includeAnimations: true,animations: animations_to_export,onlyVisible: false,diff --git a/src/styles.css b/src/styles.cssindex c45c8f1..a24d284 100644--- a/src/styles.css+++ b/src/styles.css@@ -385,9 +385,11 @@ button:disabled {z-index: 1000;display: inline-flex;flex-direction: column;- align-items: center;+ align-items: stretch;gap: 1.5rem;padding: 1rem;+ min-width: 16rem;+ max-width: 19rem;border: 1px solid var(--bg-tertiary);border-radius: 6px;background: color-mix(in srgb, var(--bg-primary) 90%, black);@@ -414,11 +416,15 @@ button:disabled {.options-container {display: flex;flex-direction: column;- align-items: center;+ align-items: stretch;gap: 0.5rem;padding: 0;}+.options-container[hidden] {+ display: none;+}+#download-bone-naming-group {gap: 0;}@@ -428,6 +434,26 @@ button:disabled {padding: 0.3rem 0.6rem;}+#download-fbx-preset-group {+ display: grid;+ grid-template-columns: repeat(2, minmax(0, 1fr));+ gap: 0.35rem;+}++#download-fbx-preset-group input + label {+ justify-content: center;+ border: solid 2px var(--radio-button-group-bg);+ border-radius: 6px;+ padding: 0.35rem 0.5rem;+ font-size: 0.95rem;+}++#download-fbx-preset-group input + label:first-of-type,+#download-fbx-preset-group input + label:last-of-type {+ border: solid 2px var(--radio-button-group-bg);+ border-radius: 6px;+}+.transform-icons {width: 25px;