Import a 3D Model and automatically assign and export animations
TypeScript
Have a download option to only export out animation + skeleton data to avoid 3d mesh
Scott Petrovic408db807d2d7
diff --git a/src/index.html b/src/index.htmlindex d6d020d..083de8d 100644--- a/src/index.html+++ b/src/index.html@@ -188,14 +188,29 @@</button><div id="download-settings" class="download-settings-panel" role="dialog" aria-label="Download settings" hidden>- <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>+ <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">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>diff --git a/src/lib/processes/export-to-file/DownloadSettings.ts b/src/lib/processes/export-to-file/DownloadSettings.tsindex 50f89c9..3bef625 100644--- a/src/lib/processes/export-to-file/DownloadSettings.ts+++ b/src/lib/processes/export-to-file/DownloadSettings.ts@@ -5,13 +5,22 @@ export enum BoneNamingStructure {Mixamo = 'mixamo'}+export enum ExportContents {+ Full = 'full',+ Skeleton = 'skeleton'+}+export class DownloadSettings extends EventTarget {private selected_bone_naming_structure: BoneNamingStructure = BoneNamingStructure.Default+ private selected_export_contents: ExportContents = ExportContents.Fullprivate dom_download_settings_popup: HTMLElement | null = nullprivate dom_download_settings_toggle: HTMLButtonElement | null = nullprivate dom_download_settings_panel: HTMLElement | null = null+ private dom_bone_naming_section: HTMLElement | null = nullprivate dom_bone_naming_group: HTMLElement | null = nullprivate dom_bone_naming_default_radio: HTMLInputElement | null = null+ private dom_export_contents_group: HTMLElement | null = null+ private dom_export_contents_full_radio: HTMLInputElement | null = nullconstructor () {super()@@ -23,24 +32,30 @@ export class DownloadSettings extends EventTarget {return this.selected_bone_naming_structure}- // Download options currently only apply to human skeleton, so this will- // show/hide the DOM download settings options on UI based on that.+ public export_contents (): ExportContents {+ return this.selected_export_contents+ }++ // 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.public update_download_settings_ui_visibility (skeleton_type: SkeletonType): void {- // reset to default+ // reset to defaultsthis.selected_bone_naming_structure = BoneNamingStructure.Defaultif (this.dom_bone_naming_default_radio !== null) {this.dom_bone_naming_default_radio.checked = true}- const is_human_skeleton = skeleton_type === SkeletonType.Human-- if (this.dom_download_settings_popup !== null) {- this.dom_download_settings_popup.style.display = is_human_skeleton ? 'inline-flex' : 'none'- this.dom_download_settings_popup.setAttribute('aria-hidden', is_human_skeleton ? 'false' : 'true')+ this.selected_export_contents = ExportContents.Full+ if (this.dom_export_contents_full_radio !== null) {+ this.dom_export_contents_full_radio.checked = true}- if (!is_human_skeleton) {- this.set_popup_visibility(false)+ 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'}}@@ -48,8 +63,11 @@ export class DownloadSettings extends EventTarget {this.dom_download_settings_popup = document.querySelector('#download-settings-popup')this.dom_download_settings_toggle = document.querySelector('#download-settings-toggle')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')}private add_event_listeners (): void {@@ -94,6 +112,23 @@ export class DownloadSettings extends EventTarget {detail: { boneNamingStructure: this.selected_bone_naming_structure }}))})++ this.dom_export_contents_group?.addEventListener('change', (event: Event) => {+ const selected_radio = event.target as HTMLInputElement | null++ if (selected_radio === null || selected_radio.name !== 'export-contents') {+ return+ }++ this.selected_export_contents =+ selected_radio.value === ExportContents.Skeleton+ ? ExportContents.Skeleton+ : ExportContents.Full++ this.dispatchEvent(new CustomEvent('export-contents-changed', {+ detail: { exportContents: this.selected_export_contents }+ }))+ })}private set_popup_visibility (is_visible: boolean): void {diff --git a/src/lib/processes/export-to-file/StepExportToFile.ts b/src/lib/processes/export-to-file/StepExportToFile.tsindex 3454e24..9aa6777 100644--- a/src/lib/processes/export-to-file/StepExportToFile.ts+++ b/src/lib/processes/export-to-file/StepExportToFile.ts@@ -1,7 +1,7 @@import { UI } from '../../UI.ts'import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter.js'import { type AnimationClip, Scene, type SkinnedMesh, type Object3D } from 'three'-import { type DownloadSettings } from './DownloadSettings.ts'+import { type DownloadSettings, ExportContents } from './DownloadSettings.ts'import { ExportBoneNamingService } from './ExportBoneNamingService.ts'// Note: EventTarget is a built-in interface and do not need to import it@@ -30,43 +30,56 @@ export class StepExportToFile extends EventTarget {const export_scene = new Scene()- // When exporting to a file, we need to temporarily move the skinned mesh to a new scene- // skinned meshes can only be part of one scene at a time, so we must move it back to- // its original parent after exporting- const original_parents = new Map<SkinnedMesh, Object3D | null>()-- skinned_meshes.forEach((final_skinned_mesh) => {- // Save the original parent- original_parents.set(final_skinned_mesh, final_skinned_mesh.parent)- export_scene.add(final_skinned_mesh)- })-const export_clips = this.animation_clips_to_export.map((clip) => {const cloned_clip = clip.clone()// Make action names consistent by replacing any whitespace with underscorescloned_clip.name = cloned_clip.name.replace(/\s+/g, '_')return cloned_clip})++ // Apply bone naming before re-parenting: the service traverses each skinned mesh to+ // find its bones, which are still children of the mesh at this point.const restore_bone_names = ExportBoneNamingService.apply_download_settings(skinned_meshes,export_clips,download_settings.bone_naming_structure())+ // "Skeleton only" exports just the bone hierarchy (the root bone subtree that hangs off+ // each skinned mesh) instead of the full skinned mesh, which excludes the mesh geometry.+ // The animation clips still target the bone names, so the selected animations continue+ // to work against the exported skeleton.+ 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++ // When exporting to a file, we need to temporarily move the exported objects to a new+ // scene. An object can only be part of one scene at a time, so we must move it back to+ // its original parent after exporting. For a skeleton-only export, the root bone's+ // original parent is its skinned mesh.+ const original_parents = new Map<Object3D, Object3D | null>()++ objects_to_export.forEach((object_to_export) => {+ // Save the original parent+ original_parents.set(object_to_export, object_to_export.parent)+ export_scene.add(object_to_export)+ })+console.log('SKINNED MESH DATA TO EXPORT:', skinned_meshes)console.log('animations to export', export_clips)this.export_glb(export_scene, export_clips, filename).then(() => {- // Move the skinned meshes back to their original parents- skinned_meshes.forEach((final_skinned_mesh) => {- const original_par = original_parents.get(final_skinned_mesh)+ // Move the exported objects back to their original parents+ objects_to_export.forEach((exported_object) => {+ const original_par = original_parents.get(exported_object)if (original_par != null) {- original_par.add(final_skinned_mesh)+ original_par.add(exported_object)} else {// If there was no original parent, remove it from the scene- export_scene.remove(final_skinned_mesh)- console.log('ERROR: No original parent found for skinned mesh when exporting and re-parenting to original scene')+ export_scene.remove(exported_object)+ console.log('ERROR: No original parent found for exported object when exporting and re-parenting to original scene')}})diff --git a/src/styles.css b/src/styles.cssindex 554304a..d86f7db 100644--- a/src/styles.css+++ b/src/styles.css@@ -362,7 +362,7 @@ button:disabled {display: inline-flex;flex-direction: column;align-items: center;- gap: 0.5rem;+ gap: 1.5rem;padding: 1rem;border: 1px solid var(--bg-tertiary);border-radius: 6px;@@ -374,12 +374,27 @@ button:disabled {display: none;}+.download-settings-header {+ align-self: flex-start;+ color: var(--text-primary);+ font-size: 1.2rem;+ white-space: pre; /* no line breaks */+}+.download-settings-label {color: var(--text-alternate);font-size: 1.0rem;white-space: pre; /* no line breaks */}+.options-container {+ display: flex;+ flex-direction: column;+ align-items: center;+ gap: 0.5rem;+ padding: 0;+}+#download-bone-naming-group {gap: 0;}