Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

remove manual scaling for FBX for now

Scott Petrovicd61749eeb75b
diff --git a/src/lib/processes/export-to-file/StepExportToFile.ts b/src/lib/processes/export-to-file/StepExportToFile.ts
index 72ac7c4..f29532a 100644
--- a/src/lib/processes/export-to-file/StepExportToFile.ts
+++ b/src/lib/processes/export-to-file/StepExportToFile.ts
@@ -1,18 +1,12 @@
import { UI } from '../../UI.ts'
import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter.js'
import { FBXExporter } from '@comfyorg/fbx-exporter-three'
-import { type AnimationClip, Group, Scene, type SkinnedMesh, type Object3D } from 'three'
+import { type AnimationClip, 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()
@@ -55,12 +49,6 @@ export class StepExportToFile extends EventTarget {
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
@@ -94,18 +82,9 @@ 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)
diff --git a/src/retarget/steps/StepExportRetargetedAnimations.ts b/src/retarget/steps/StepExportRetargetedAnimations.ts
index 563e135..d352837 100644
--- a/src/retarget/steps/StepExportRetargetedAnimations.ts
+++ b/src/retarget/steps/StepExportRetargetedAnimations.ts
@@ -1,4 +1,4 @@
-import { Group, Scene, type AnimationClip, type Object3D, type SkinnedMesh } from 'three'
+import { Scene, type AnimationClip, type Object3D, type SkinnedMesh } from 'three'
import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter'
import { FBXExporter } from '@comfyorg/fbx-exporter-three'
import { AnimationRetargetService } from '../AnimationRetargetService'
@@ -7,8 +7,6 @@ import { type AnimationExportSelection } from '../../lib/processes/animations-li
import { type DownloadSettings, ExportContents, ExportFormat } from '../../lib/processes/export-to-file/DownloadSettings'
import { ExportBoneNamingService } from '../../lib/processes/export-to-file/ExportBoneNamingService'
-const FBX_EXPORT_SCALE = 100
-
export class StepExportRetargetedAnimations extends EventTarget {
public animation_clips_to_export: AnimationClip[] = []
private readonly fbx_exporter: FBXExporter = new FBXExporter()
@@ -62,9 +60,6 @@ export class StepExportRetargetedAnimations extends EventTarget {
download_settings: DownloadSettings
): Promise<void> {
const export_scene = new Scene()
- const export_root_group = new Group()
- export_root_group.name = 'retarget-fbx-export-root'
- export_root_group.scale.setScalar(FBX_EXPORT_SCALE)
const restore_bone_names = ExportBoneNamingService.apply_download_settings(
skinned_meshes,
@@ -81,19 +76,9 @@ export class StepExportRetargetedAnimations extends EventTarget {
objects_to_export.forEach((object_to_export) => {
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)
- }
-
try {
await this.export_scene(export_scene, animations_to_export, file_name, download_settings.export_format())
} finally {
@@ -157,22 +142,10 @@ export class StepExportRetargetedAnimations extends EventTarget {
}
public async export_fbx (exported_scene: Scene, animations_to_export: AnimationClip[], file_name: string): Promise<void> {
- const export_scene = new Group()
- export_scene.name = 'retarget-fbx-export-root'
- export_scene.scale.setScalar(FBX_EXPORT_SCALE)
-
- const original_parents = new Map<Group | Scene, Group | Scene | null>()
- const root_children = [...exported_scene.children]
-
- root_children.forEach((child) => {
- original_parents.set(child as Group | Scene, child.parent as Group | Scene | null)
- export_scene.add(child)
- })
-
- export_scene.animations = animations_to_export
+ exported_scene.animations = animations_to_export
try {
- const result = await this.fbx_exporter.parseAsync(export_scene, {
+ const result = await this.fbx_exporter.parseAsync(exported_scene, {
preset: 'unreal',
includeAnimations: true,
animations: animations_to_export,
@@ -182,16 +155,7 @@ export class StepExportRetargetedAnimations extends EventTarget {
this.save_uint8_array(result, `${file_name}.fbx`)
} finally {
- export_scene.animations = []
-
- root_children.forEach((child) => {
- const original_parent = original_parents.get(child as Group | Scene)
- if (original_parent !== null && original_parent !== undefined) {
- original_parent.add(child)
- } else {
- exported_scene.add(child)
- }
- })
+ exported_scene.animations = []
}
}