Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Just start with Unity and Unreal export formats. Not sure if any other platforms rely on FBX so bad

Scott Petrovice5470e083991
diff --git a/src/lib/DOMUtilities.ts b/src/lib/DOMUtilities.ts
index ac473b4..c310640 100644
--- a/src/lib/DOMUtilities.ts
+++ b/src/lib/DOMUtilities.ts
@@ -1,4 +1,10 @@
import { RigConfig } from './RigConfig.ts'
+import {
+ BoneNamingStructure,
+ ExportContents,
+ ExportFormat,
+ FbxExportPreset
+} from './processes/export-to-file/DownloadSettings.ts'
interface RangeSettingConfig {
min: string
@@ -186,55 +192,30 @@ export class DOMUtilities {
<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>
+ <span class="download-settings-label">Bone Naming Pattern</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>
+ ${DOMUtilities.get_bone_naming_options_markup()}
</fieldset>
</div>
<div class="options-container">
- <span class="download-settings-label">Format</span>
+ <span class="download-settings-label">File 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 7.4</label>
+ ${DOMUtilities.get_export_format_options_markup()}
</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>
+ ${DOMUtilities.get_fbx_preset_options_markup()}
</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>
+ ${DOMUtilities.get_export_contents_options_markup()}
</fieldset>
</div>
</div>
@@ -264,6 +245,110 @@ export class DOMUtilities {
return 'images/icons/arrow-dropdown.svg'
}
+ private static get_fbx_preset_options_markup (): string {
+ const default_fbx_preset = DOMUtilities.get_default_fbx_preset()
+
+ return Object.values(FbxExportPreset).map((fbx_preset) => {
+ const preset_id = `fbx-preset-${fbx_preset}`
+ const preset_label = DOMUtilities.get_download_option_label(fbx_preset)
+ const is_checked = fbx_preset === default_fbx_preset ? ' checked' : ''
+
+ return `
+ <input type="radio" id="${preset_id}" name="fbx-export-preset" value="${fbx_preset}"${is_checked}>
+ <label for="${preset_id}">${preset_label}</label>
+ `
+ }).join('')
+ }
+
+ private static get_bone_naming_options_markup (): string {
+ const default_bone_naming_structure = DOMUtilities.get_default_bone_naming_structure()
+
+ return Object.values(BoneNamingStructure).map((bone_naming_structure) => {
+ const option_id = `bone-naming-${bone_naming_structure}`
+ const option_label = DOMUtilities.get_download_option_label(bone_naming_structure)
+ const is_checked = bone_naming_structure === default_bone_naming_structure ? ' checked' : ''
+
+ return `
+ <input type="radio" id="${option_id}" name="bone-naming-structure" value="${bone_naming_structure}"${is_checked}>
+ <label for="${option_id}">${option_label}</label>
+ `
+ }).join('')
+ }
+
+ private static get_export_format_options_markup (): string {
+ const default_export_format = DOMUtilities.get_default_export_format()
+
+ return Object.values(ExportFormat).map((export_format) => {
+ const option_id = `export-format-${export_format}`
+ const option_label = DOMUtilities.get_download_option_label(export_format)
+ const is_checked = export_format === default_export_format ? ' checked' : ''
+
+ return `
+ <input type="radio" id="${option_id}" name="export-format" value="${export_format}"${is_checked}>
+ <label for="${option_id}">${option_label}</label>
+ `
+ }).join('')
+ }
+
+ private static get_export_contents_options_markup (): string {
+ const default_export_contents = DOMUtilities.get_default_export_contents()
+
+ return Object.values(ExportContents).map((export_contents) => {
+ const option_id = `export-contents-${export_contents}`
+ const option_label = DOMUtilities.get_download_option_label(export_contents)
+ const is_checked = export_contents === default_export_contents ? ' checked' : ''
+
+ return `
+ <input type="radio" id="${option_id}" name="export-contents" value="${export_contents}"${is_checked}>
+ <label for="${option_id}">${option_label}</label>
+ `
+ }).join('')
+ }
+
+ private static get_default_bone_naming_structure (): BoneNamingStructure {
+ return DOMUtilities.get_first_enum_value(BoneNamingStructure)
+ }
+
+ private static get_default_export_format (): ExportFormat {
+ return DOMUtilities.get_first_enum_value(ExportFormat)
+ }
+
+ private static get_default_export_contents (): ExportContents {
+ return DOMUtilities.get_first_enum_value(ExportContents)
+ }
+
+ private static get_default_fbx_preset (): FbxExportPreset {
+ return DOMUtilities.get_first_enum_value(FbxExportPreset)
+ }
+
+ private static get_first_enum_value<T extends string> (enum_object: Record<string, T>): T {
+ const enum_values = Object.values(enum_object)
+ if (enum_values.length === 0) {
+ throw new Error('Enum has no values')
+ }
+
+ return enum_values[0]
+ }
+
+ private static get_download_option_label (value: string): string {
+ const custom_labels: Partial<Record<string, string>> = {
+ threejs: 'Three.js',
+ fbx: 'FBX 7.4',
+ glb: 'GLB',
+ full: 'Default',
+ skeleton: 'Skeleton + Animations'
+ }
+
+ const custom_label = custom_labels[value]
+ if (custom_label !== undefined) {
+ return custom_label
+ }
+
+ return value
+ .replace(/[-_]+/g, ' ')
+ .replace(/\b\w/g, (match) => match.toUpperCase())
+ }
+
private static is_retarget_page (): boolean {
const path_name = window.location.pathname.toLowerCase()
return path_name.includes('/retarget/')
diff --git a/src/lib/processes/export-to-file/DownloadSettings.ts b/src/lib/processes/export-to-file/DownloadSettings.ts
index 2aba3b0..33fc5b6 100644
--- a/src/lib/processes/export-to-file/DownloadSettings.ts
+++ b/src/lib/processes/export-to-file/DownloadSettings.ts
@@ -17,30 +17,26 @@ export enum ExportFormat {
export enum FbxExportPreset {
Unreal = 'unreal',
- Blender = 'blender',
- ThreeJS = 'threejs',
+ // Blender = 'blender',
+ // ThreeJS = 'threejs',
Unity = 'unity',
- Maya = 'maya'
+ // Maya = 'maya'
}
export class DownloadSettings extends EventTarget {
- private selected_bone_naming_structure: BoneNamingStructure = BoneNamingStructure.Default
- private selected_export_contents: ExportContents = ExportContents.Full
- private selected_export_format: ExportFormat = ExportFormat.GLB
- private selected_fbx_export_preset: FbxExportPreset = FbxExportPreset.Blender
+ private selected_bone_naming_structure: BoneNamingStructure = this.get_default_bone_naming_structure()
+ private selected_export_contents: ExportContents = this.get_default_export_contents()
+ private selected_export_format: ExportFormat = this.get_default_export_format()
+ private selected_fbx_export_preset: FbxExportPreset = this.get_default_fbx_export_preset()
private dom_download_settings_popup: HTMLElement | null = null
private dom_download_settings_toggle: HTMLButtonElement | null = null
private dom_download_settings_panel: HTMLElement | null = null
private dom_bone_naming_section: HTMLElement | null = null
private dom_bone_naming_group: HTMLElement | null = null
- private dom_bone_naming_default_radio: HTMLInputElement | null = null
private dom_export_contents_group: HTMLElement | null = null
- private dom_export_contents_full_radio: HTMLInputElement | null = null
private dom_export_format_group: HTMLElement | null = null
- private 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 = null
constructor () {
super()
@@ -70,25 +66,17 @@ export class DownloadSettings extends EventTarget {
// the export contents options remain available regardless of skeleton type.
public update_download_settings_ui_visibility (skeleton_type: SkeletonType): void {
// reset to defaults
- this.selected_bone_naming_structure = BoneNamingStructure.Default
- if (this.dom_bone_naming_default_radio !== null) {
- this.dom_bone_naming_default_radio.checked = true
- }
+ this.selected_bone_naming_structure = this.get_default_bone_naming_structure()
+ this.set_radio_group_value(this.dom_bone_naming_group, 'bone-naming-structure', this.selected_bone_naming_structure)
- this.selected_export_contents = ExportContents.Full
- if (this.dom_export_contents_full_radio !== null) {
- this.dom_export_contents_full_radio.checked = true
- }
+ this.selected_export_contents = this.get_default_export_contents()
+ this.set_radio_group_value(this.dom_export_contents_group, 'export-contents', this.selected_export_contents)
- this.selected_export_format = ExportFormat.GLB
- if (this.dom_export_format_glb_radio !== null) {
- this.dom_export_format_glb_radio.checked = true
- }
+ this.selected_export_format = this.get_default_export_format()
+ this.set_radio_group_value(this.dom_export_format_group, 'export-format', this.selected_export_format)
- this.selected_fbx_export_preset = FbxExportPreset.Blender
- if (this.dom_fbx_preset_blender_radio !== null) {
- this.dom_fbx_preset_blender_radio.checked = true
- }
+ this.selected_fbx_export_preset = this.get_default_fbx_export_preset()
+ this.set_radio_group_value(this.dom_fbx_preset_group, 'fbx-export-preset', this.selected_fbx_export_preset)
const is_human_skeleton = skeleton_type === SkeletonType.Human
@@ -106,14 +94,10 @@ export class DownloadSettings extends EventTarget {
this.dom_download_settings_panel = document.querySelector('#download-settings')
this.dom_bone_naming_section = document.querySelector('#download-bone-naming-section')
this.dom_bone_naming_group = document.querySelector('#download-bone-naming-group')
- this.dom_bone_naming_default_radio = document.querySelector('#bone-naming-default')
this.dom_export_contents_group = document.querySelector('#download-export-contents-group')
- 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 {
@@ -149,10 +133,11 @@ export class DownloadSettings extends EventTarget {
return
}
- this.selected_bone_naming_structure =
- selected_radio.value === BoneNamingStructure.Mixamo
- ? BoneNamingStructure.Mixamo
- : BoneNamingStructure.Default
+ if (this.is_bone_naming_structure(selected_radio.value)) {
+ this.selected_bone_naming_structure = selected_radio.value
+ } else {
+ this.selected_bone_naming_structure = this.get_default_bone_naming_structure()
+ }
this.dispatchEvent(new CustomEvent('bone-naming-structure-changed', {
detail: { boneNamingStructure: this.selected_bone_naming_structure }
@@ -166,10 +151,11 @@ export class DownloadSettings extends EventTarget {
return
}
- this.selected_export_contents =
- selected_radio.value === ExportContents.Skeleton
- ? ExportContents.Skeleton
- : ExportContents.Full
+ if (this.is_export_contents(selected_radio.value)) {
+ this.selected_export_contents = selected_radio.value
+ } else {
+ this.selected_export_contents = this.get_default_export_contents()
+ }
this.dispatchEvent(new CustomEvent('export-contents-changed', {
detail: { exportContents: this.selected_export_contents }
@@ -183,10 +169,11 @@ export class DownloadSettings extends EventTarget {
return
}
- this.selected_export_format =
- selected_radio.value === ExportFormat.FBX
- ? ExportFormat.FBX
- : ExportFormat.GLB
+ if (this.is_export_format(selected_radio.value)) {
+ this.selected_export_format = selected_radio.value
+ } else {
+ this.selected_export_format = this.get_default_export_format()
+ }
this.dispatchEvent(new CustomEvent('export-format-changed', {
detail: { exportFormat: this.selected_export_format }
@@ -202,22 +189,10 @@ export class DownloadSettings extends EventTarget {
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
+ if (this.is_fbx_export_preset(selected_radio.value)) {
+ this.selected_fbx_export_preset = selected_radio.value
+ } else {
+ this.selected_fbx_export_preset = this.get_default_fbx_export_preset()
}
this.dispatchEvent(new CustomEvent('fbx-export-preset-changed', {
@@ -244,4 +219,52 @@ export class DownloadSettings extends EventTarget {
this.dom_download_settings_toggle.setAttribute('aria-expanded', is_visible ? 'true' : 'false')
}
}
+
+ private set_radio_group_value (group: HTMLElement | null, name: string, value: string): void {
+ const option_radio = group?.querySelector<HTMLInputElement>(`input[name="${name}"][value="${value}"]`)
+ if (option_radio !== null && option_radio !== undefined) {
+ option_radio.checked = true
+ }
+ }
+
+ private is_bone_naming_structure (value: string): value is BoneNamingStructure {
+ return Object.values(BoneNamingStructure).includes(value as BoneNamingStructure)
+ }
+
+ private is_export_contents (value: string): value is ExportContents {
+ return Object.values(ExportContents).includes(value as ExportContents)
+ }
+
+ private is_export_format (value: string): value is ExportFormat {
+ return Object.values(ExportFormat).includes(value as ExportFormat)
+ }
+
+ private is_fbx_export_preset (value: string): value is FbxExportPreset {
+ return Object.values(FbxExportPreset).includes(value as FbxExportPreset)
+ }
+
+ private get_default_fbx_export_preset (): FbxExportPreset {
+ return this.get_first_enum_value(FbxExportPreset)
+ }
+
+ private get_default_bone_naming_structure (): BoneNamingStructure {
+ return this.get_first_enum_value(BoneNamingStructure)
+ }
+
+ private get_default_export_contents (): ExportContents {
+ return this.get_first_enum_value(ExportContents)
+ }
+
+ private get_default_export_format (): ExportFormat {
+ return this.get_first_enum_value(ExportFormat)
+ }
+
+ private get_first_enum_value<T extends string> (enum_object: Record<string, T>): T {
+ const enum_values = Object.values(enum_object)
+ if (enum_values.length === 0) {
+ throw new Error('Enum has no values')
+ }
+
+ return enum_values[0]
+ }
}