Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Move some calculations for retargeting to static util functions

Scott Petrovic6db6b14ca9bd
diff --git a/src/retarget/AnimationRetargetService.ts b/src/retarget/AnimationRetargetService.ts
index 5e990c3..a5f5101 100644
--- a/src/retarget/AnimationRetargetService.ts
+++ b/src/retarget/AnimationRetargetService.ts
@@ -1,8 +1,7 @@
import {
- AnimationClip, type Object3D, QuaternionKeyframeTrack,
+ AnimationClip, QuaternionKeyframeTrack,
VectorKeyframeTrack, Scene, Group, type SkinnedMesh,
- Skeleton,
- type Bone
+ type Skeleton
} from 'three'
import { RetargetUtils, type TrackNameParts } from './RetargetUtils.ts'
import { TargetBoneMappingType } from './steps/StepBoneMapping.ts'
@@ -171,9 +170,9 @@ export class AnimationRetargetService {
// #region PRIVATE METHODS
private apply_human_swing_twist_retargeting (source_clip: AnimationClip, target_mapping_type: TargetBoneMappingType): AnimationClip {
- // the retargeter needs Skeleton inputs fot both source and target.
+ // the retargeter needs Skeleton inputs for both source and target.
// the source armature is a Group, so we need to convert to a THREE.Skeleton before we can continue
- const source_skeleton: Skeleton | null = this.create_skeleton_from_source(this.source_armature)
+ const source_skeleton: Skeleton | null = RetargetUtils.create_skeleton_from_group_object(this.source_armature)
if (source_skeleton === null) {
console.error('Failed to extract source skeleton from source armature for Human Retargeter.')
return source_clip.clone()
@@ -184,7 +183,9 @@ export class AnimationRetargetService {
return source_clip.clone()
}
- const detached_target_skeleton: Skeleton = this.clone_skeleton(this.target_skinned_meshes[0].skeleton)
+ // we don't want to mutate/modify the original target skeleton since it is shared
+ // across the app and will create issues as we change animations for retargeting later.
+ const detached_target_skeleton: Skeleton = RetargetUtils.clone_skeleton(this.target_skinned_meshes[0].skeleton)
// create a custom "Rig" for the source and the target skeletons
const source_rig: Rig = new Rig(source_skeleton)
@@ -233,103 +234,5 @@ export class AnimationRetargetService {
return retargeted_clip
}
- /**
- * Utility: Convert a Group (with Armature and Bone hierarchy) to a THREE.Skeleton
- * @param group - The root Group containing the Armature and Bone hierarchy
- * @returns Skeleton or null if not found
- */
- private create_skeleton_from_source (group: Group): Skeleton | null {
- // Find the Armature child
- const armature = group.children.find(child => child.type === 'Object3D' &&
- child.name.toLowerCase().includes('armature'))
-
- if (armature === undefined) return null
-
- // Find the root Bone under the Armature
- const root_bone = armature.children.find(child => child.type === 'Bone') as Bone | undefined
-
- if (root_bone === undefined) return null
-
- const detached_armature = armature.clone(true)
- const bones = this.collect_bones(detached_armature)
-
- if (bones.length === 0) return null
-
- const skeleton = new Skeleton(bones)
- skeleton.calculateInverses()
- skeleton.pose()
- return skeleton
- }
-
- private clone_skeleton (source_skeleton: Skeleton): Skeleton {
- const original_to_clone = new Map<Bone, Bone>()
-
- const root_bones = source_skeleton.bones.filter((bone) =>
- bone.parent === null || bone.parent.type !== 'Bone'
- )
-
- const detached_parent_cache = new Map<Object3D, Object3D>()
-
- const ensure_detached_parent = (source_parent: Object3D): Object3D => {
- const cached_parent = detached_parent_cache.get(source_parent)
- if (cached_parent !== undefined) {
- return cached_parent
- }
-
- const detached_parent = new Group()
- source_parent.updateWorldMatrix(true, false)
- source_parent.matrixWorld.decompose(detached_parent.position, detached_parent.quaternion, detached_parent.scale)
- detached_parent.updateMatrixWorld(true)
-
- detached_parent_cache.set(source_parent, detached_parent)
- return detached_parent
- }
-
- root_bones.forEach((root_bone) => {
- const cloned_root = root_bone.clone(true)
-
- if (root_bone.parent !== null && root_bone.parent.type !== 'Bone') {
- const detached_parent = ensure_detached_parent(root_bone.parent)
- detached_parent.add(cloned_root)
- }
-
- const stack: Array<{ original: Bone, cloned: Bone }> = [{ original: root_bone, cloned: cloned_root }]
-
- while (stack.length > 0) {
- const pair = stack.pop()
- if (pair === undefined) continue
-
- original_to_clone.set(pair.original, pair.cloned)
-
- const original_children = pair.original.children.filter(child => child.type === 'Bone') as Bone[]
- const cloned_children = pair.cloned.children.filter(child => child.type === 'Bone') as Bone[]
-
- for (let i = 0; i < original_children.length; i++) {
- stack.push({
- original: original_children[i],
- cloned: cloned_children[i]
- })
- }
- }
- })
-
- const cloned_bones = source_skeleton.bones
- .map((bone) => original_to_clone.get(bone))
- .filter((bone): bone is Bone => bone !== undefined)
-
- const cloned_bone_inverses = source_skeleton.boneInverses.map((inverse) => inverse.clone())
- const cloned_skeleton = new Skeleton(cloned_bones, cloned_bone_inverses)
- cloned_skeleton.pose()
-
- return cloned_skeleton
- }
-
- // Recursively collect all bones. part of extract_skeleton_from_group()
- private collect_bones (object: Object3D, bones: Bone[] = []): Bone[] {
- if (object.type === 'Bone') bones.push(object as Bone)
- object.children.forEach(child => this.collect_bones(child, bones))
- return bones
- }
-
// #endregion
}
diff --git a/src/retarget/RetargetUtils.ts b/src/retarget/RetargetUtils.ts
index a8d9598..dea88ca 100644
--- a/src/retarget/RetargetUtils.ts
+++ b/src/retarget/RetargetUtils.ts
@@ -1,4 +1,4 @@
-import { type Scene, type Group, type Skeleton, type SkinnedMesh } from 'three'
+import { type Scene, Group, Skeleton, type SkinnedMesh, type Bone, type Object3D } from 'three'
import { ModalDialog } from '../lib/ModalDialog.ts'
import { SkeletonType } from '../lib/enums/SkeletonType.ts'
@@ -9,6 +9,39 @@ export interface TrackNameParts {
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class RetargetUtils {
+ /**
+ * Convert a Group (with Armature and Bone hierarchy) to a detached THREE.Skeleton
+ * @param group The root Group containing the Armature and Bone hierarchy
+ * @returns Skeleton or null if not found
+ */
+ static create_skeleton_from_group_object (group: Group): Skeleton | null {
+ const armature = group.children.find(child => child.type === 'Object3D' &&
+ child.name.toLowerCase().includes('armature'))
+
+ if (armature === undefined) return null
+
+ const root_bone = armature.children.find(child => child.type === 'Bone') as Bone | undefined
+ if (root_bone === undefined) return null
+
+ const detached_armature = armature.clone(true)
+ const bones = this.collect_bones(detached_armature)
+ if (bones.length === 0) return null
+
+ const skeleton = new Skeleton(bones)
+ skeleton.calculateInverses()
+ skeleton.pose()
+ return skeleton
+ }
+
+ /**
+ * Recursively collect all bones from an Object3D subtree.
+ */
+ static collect_bones (object: Object3D, bones: Bone[] = []): Bone[] {
+ if (object.type === 'Bone') bones.push(object as Bone)
+ object.children.forEach(child => this.collect_bones(child, bones))
+ return bones
+ }
+
/**
* Resets all SkinnedMeshes in the group to their rest pose
*/
@@ -152,4 +185,76 @@ export class RetargetUtils {
return reverse_mappings
}
+
+ /**
+ * Clone a skeleton into a detached working copy for retargeting.
+ *
+ * Why this exists instead of the native three.js `Skeleton.clone()`:
+ * - `Skeleton.clone()` does not guarantee a fully detached bone hierarchy suitable for isolated edits.
+ * - Our retargeting path needs stable non-bone root parent world transforms for pose offsets.
+ * - This function deep-clones bone hierarchies and recreates detached non-bone root parents using
+ * decomposed world transforms, preventing mutation of live scene bones.
+ */
+ static clone_skeleton (source_skeleton: Skeleton): Skeleton {
+ const original_to_clone = new Map<Bone, Bone>()
+
+ const root_bones = source_skeleton.bones.filter((bone) =>
+ bone.parent === null || bone.parent.type !== 'Bone'
+ )
+
+ const detached_parent_cache = new Map<Object3D, Object3D>()
+
+ const ensure_detached_parent = (source_parent: Object3D): Object3D => {
+ const cached_parent = detached_parent_cache.get(source_parent)
+ if (cached_parent !== undefined) {
+ return cached_parent
+ }
+
+ const detached_parent = new Group()
+ source_parent.updateWorldMatrix(true, false)
+ source_parent.matrixWorld.decompose(detached_parent.position, detached_parent.quaternion, detached_parent.scale)
+ detached_parent.updateMatrixWorld(true)
+
+ detached_parent_cache.set(source_parent, detached_parent)
+ return detached_parent
+ }
+
+ root_bones.forEach((root_bone) => {
+ const cloned_root = root_bone.clone(true)
+
+ if (root_bone.parent !== null && root_bone.parent.type !== 'Bone') {
+ const detached_parent = ensure_detached_parent(root_bone.parent)
+ detached_parent.add(cloned_root)
+ }
+
+ const stack: Array<{ original: Bone, cloned: Bone }> = [{ original: root_bone, cloned: cloned_root }]
+
+ while (stack.length > 0) {
+ const pair = stack.pop()
+ if (pair === undefined) continue
+
+ original_to_clone.set(pair.original, pair.cloned)
+
+ const original_children = pair.original.children.filter(child => child.type === 'Bone') as Bone[]
+ const cloned_children = pair.cloned.children.filter(child => child.type === 'Bone') as Bone[]
+
+ for (let i = 0; i < original_children.length; i++) {
+ stack.push({
+ original: original_children[i],
+ cloned: cloned_children[i]
+ })
+ }
+ }
+ })
+
+ const cloned_bones = source_skeleton.bones
+ .map((bone) => original_to_clone.get(bone))
+ .filter((bone): bone is Bone => bone !== undefined)
+
+ const cloned_bone_inverses = source_skeleton.boneInverses.map((inverse) => inverse.clone())
+ const cloned_skeleton = new Skeleton(cloned_bones, cloned_bone_inverses)
+ cloned_skeleton.pose()
+
+ return cloned_skeleton
+ }
}