Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Try to start using the fbx exporter found on github

Scott Petrovicaa3b55a8cb4c
diff --git a/package-lock.json b/package-lock.json
index 39d855d..580bb70 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -8,6 +8,7 @@
"name": "model-motionizer",
"version": "0.0.0",
"dependencies": {
+ "@comfyorg/fbx-exporter-three": "^1.0.1",
"file-saver": "^2.0.5",
"jszip": "^3.10.1",
"three": "^0.183.0",
@@ -209,6 +210,27 @@
"node": ">=16"
}
},
+ "node_modules/@comfyorg/fbx-exporter-three": {
+ "version": "1.0.1",
+ "resolved": "https://registry.npmjs.org/@comfyorg/fbx-exporter-three/-/fbx-exporter-three-1.0.1.tgz",
+ "integrity": "sha512-fQ1zBsgmmwfio6iEi91hRiFCr946yEgqR2DGh/UMismaLyUohiKGOJL/OnJQnW3+yne/PXxVoYgcortyumsO5w==",
+ "license": "MIT",
+ "dependencies": {
+ "fflate": "^0.8.2"
+ },
+ "engines": {
+ "node": ">=18"
+ },
+ "peerDependencies": {
+ "@types/three": ">=0.160.0",
+ "three": ">=0.160.0"
+ },
+ "peerDependenciesMeta": {
+ "@types/three": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@cspotcode/source-map-support": {
"version": "0.8.1",
"resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz",
@@ -319,6 +341,7 @@
}
],
"license": "MIT",
+ "peer": true,
"engines": {
"node": ">=18"
},
@@ -342,6 +365,7 @@
}
],
"license": "MIT",
+ "peer": true,
"engines": {
"node": ">=18"
}
@@ -350,7 +374,7 @@
"version": "0.12.0",
"resolved": "https://registry.npmjs.org/@dimforge/rapier3d-compat/-/rapier3d-compat-0.12.0.tgz",
"integrity": "sha512-uekIGetywIgopfD97oDL5PfeezkFpNhwlzlaEYNOA0N6ghdsOvh/HYjSMek5Q2O1PYvRSDFcqFVJl4r4ZBwOow==",
- "dev": true,
+ "devOptional": true,
"license": "Apache-2.0"
},
"node_modules/@emnapi/core": {
@@ -1167,9 +1191,6 @@
"cpu": [
"arm"
],
- "libc": [
- "glibc"
- ],
"license": "LGPL-3.0-or-later",
"optional": true,
"os": [
@@ -1186,9 +1207,6 @@
"cpu": [
"arm64"
],
- "libc": [
- "glibc"
- ],
"license": "LGPL-3.0-or-later",
"optional": true,
"os": [
@@ -1205,9 +1223,6 @@
"cpu": [
"ppc64"
],
- "libc": [
- "glibc"
- ],
"license": "LGPL-3.0-or-later",
"optional": true,
"os": [
@@ -1224,9 +1239,6 @@
"cpu": [
"riscv64"
],
- "libc": [
- "glibc"
- ],
"license": "LGPL-3.0-or-later",
"optional": true,
"os": [
@@ -1243,9 +1255,6 @@
"cpu": [
"s390x"
],
- "libc": [
- "glibc"
- ],
"license": "LGPL-3.0-or-later",
"optional": true,
"os": [
@@ -1262,9 +1271,6 @@
"cpu": [
"x64"
],
- "libc": [
- "glibc"
- ],
"license": "LGPL-3.0-or-later",
"optional": true,
"os": [
@@ -1281,9 +1287,6 @@
"cpu": [
"arm64"
],
- "libc": [
- "musl"
- ],
"license": "LGPL-3.0-or-later",
"optional": true,
"os": [
@@ -1300,9 +1303,6 @@
"cpu": [
"x64"
],
- "libc": [
- "musl"
- ],
"license": "LGPL-3.0-or-later",
"optional": true,
"os": [
@@ -1319,9 +1319,6 @@
"cpu": [
"arm"
],
- "libc": [
- "glibc"
- ],
"license": "Apache-2.0",
"optional": true,
"os": [
@@ -1344,9 +1341,6 @@
"cpu": [
"arm64"
],
- "libc": [
- "glibc"
- ],
"license": "Apache-2.0",
"optional": true,
"os": [
@@ -1369,9 +1363,6 @@
"cpu": [
"ppc64"
],
- "libc": [
- "glibc"
- ],
"license": "Apache-2.0",
"optional": true,
"os": [
@@ -1394,9 +1385,6 @@
"cpu": [
"riscv64"
],
- "libc": [
- "glibc"
- ],
"license": "Apache-2.0",
"optional": true,
"os": [
@@ -1419,9 +1407,6 @@
"cpu": [
"s390x"
],
- "libc": [
- "glibc"
- ],
"license": "Apache-2.0",
"optional": true,
"os": [
@@ -1444,9 +1429,6 @@
"cpu": [
"x64"
],
- "libc": [
- "glibc"
- ],
"license": "Apache-2.0",
"optional": true,
"os": [
@@ -1469,9 +1451,6 @@
"cpu": [
"arm64"
],
- "libc": [
- "musl"
- ],
"license": "Apache-2.0",
"optional": true,
"os": [
@@ -1494,9 +1473,6 @@
"cpu": [
"x64"
],
- "libc": [
- "musl"
- ],
"license": "Apache-2.0",
"optional": true,
"os": [
@@ -1781,9 +1757,6 @@
"cpu": [
"arm64"
],
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1800,9 +1773,6 @@
"cpu": [
"arm64"
],
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1819,9 +1789,6 @@
"cpu": [
"ppc64"
],
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1838,9 +1805,6 @@
"cpu": [
"s390x"
],
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1857,9 +1821,6 @@
"cpu": [
"x64"
],
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1876,9 +1837,6 @@
"cpu": [
"x64"
],
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1994,7 +1952,7 @@
"version": "23.1.3",
"resolved": "https://registry.npmjs.org/@tweenjs/tween.js/-/tween.js-23.1.3.tgz",
"integrity": "sha512-vJmvvwFxYuGnF2axRtPYocag6Clbb5YS7kLL+SO/TeVFzHqDIWrNKYtcsPMibjDx9O+bu+psAy9NKfWklassUA==",
- "dev": true,
+ "devOptional": true,
"license": "MIT"
},
"node_modules/@tybys/wasm-util": {
@@ -2050,15 +2008,16 @@
"version": "0.17.4",
"resolved": "https://registry.npmjs.org/@types/stats.js/-/stats.js-0.17.4.tgz",
"integrity": "sha512-jIBvWWShCvlBqBNIZt0KAshWpvSjhkwkEu4ZUcASoAvhmrgAUI2t1dXrjSL4xXVLB4FznPrIsX3nKXFl/Dt4vA==",
- "dev": true,
+ "devOptional": true,
"license": "MIT"
},
"node_modules/@types/three": {
"version": "0.183.1",
"resolved": "https://registry.npmjs.org/@types/three/-/three-0.183.1.tgz",
"integrity": "sha512-f2Pu5Hrepfgavttdye3PsH5RWyY/AvdZQwIVhrc4uNtvF7nOWJacQKcoVJn0S4f0yYbmAE6AR+ve7xDcuYtMGw==",
- "dev": true,
+ "devOptional": true,
"license": "MIT",
+ "peer": true,
"dependencies": {
"@dimforge/rapier3d-compat": "~0.12.0",
"@tweenjs/tween.js": "~23.1.3",
@@ -2073,7 +2032,7 @@
"version": "0.5.24",
"resolved": "https://registry.npmjs.org/@types/webxr/-/webxr-0.5.24.tgz",
"integrity": "sha512-h8fgEd/DpoS9CBrjEQXR+dIDraopAEfu4wYVNY2tEPwk60stPWhvZMf4Foo5FakuQ7HFZoa8WceaWFervK2Ovg==",
- "dev": true,
+ "devOptional": true,
"license": "MIT"
},
"node_modules/@typescript-eslint/eslint-plugin": {
@@ -2111,6 +2070,7 @@
"integrity": "sha512-30ScMRHIAD33JJQkgfGW1t8CURZtjc2JpTrq5n2HFhOefbAhb7ucc7xJwdWcrEtqUIYJ73Nybpsggii6GtAHjA==",
"dev": true,
"license": "MIT",
+ "peer": true,
"dependencies": {
"@typescript-eslint/scope-manager": "8.57.2",
"@typescript-eslint/types": "8.57.2",
@@ -2444,6 +2404,7 @@
"integrity": "sha512-/irhyeAcKS2u6Zokagf9tqZJ0t8S6kMZq4ZG9BHZv7I+fkRrYfQX4w7geYeC2r6obThz39PDxvXQzZX+qXqGeg==",
"dev": true,
"license": "MIT",
+ "peer": true,
"dependencies": {
"@vitest/utils": "4.1.2",
"fflate": "^0.8.2",
@@ -2479,7 +2440,7 @@
"version": "0.1.69",
"resolved": "https://registry.npmjs.org/@webgpu/types/-/types-0.1.69.tgz",
"integrity": "sha512-RPmm6kgRbI8e98zSD3RVACvnuktIja5+yLgDAkTmxLr90BEwdTXRQWNLF3ETTTyH/8mKhznZuN5AveXYFEsMGQ==",
- "dev": true,
+ "devOptional": true,
"license": "BSD-3-Clause"
},
"node_modules/acorn": {
@@ -2488,6 +2449,7 @@
"integrity": "sha512-UVJyE9MttOsBQIDKw1skb9nAwQuR5wuGD3+82K6JgJlm/Y+KI92oNsMNGZCYdDsVtRHSak0pcV5Dno5+4jh9sw==",
"dev": true,
"license": "MIT",
+ "peer": true,
"bin": {
"acorn": "bin/acorn"
},
@@ -3331,6 +3293,7 @@
"integrity": "sha512-8VwMnyGCONIs6cWue2IdpHxHnAjzxnw2Zr7MkVxB2vjmQ2ivqGFb4LEG3SMnv0Gb2F/G/2yA8zUaiL1gywDCCg==",
"hasInstallScript": true,
"license": "MIT",
+ "peer": true,
"bin": {
"esbuild": "bin/esbuild"
},
@@ -3385,6 +3348,7 @@
"integrity": "sha512-XoMjdBOwe/esVgEvLmNsD3IRHkm7fbKIUGvrleloJXUZgDHig2IPWNniv+GwjyJXzuNqVjlr5+4yVUZjycJwfQ==",
"dev": true,
"license": "MIT",
+ "peer": true,
"dependencies": {
"@eslint-community/eslint-utils": "^4.8.0",
"@eslint-community/regexpp": "^4.12.1",
@@ -3933,7 +3897,6 @@
"version": "0.8.2",
"resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.2.tgz",
"integrity": "sha512-cPJU47OaAoCbg0pBvzsgpTPhmhqI5eJjh/JIu8tPj5q+T7iLvW/JAYUqmE7KOB4R1ZyEhzBaIQpQpardBF5z8A==",
- "dev": true,
"license": "MIT"
},
"node_modules/file-entry-cache": {
@@ -4917,6 +4880,7 @@
"integrity": "sha512-8i7LzZj7BF8uplX+ZyOlIz86V6TAsSs+np6m1kpW9u0JWi4z/1t+FzcK1aek+ybTnAC4KhBL4uXCNT0wcUIeCw==",
"dev": true,
"license": "MIT",
+ "peer": true,
"dependencies": {
"cssstyle": "^4.1.0",
"data-urls": "^5.0.0",
@@ -5176,9 +5140,6 @@
"cpu": [
"arm64"
],
- "libc": [
- "glibc"
- ],
"license": "MPL-2.0",
"optional": true,
"os": [
@@ -5199,9 +5160,6 @@
"cpu": [
"arm64"
],
- "libc": [
- "musl"
- ],
"license": "MPL-2.0",
"optional": true,
"os": [
@@ -5222,9 +5180,6 @@
"cpu": [
"x64"
],
- "libc": [
- "glibc"
- ],
"license": "MPL-2.0",
"optional": true,
"os": [
@@ -5245,9 +5200,6 @@
"cpu": [
"x64"
],
- "libc": [
- "musl"
- ],
"license": "MPL-2.0",
"optional": true,
"os": [
@@ -5383,7 +5335,7 @@
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/meshoptimizer/-/meshoptimizer-1.0.1.tgz",
"integrity": "sha512-Vix+QlA1YYT3FwmBBZ+49cE5y/b+pRrcXKqGpS5ouh33d3lSp2PoTpCw19E0cKDFWalembrHnIaZetf27a+W2g==",
- "dev": true,
+ "devOptional": true,
"license": "MIT"
},
"node_modules/mime-db": {
@@ -6500,7 +6452,8 @@
"version": "0.183.2",
"resolved": "https://registry.npmjs.org/three/-/three-0.183.2.tgz",
"integrity": "sha512-di3BsL2FEQ1PA7Hcvn4fyJOlxRRgFYBpMTcyOgkwJIaDOdJMebEFPA+t98EvjuljDx4hNulAGwF6KIjtwI5jgQ==",
- "license": "MIT"
+ "license": "MIT",
+ "peer": true
},
"node_modules/tinybench": {
"version": "2.9.0",
@@ -6763,6 +6716,7 @@
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
"dev": true,
"license": "Apache-2.0",
+ "peer": true,
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
@@ -6828,6 +6782,7 @@
"resolved": "https://registry.npmjs.org/unenv/-/unenv-2.0.0-rc.24.tgz",
"integrity": "sha512-i7qRCmY42zmCwnYlh9H2SvLEypEFGye5iRmEMKjcGi7zk9UquigRjFtTLz0TYqr0ZGLZhaMHl/foy1bZR+Cwlw==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"pathe": "^2.0.3"
}
@@ -6853,6 +6808,7 @@
"resolved": "https://registry.npmjs.org/vite/-/vite-8.0.3.tgz",
"integrity": "sha512-B9ifbFudT1TFhfltfaIPgjo9Z3mDynBTJSUYxTjOQruf/zHH+ezCQKcoqO+h7a9Pw9Nm/OtlXAiGT1axBgwqrQ==",
"license": "MIT",
+ "peer": true,
"dependencies": {
"lightningcss": "^1.32.0",
"picomatch": "^4.0.4",
@@ -6954,6 +6910,7 @@
"integrity": "sha512-xjR1dMTVHlFLh98JE3i/f/WePqJsah4A0FK9cc8Ehp9Udk0AZk6ccpIZhh1qJ/yxVWRZ+Q54ocnD8TXmkhspGg==",
"dev": true,
"license": "MIT",
+ "peer": true,
"dependencies": {
"@vitest/expect": "4.1.2",
"@vitest/mocker": "4.1.2",
@@ -7236,6 +7193,7 @@
"integrity": "sha512-ZuEq1OdrJBS+NV+L5HMYPCzVn49a2O60slQiiLpG44jqtlOo+S167fWC76kEXteXLLLydeuRrluRel7WdOUa4g==",
"hasInstallScript": true,
"license": "Apache-2.0",
+ "peer": true,
"bin": {
"workerd": "bin/workerd"
},
diff --git a/package.json b/package.json
index 8b83971..0df7064 100644
--- a/package.json
+++ b/package.json
@@ -26,6 +26,7 @@
"vitest": "^4.1.0"
},
"dependencies": {
+ "@comfyorg/fbx-exporter-three": "^1.0.1",
"file-saver": "^2.0.5",
"jszip": "^3.10.1",
"three": "^0.183.0",
diff --git a/src/create.html b/src/create.html
index e291adc..6d159ff 100644
--- a/src/create.html
+++ b/src/create.html
@@ -457,7 +457,7 @@
<div class="download-combo">
<button
id="export-button"
- data-tippy-content="Exporting will combine all animations into a single GLB file."
+ 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" />
@@ -493,6 +493,30 @@
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"
diff --git a/src/index.html b/src/index.html
index 083de8d..363e923 100644
--- a/src/index.html
+++ b/src/index.html
@@ -164,7 +164,7 @@
<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 animations into a single GLB file.">
+ <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>
@@ -201,6 +201,17 @@
</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">
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.ts
index a4c90a7..4c1102e 100644
--- a/src/lib/EventListeners.ts
+++ b/src/lib/EventListeners.ts
@@ -6,6 +6,7 @@ import { Utility } from './Utilities'
import { ModelCleanupUtility } from './processes/load-model/ModelCleanupUtility'
import { ModelAnalysisReport } from './processes/load-model/ModelAnalysisReport'
import { DownloadSuccessDialog } from './components/download-success/DownloadSuccessDialog'
+import { ModalDialog } from './ModalDialog.ts'
import { type Bone, type Material, type MeshBasicMaterial, type MeshPhongMaterial, type MeshStandardMaterial } from 'three'
export class EventListeners {
@@ -258,12 +259,18 @@ export class EventListeners {
const export_selections = this.bootstrap.animations_listing_step.get_animation_export_selections()
this.bootstrap.file_export_step.set_animation_clips_to_export(all_clips, export_selections)
- this.bootstrap.file_export_step.export(
+ void this.bootstrap.file_export_step.export(
this.bootstrap.animations_listing_step.active_skinned_meshes(),
'exported-model',
this.bootstrap.download_settings
)
- new DownloadSuccessDialog().show()
+ .then(() => {
+ new DownloadSuccessDialog().show()
+ })
+ .catch((error: unknown) => {
+ const error_message = error instanceof Error ? error.message : 'Failed to export file.'
+ new ModalDialog('Export failed', error_message).show()
+ })
})
// going back to edit skeleton step after skinning
diff --git a/src/lib/processes/export-to-file/DownloadSettings.ts b/src/lib/processes/export-to-file/DownloadSettings.ts
index 3bef625..1534545 100644
--- a/src/lib/processes/export-to-file/DownloadSettings.ts
+++ b/src/lib/processes/export-to-file/DownloadSettings.ts
@@ -10,9 +10,15 @@ export enum ExportContents {
Skeleton = 'skeleton'
}
+export enum ExportFormat {
+ GLB = 'glb',
+ FBX = 'fbx'
+}
+
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 dom_download_settings_popup: HTMLElement | null = null
private dom_download_settings_toggle: HTMLButtonElement | null = null
private dom_download_settings_panel: HTMLElement | null = null
@@ -21,6 +27,8 @@ export class DownloadSettings extends EventTarget {
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
constructor () {
super()
@@ -36,6 +44,10 @@ export class DownloadSettings extends EventTarget {
return this.selected_export_contents
}
+ public export_format (): ExportFormat {
+ return this.selected_export_format
+ }
+
// 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.
@@ -51,6 +63,11 @@ export class DownloadSettings extends EventTarget {
this.dom_export_contents_full_radio.checked = true
}
+ this.selected_export_format = ExportFormat.GLB
+ if (this.dom_export_format_glb_radio !== null) {
+ this.dom_export_format_glb_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.
@@ -68,6 +85,8 @@ export class DownloadSettings extends EventTarget {
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')
}
private add_event_listeners (): void {
@@ -129,6 +148,23 @@ export class DownloadSettings extends EventTarget {
detail: { exportContents: this.selected_export_contents }
}))
})
+
+ this.dom_export_format_group?.addEventListener('change', (event: Event) => {
+ const selected_radio = event.target as HTMLInputElement | null
+
+ if (selected_radio === null || selected_radio.name !== 'export-format') {
+ return
+ }
+
+ this.selected_export_format =
+ selected_radio.value === ExportFormat.FBX
+ ? ExportFormat.FBX
+ : ExportFormat.GLB
+
+ this.dispatchEvent(new CustomEvent('export-format-changed', {
+ detail: { exportFormat: this.selected_export_format }
+ }))
+ })
}
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.ts
index a9a0b4d..72ac7c4 100644
--- a/src/lib/processes/export-to-file/StepExportToFile.ts
+++ b/src/lib/processes/export-to-file/StepExportToFile.ts
@@ -1,15 +1,23 @@
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, ExportContents } from './DownloadSettings.ts'
+import { FBXExporter } from '@comfyorg/fbx-exporter-three'
+import { type AnimationClip, Group, Scene, type SkinnedMesh, type Object3D } from 'three'
+import { type DownloadSettings, ExportContents, ExportFormat } from './DownloadSettings.ts'
import { ExportBoneNamingService } from './ExportBoneNamingService.ts'
import { AnimationUtility } from '../animations-listing/AnimationUtility.ts'
import { type AnimationExportSelection } from '../animations-listing/interfaces/AnimationExportSelection.ts'
+// The FBX format generally uses centimeters as the unit of measurement, while three.js uses meters.
+// To ensure compatibility with 3D software that expects FBX files to be in centimeters,
+// we scale the exported objects down by a factor of 0.01 (1/100). This scaling is applied to
+// the root group of the exported scene, which contains all the objects being exported.
+const FBX_EXPORT_SCALE = .01
+
// Note: EventTarget is a built-in interface and do not need to import it
export class StepExportToFile extends EventTarget {
private readonly ui: UI = UI.getInstance()
private 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 = []
@@ -38,15 +46,21 @@ export class StepExportToFile extends EventTarget {
public export (
skinned_meshes: SkinnedMesh[],
filename: string = 'exported_model',
- download_settings: DownloadSettings): void {
+ download_settings: DownloadSettings): Promise<void> {
if (this.animation_clips_to_export.length === 0) {
console.log('ERROR: No animation clips added to export')
- return
+ return Promise.reject(new Error('No animation clips added to export'))
}
const export_scene = new Scene()
+ // Normal FBX exports use the unit of centimeters, but
+ // three.js uses meters. Multiply everything by 100 to make it more compatible with 3d software
+ const export_root_group = new Group()
+ export_root_group.name = 'fbx-export-root'
+ export_root_group.scale.setScalar(FBX_EXPORT_SCALE)
+
const export_clips = this.animation_clips_to_export.map((clip) => {
const cloned_clip = clip.clone()
// Make action names consistent by replacing any whitespace with underscores
@@ -80,13 +94,22 @@ export class StepExportToFile extends EventTarget {
objects_to_export.forEach((object_to_export) => {
// Save the original parent
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)
+ }
+
console.log('SKINNED MESH DATA TO EXPORT:', skinned_meshes)
console.log('animations to export', export_clips)
- this.export_glb(export_scene, export_clips, filename)
+ return this.export_scene(export_scene, export_clips, filename, download_settings.export_format())
.then(() => {
// Move the exported objects back to their original parents
objects_to_export.forEach((exported_object) => {
@@ -103,12 +126,28 @@ export class StepExportToFile extends EventTarget {
restore_bone_names()
})
.catch((error) => {
- console.log('Error exporting GLB:', error)
+ console.log('Error exporting file:', error)
restore_bone_names()
+
+ throw error
})
}
+ 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> {
await new Promise<void>((resolve, reject) => {
const gltf_exporter = new GLTFExporter()
@@ -141,6 +180,24 @@ export class StepExportToFile extends EventTarget {
})
}
+ public async export_fbx (exported_scene: Scene, animations_to_export: AnimationClip[], file_name: string): Promise<void> {
+ exported_scene.animations = animations_to_export
+
+ try {
+ const result = await this.fbx_exporter.parseAsync(exported_scene, {
+ preset: 'unreal',
+ includeAnimations: true,
+ animations: animations_to_export,
+ onlyVisible: false,
+ embedTextures: true
+ })
+
+ this.save_uint8_array(result, `${file_name}.fbx`)
+ } finally {
+ exported_scene.animations = []
+ }
+ }
+
private save_file (blob: Blob, filename: string): void {
if (this.ui.dom_export_button_hidden_link != null) {
this.ui.dom_export_button_hidden_link.href = URL.createObjectURL(blob)
@@ -155,4 +212,9 @@ export class StepExportToFile extends EventTarget {
private save_array_buffer (buffer: ArrayBuffer, filename: string): void {
this.save_file(new Blob([buffer], { type: 'application/octet-stream' }), filename)
}
+
+ // used for FBX exporter to turn content into a byte array for saving
+ private save_uint8_array (buffer: Uint8Array, filename: string): void {
+ this.save_file(new Blob([buffer], { type: 'application/octet-stream' }), filename)
+ }
}