Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Fix Unity FBX exports facing the opposite direction

Scott Petrovic999f612a8427
diff --git a/src/lib/processes/export-to-file/DownloadSettings.ts b/src/lib/processes/export-to-file/DownloadSettings.ts
index 33fc5b6..327e4fc 100644
--- a/src/lib/processes/export-to-file/DownloadSettings.ts
+++ b/src/lib/processes/export-to-file/DownloadSettings.ts
@@ -23,6 +23,12 @@ export enum FbxExportPreset {
// Maya = 'maya'
}
+// The exporter's 'unity' preset writes FrontAxisSign/CoordAxisSign = -1, which Unity reads
+// as a 180° turn, so exports faced backwards. '-Z' writes the standard Maya/Mixamo header.
+export function fbx_export_axis_overrides (preset: FbxExportPreset): { axisForward?: '-Z' } {
+ return preset === FbxExportPreset.Unity ? { axisForward: '-Z' } : {}
+}
+
export class DownloadSettings extends EventTarget {
private selected_bone_naming_structure: BoneNamingStructure = this.get_default_bone_naming_structure()
private selected_export_contents: ExportContents = this.get_default_export_contents()
diff --git a/src/lib/processes/export-to-file/StepExportToFile.ts b/src/lib/processes/export-to-file/StepExportToFile.ts
index 6cd9319..5b62c25 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, type FbxExportPreset } from './DownloadSettings.ts'
+import { type DownloadSettings, ExportContents, ExportFormat, type FbxExportPreset, fbx_export_axis_overrides } from './DownloadSettings.ts'
import { ExportBoneNamingService } from './ExportBoneNamingService.ts'
import { ExportHierarchyService } from './ExportHierarchyService.ts'
import { ExportAnimationCleanupService } from './ExportAnimationCleanupService.ts'
@@ -187,6 +187,7 @@ export class StepExportToFile extends EventTarget {
try {
const result = await this.fbx_exporter.parseAsync(exported_scene, {
preset: fbx_export_preset,
+ ...fbx_export_axis_overrides(fbx_export_preset),
includeAnimations: true,
animations: animations_to_export,
onlyVisible: false,
diff --git a/src/retarget/steps/StepExportRetargetedAnimations.ts b/src/retarget/steps/StepExportRetargetedAnimations.ts
index 32a703f..5c99774 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 AnimationClipExportSelection } from '../../lib/processes/animations-listing/interfaces/AnimationExportSelection'
-import { type DownloadSettings, ExportContents, ExportFormat, type FbxExportPreset } from '../../lib/processes/export-to-file/DownloadSettings'
+import { type DownloadSettings, ExportContents, ExportFormat, type FbxExportPreset, fbx_export_axis_overrides } from '../../lib/processes/export-to-file/DownloadSettings'
import { ExportBoneNamingService } from '../../lib/processes/export-to-file/ExportBoneNamingService'
import { ExportHierarchyService } from '../../lib/processes/export-to-file/ExportHierarchyService'
import { ExportAnimationCleanupService } from '../../lib/processes/export-to-file/ExportAnimationCleanupService'
@@ -165,6 +165,7 @@ export class StepExportRetargetedAnimations extends EventTarget {
try {
const result = await this.fbx_exporter.parseAsync(exported_scene, {
preset: fbx_export_preset,
+ ...fbx_export_axis_overrides(fbx_export_preset),
includeAnimations: true,
animations: animations_to_export,
onlyVisible: false,