Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Remove position data for retarget animations. save a reference what the rig type is if there is a bone mapping.

Scott Petrovic21c5b2039306
diff --git a/src/retarget/BoneAutoMapper.ts b/src/retarget/BoneAutoMapper.ts
index 811efde..6f8e99b 100644
--- a/src/retarget/BoneAutoMapper.ts
+++ b/src/retarget/BoneAutoMapper.ts
@@ -1,6 +1,7 @@
import { Bone, Group, Object3D, SkinnedMesh } from 'three'
import { BoneCategoryMapper } from './BoneCategoryMapper'
import { MixamoMapper } from './MixamoMapper'
+import { TargetBoneMappingType } from './StepBoneMapping'
/**
* Bone categories for grouping bones by anatomical area
@@ -51,7 +52,8 @@ export class BoneAutoMapper {
*/
public static auto_map_bones (
source_armature: Object3D,
- target_skeleton_data: Group
+ target_skeleton_data: Group,
+ target_bone_mapping_type: TargetBoneMappingType
): Map<string, string> {
// mappings: final output mapping of target bone name to source bone name
let mappings = new Map<string, string>()
@@ -72,8 +74,7 @@ export class BoneAutoMapper {
// if the target is a mixamo rig and our skeleton type is human, we can do a direct name mapping
// without worrying about guessing
- const is_target_mixamo_rig: boolean = target_bones_meta.some(bone => bone.name.toLowerCase().includes('mixamorig'))
- if (is_target_mixamo_rig) {
+ if (target_bone_mapping_type === TargetBoneMappingType.Mixamo) {
console.log('Target skeleton appears to be a Mixamo rig, performing direct name mapping...')
mappings = MixamoMapper.map_mixamo_bones(source_bones_meta, target_bones_meta)
return mappings
diff --git a/src/retarget/MixamoMapper.ts b/src/retarget/MixamoMapper.ts
index 0e4d57f..61bd93a 100644
--- a/src/retarget/MixamoMapper.ts
+++ b/src/retarget/MixamoMapper.ts
@@ -106,6 +106,15 @@ export class MixamoMapper {
'DEF-f_pinky04_tipR': 'mixamorigRightHandPinky4'
}
+ /**
+ * Check if the given skeleton is a Mixamo skeleton
+ * @param bones - Bones to check
+ * @returns True if any bone name contains "mixamorig"
+ */
+ static is_target_valid_skeleton (bone_names: string[]): boolean {
+ return bone_names.some(name => name.toLowerCase().includes('mixamorig'))
+ }
+
/**
* Map Mesh2Motion bones to Mixamo bones
* @param source_bones - Mesh2Motion skeleton bones
diff --git a/src/retarget/RetargetAnimationPreview.ts b/src/retarget/RetargetAnimationPreview.ts
index 63f5f3e..866a1f6 100644
--- a/src/retarget/RetargetAnimationPreview.ts
+++ b/src/retarget/RetargetAnimationPreview.ts
@@ -1,6 +1,7 @@
-import { AnimationClip, AnimationMixer, type Scene, type SkinnedMesh, VectorKeyframeTrack, QuaternionKeyframeTrack, AnimationAction } from 'three'
+import { AnimationClip, AnimationMixer, type Scene, type SkinnedMesh, VectorKeyframeTrack, QuaternionKeyframeTrack, type AnimationAction } from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { SkeletonType } from '../lib/enums/SkeletonType.ts'
+import { AnimationUtility } from '../lib/processes/animations-listing/AnimationUtility.ts'
import type { StepBoneMapping } from './StepBoneMapping.ts'
/**
@@ -115,6 +116,10 @@ export class RetargetAnimationPreview extends EventTarget {
this.current_animation_clip = gltf.animations[0]
if (this.current_animation_clip !== null) {
console.log('Loaded animation:', this.current_animation_clip.name)
+
+ // Clean up the animation track data before using it
+ // This removes unnecessary position tracks and keeps only rotation data
+ AnimationUtility.clean_track_data([this.current_animation_clip])
}
this.update_preview_animation()
resolve()
diff --git a/src/retarget/StepBoneMapping.ts b/src/retarget/StepBoneMapping.ts
index 0da8c5b..7f2d1dd 100644
--- a/src/retarget/StepBoneMapping.ts
+++ b/src/retarget/StepBoneMapping.ts
@@ -1,10 +1,22 @@
import { type Group, type Object3D, type Object3DEventMap, type Scene, type SkinnedMesh } from 'three'
import { SkeletonType } from '../lib/enums/SkeletonType.ts'
import { BoneAutoMapper } from './BoneAutoMapper.ts'
+import { MixamoMapper } from './MixamoMapper.ts'
+
+// when we are auto-mapping, keep track of what rig type we matched target against
+export enum TargetBoneMappingType {
+ Mixamo = 'mixamo',
+ Mesh2Motion = 'mesh2motion',
+ Custom = 'custom', // trying to figure out bone mappings with bone name analysis
+ None = 'none'
+ // TODO: Add more types later
+}
export class StepBoneMapping extends EventTarget {
private readonly _main_scene: Scene
private target_skeleton_data: Group<Object3DEventMap> | null = null
+ private target_mapping_template: TargetBoneMappingType = TargetBoneMappingType.None
+
private source_armature: Object3D | null = null
private source_skeleton_type: SkeletonType = SkeletonType.None
@@ -339,10 +351,18 @@ export class StepBoneMapping extends EventTarget {
return
}
+ // see if target bones follow a known template
+ if (MixamoMapper.is_target_valid_skeleton(this.get_target_bone_names())) {
+ this.target_mapping_template = TargetBoneMappingType.Mixamo
+ } else {
+ this.target_mapping_template = TargetBoneMappingType.Custom
+ }
+
// Use BoneAutoMapper to generate mappings
const auto_mappings = BoneAutoMapper.auto_map_bones(
- this.source_armature!,
- this.target_skeleton_data!
+ this.source_armature,
+ this.target_skeleton_data,
+ this.target_mapping_template
)
// Apply the auto-generated mappings
@@ -351,8 +371,19 @@ export class StepBoneMapping extends EventTarget {
console.log(`Auto-mapped ${auto_mappings.size} bones:`, auto_mappings)
// Update UI
+ this.polish_rig_after_bone_mappings()
this.update_target_bones_list()
this.update_clear_button_visibility()
this.dispatchEvent(new CustomEvent('bone-mappings-changed'))
}
+
+ // some rig types don't have a root (like mixamo), so we need to rotate
+ // certain bones to get the model standing upright after retargeting
+ private polish_rig_after_bone_mappings (): void {
+ if (this.target_mapping_template === TargetBoneMappingType.Mixamo) {
+ console.log('Applying Mixamo rig polish adjustments since there is no root bone')
+ // for mixamo rigs, rotate the entire group by -90 degrees on X to get upright
+ // TODO: how to do this??
+ }
+ }
}