Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Start hooking up functionality with exporting out different human bone names

Scott Petrovic054eee479575
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.ts
index 3f35729..94c24e7 100644
--- a/src/lib/EventListeners.ts
+++ b/src/lib/EventListeners.ts
@@ -198,7 +198,11 @@ export class EventListeners {
const animations_to_export: number[] = this.bootstrap.animations_listing_step.get_animation_indices_to_export()
this.bootstrap.file_export_step.set_animation_clips_to_export(all_clips, animations_to_export)
- this.bootstrap.file_export_step.export(this.bootstrap.animations_listing_step.active_skinned_meshes(), 'exported-model')
+ this.bootstrap.file_export_step.export(
+ this.bootstrap.animations_listing_step.active_skinned_meshes(),
+ 'exported-model',
+ this.bootstrap.download_settings
+ )
})
// going back to edit skeleton step after skinning
diff --git a/src/lib/processes/export-to-file/ExportBoneNamingService.ts b/src/lib/processes/export-to-file/ExportBoneNamingService.ts
new file mode 100644
index 0000000..b1ab05d
--- /dev/null
+++ b/src/lib/processes/export-to-file/ExportBoneNamingService.ts
@@ -0,0 +1,91 @@
+import { type AnimationClip, type Object3D, type SkinnedMesh } from 'three'
+import { BoneNamingStructure } from './DownloadSettings.ts'
+import { MixamoMapper } from '../../../retarget/bone-automap/MixamoMapper.ts'
+
+// eslint-disable-next-line @typescript-eslint/no-extraneous-class
+export class ExportBoneNamingService {
+ public static apply_download_settings (
+ skinned_meshes: SkinnedMesh[],
+ animation_clips: AnimationClip[],
+ bone_naming_structure: BoneNamingStructure
+ ): () => void {
+ if (bone_naming_structure !== BoneNamingStructure.Mixamo) {
+ return () => {}
+ }
+
+ const renamed_bones = new Map<Object3D, string>()
+ this.rename_bones_to_mixamo(skinned_meshes, renamed_bones)
+ this.rename_animation_tracks_to_mixamo(animation_clips)
+
+ return () => {
+ renamed_bones.forEach((original_name, bone_object) => {
+ bone_object.name = original_name
+ })
+ }
+ }
+
+ private static rename_bones_to_mixamo (skinned_meshes: SkinnedMesh[], renamed_bones: Map<Object3D, string>): void {
+ skinned_meshes.forEach((skinned_mesh) => {
+ skinned_mesh.traverse((scene_object) => {
+ if (scene_object.type !== 'Bone') {
+ return
+ }
+
+ const mixamo_name = MixamoMapper.map_source_bone_name_to_mixamo(scene_object.name)
+ if (mixamo_name === undefined) {
+ return
+ }
+
+ renamed_bones.set(scene_object, scene_object.name)
+ scene_object.name = mixamo_name
+ })
+ })
+ }
+
+ private static rename_animation_tracks_to_mixamo (animation_clips: AnimationClip[]): void {
+ animation_clips.forEach((clip) => {
+ clip.tracks.forEach((track) => {
+ track.name = this.rename_track_name_to_mixamo(track.name)
+ })
+ })
+ }
+
+ private static rename_track_name_to_mixamo (track_name: string): string {
+ const first_dot = track_name.indexOf('.')
+ if (first_dot > 0) {
+ const node_path = track_name.slice(0, first_dot)
+ const mapped_node_path = this.rename_node_path_to_mixamo(node_path)
+ if (mapped_node_path !== node_path) {
+ return `${mapped_node_path}${track_name.slice(first_dot)}`
+ }
+ }
+
+ // handle PropertyBinding names like .bones[bone_name].quaternion
+ return track_name.replace(/(\.bones\["?)([^\]"]+)("?\])/g, (match: string, prefix: string, bone_name: string, suffix: string) => {
+ const mixamo_name = MixamoMapper.map_source_bone_name_to_mixamo(bone_name)
+ if (mixamo_name === undefined) {
+ return match
+ }
+
+ return `${prefix}${mixamo_name}${suffix}`
+ })
+ }
+
+ private static rename_node_path_to_mixamo (node_path: string): string {
+ const direct_mixamo_name = MixamoMapper.map_source_bone_name_to_mixamo(node_path)
+ if (direct_mixamo_name !== undefined) {
+ return direct_mixamo_name
+ }
+
+ const path_parts = node_path.split('/')
+ const node_leaf_name = path_parts[path_parts.length - 1]
+ const mapped_leaf_name = MixamoMapper.map_source_bone_name_to_mixamo(node_leaf_name)
+
+ if (mapped_leaf_name === undefined) {
+ return node_path
+ }
+
+ path_parts[path_parts.length - 1] = mapped_leaf_name
+ return path_parts.join('/')
+ }
+}
\ No newline at end of file
diff --git a/src/lib/processes/export-to-file/StepExportToFile.ts b/src/lib/processes/export-to-file/StepExportToFile.ts
index 92b1d0d..e9edc0f 100644
--- a/src/lib/processes/export-to-file/StepExportToFile.ts
+++ b/src/lib/processes/export-to-file/StepExportToFile.ts
@@ -1,8 +1,10 @@
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 { ExportBoneNamingService } from './ExportBoneNamingService.ts'
-// Note: EventTarget is a built-ininterface and do not need to import it
+// 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[] = []
@@ -16,7 +18,11 @@ export class StepExportToFile extends EventTarget {
})
}
- public export (skinned_meshes: SkinnedMesh[], filename = 'exported_model'): void {
+ public export (
+ skinned_meshes: SkinnedMesh[],
+ filename: string = 'exported_model',
+ download_settings: DownloadSettings): void {
+
if (this.animation_clips_to_export.length === 0) {
console.log('ERROR: No animation clips added to export')
return
@@ -35,10 +41,17 @@ export class StepExportToFile extends EventTarget {
export_scene.add(final_skinned_mesh)
})
+ const export_clips = this.animation_clips_to_export.map((clip) => clip.clone())
+ const restore_bone_names = ExportBoneNamingService.apply_download_settings(
+ skinned_meshes,
+ export_clips,
+ download_settings.bone_naming_structure()
+ )
+
console.log('SKINNED MESH DATA TO EXPORT:', skinned_meshes)
- console.log('animations to export', this.animation_clips_to_export)
+ console.log('animations to export', export_clips)
- this.export_glb(export_scene, this.animation_clips_to_export, filename)
+ this.export_glb(export_scene, export_clips, filename)
.then(() => {
// Move the skinned meshes back to their original parents
skinned_meshes.forEach((final_skinned_mesh) => {
@@ -51,8 +64,14 @@ export class StepExportToFile extends EventTarget {
console.log('ERROR: No original parent found for skinned mesh when exporting and re-parenting to original scene')
}
})
+
+ restore_bone_names()
+ })
+ .catch((error) => {
+ console.log('Error exporting GLB:', error)
+
+ restore_bone_names()
})
- .catch((error) => { console.log('Error exporting GLB:', error) })
}
public async export_glb (exported_scene: Scene, animations_to_export: AnimationClip[], file_name: string): Promise<void> {
diff --git a/src/retarget/bone-automap/MixamoMapper.ts b/src/retarget/bone-automap/MixamoMapper.ts
index 948c241..ef895c6 100644
--- a/src/retarget/bone-automap/MixamoMapper.ts
+++ b/src/retarget/bone-automap/MixamoMapper.ts
@@ -115,6 +115,13 @@ export class MixamoMapper {
return bone_names.some(name => name.toLowerCase().includes('mixamorig'))
}
+ /**
+ * Get the expected Mixamo bone name for a Mesh2Motion bone name.
+ */
+ static map_source_bone_name_to_mixamo (source_bone_name: string): string | undefined {
+ return this.BONE_MAP[source_bone_name]
+ }
+
/**
* Map Mesh2Motion bones to Mixamo bones
* @param source_bones - Mesh2Motion skeleton bones