Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Have position tracking bone be on the rig since there are variations by skeleton

Scott Petrovic8ee33f82d9e4
diff --git a/src/lib/RigConfig.ts b/src/lib/RigConfig.ts
index f95f0ec..f3c4872 100644
--- a/src/lib/RigConfig.ts
+++ b/src/lib/RigConfig.ts
@@ -10,6 +10,9 @@ export interface RigConfigEntry {
has_hand_options: boolean // Only Human has per-finger hand skeleton options
has_head_weight_correction: boolean // Only Human shows the head weight correction panel
has_arm_extension: boolean // Only Human shows the arm expand/contract slider
+ // The bone used for position tracking (e.g., 'hips' or 'head').
+ // we only have one bone per rig that we allow position keyframes (besides root)
+ position_tracking_bone_name: string
}
/**
@@ -29,7 +32,8 @@ export class RigConfig {
animation_preview_folder: 'human',
has_hand_options: true,
has_head_weight_correction: true,
- has_arm_extension: true
+ has_arm_extension: true,
+ position_tracking_bone_name: 'pelvis',
} satisfies RigConfigEntry,
{
skeleton_type: SkeletonType.Fox,
@@ -40,7 +44,8 @@ export class RigConfig {
animation_preview_folder: 'fox',
has_hand_options: false,
has_head_weight_correction: false,
- has_arm_extension: false
+ has_arm_extension: false,
+ position_tracking_bone_name: 'hips',
} satisfies RigConfigEntry,
{
skeleton_type: SkeletonType.Bird,
@@ -51,7 +56,8 @@ export class RigConfig {
animation_preview_folder: 'bird',
has_hand_options: false,
has_head_weight_correction: false,
- has_arm_extension: false
+ has_arm_extension: false,
+ position_tracking_bone_name: 'hips',
} satisfies RigConfigEntry,
{
skeleton_type: SkeletonType.Dragon,
@@ -62,7 +68,8 @@ export class RigConfig {
animation_preview_folder: 'dragon',
has_hand_options: false,
has_head_weight_correction: false,
- has_arm_extension: false
+ has_arm_extension: false,
+ position_tracking_bone_name: 'hips',
} satisfies RigConfigEntry,
{
skeleton_type: SkeletonType.Kaiju,
@@ -73,7 +80,8 @@ export class RigConfig {
animation_preview_folder: 'kaiju',
has_hand_options: false,
has_head_weight_correction: false,
- has_arm_extension: false
+ has_arm_extension: false,
+ position_tracking_bone_name: 'hips',
} satisfies RigConfigEntry,
{
skeleton_type: SkeletonType.Spider,
@@ -84,7 +92,8 @@ export class RigConfig {
animation_preview_folder: 'spider',
has_hand_options: false,
has_head_weight_correction: false,
- has_arm_extension: false
+ has_arm_extension: false,
+ position_tracking_bone_name: 'hips',
} satisfies RigConfigEntry,
{
skeleton_type: SkeletonType.Snake,
@@ -95,7 +104,8 @@ export class RigConfig {
animation_preview_folder: 'snake',
has_hand_options: false,
has_head_weight_correction: false,
- has_arm_extension: false
+ has_arm_extension: false,
+ position_tracking_bone_name: 'head', // snake doesn't have hips, so we track position from the head instead
} satisfies RigConfigEntry
]
diff --git a/src/lib/processes/animations-listing/AnimationLoader.ts b/src/lib/processes/animations-listing/AnimationLoader.ts
index 9816448..3c54fb2 100644
--- a/src/lib/processes/animations-listing/AnimationLoader.ts
+++ b/src/lib/processes/animations-listing/AnimationLoader.ts
@@ -25,6 +25,8 @@ export class AnimationLoader extends EventTarget {
private completed_files: number = 0
private total_files: number = 0
+ private skeleton_type: SkeletonType | null = null
+
private create_default_metadata (): AnimationClipMetadata {
return {
source_type: 'default-library',
@@ -49,11 +51,12 @@ export class AnimationLoader extends EventTarget {
skeleton_type: SkeletonType,
skeleton_scale: number = 1.0
): Promise<TransformedAnimationClipPair[]> {
- const configured_animation_files = RigConfig.get_animation_file_paths(skeleton_type)
+ this.skeleton_type = skeleton_type
+ const configured_animation_files = RigConfig.get_animation_file_paths(this.skeleton_type)
const file_paths = configured_animation_files.map(f => `${this.animations_file_path}${f}`)
if (file_paths.length === 0) {
- throw new Error(`No animation files found for skeleton type: ${skeleton_type}`)
+ throw new Error(`No animation files found for skeleton type: ${this.skeleton_type}`)
}
// Initialize progress tracking
@@ -231,7 +234,7 @@ export class AnimationLoader extends EventTarget {
const cloned_animations = AnimationUtility.deep_clone_animation_clips(raw_animations)
// Clean track data (remove position tracks except for specific cases)
- AnimationUtility.clean_track_data(cloned_animations)
+ AnimationUtility.clean_track_data(cloned_animations, this.skeleton_type)
// Apply skeleton scaling to position keyframes
AnimationUtility.apply_skeleton_scale_to_position_keyframes(cloned_animations, skeleton_scale)
diff --git a/src/lib/processes/animations-listing/AnimationUtility.ts b/src/lib/processes/animations-listing/AnimationUtility.ts
index 62b9063..bc82dba 100644
--- a/src/lib/processes/animations-listing/AnimationUtility.ts
+++ b/src/lib/processes/animations-listing/AnimationUtility.ts
@@ -1,5 +1,7 @@
import { AnimationClip, Quaternion, Vector3, type KeyframeTrack, type QuaternionKeyframeTrack } from 'three'
import type { TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair'
+import { RigConfig } from '../../RigConfig'
+import { SkeletonType } from '../../enums/SkeletonType'
export class AnimationUtility {
// when we scaled the skeleton itself near the beginning, we kept track of that
@@ -34,7 +36,7 @@ export class AnimationUtility {
/// Removes position tracks from animation clips, keeping only rotation tracks.
/// @param animation_clips - The animation clips to modify.
/// @param preserve_root_position - Whether to keep the root position track.
- static clean_track_data (animation_clips: AnimationClip[]): void {
+ static clean_track_data (animation_clips: AnimationClip[], skeleton_type: SkeletonType): void {
animation_clips.forEach((animation_clip: AnimationClip) => {
// remove all position nodes except root
let rotation_tracks: KeyframeTrack[] = []
@@ -42,19 +44,19 @@ export class AnimationUtility {
// does the animation clip name include "RM". This indicates a root motion clip and we can keep the root position track
const preserve_root_position: boolean = animation_clip.name.toLowerCase().endsWith('rm')
+ // get position tracking bone name for normal movement (probably hips or head in the case of a snake)
+ const position_tracking_bone_name: string | undefined =
+ RigConfig.by_skeleton_type(skeleton_type)?.position_tracking_bone_name
+
if (preserve_root_position) {
rotation_tracks = animation_clip.tracks
.filter((x: KeyframeTrack) => x.name.includes('quaternion') ||
- x.name.toLowerCase().includes('hips.position') ||
- x.name.toLowerCase().includes('head.position') ||
- x.name.toLowerCase().includes('pelvis.position') ||
+ x.name.toLowerCase().includes(position_tracking_bone_name + '.position') ||
x.name.toLowerCase().includes('root.position'))
} else {
rotation_tracks = animation_clip.tracks
.filter((x: KeyframeTrack) => x.name.includes('quaternion') ||
- x.name.toLowerCase().includes('hips.position') ||
- x.name.toLowerCase().includes('head.position') ||
- x.name.toLowerCase().includes('pelvis.position'))
+ x.name.toLowerCase().includes(position_tracking_bone_name + '.position'))
}
animation_clip.tracks = rotation_tracks // update track data
diff --git a/src/lib/solvers/WeightCalculator.ts b/src/lib/solvers/WeightCalculator.ts
index 6f1fc27..9492c77 100644
--- a/src/lib/solvers/WeightCalculator.ts
+++ b/src/lib/solvers/WeightCalculator.ts
@@ -6,6 +6,7 @@ import {
import { Utility } from '../Utilities.js'
import { SkeletonType } from '../enums/SkeletonType.js'
+import { RigConfig } from '../RigConfig.js'
/**
* Handles the core bone-to-vertex weight calculation logic.
@@ -19,7 +20,7 @@ export class WeightCalculator {
private cached_median_child_bone_positions: Vector3[] = []
private readonly bone_object_to_index = new Map<Bone, number>()
- private distance_to_bottom_of_hip: number = 0
+ private distance_to_bottom_of_position_tracking_bone: number = 0
// each index will be a bone index. the value will be a list of vertex indices that belong to that bone
private readonly bones_vertex_segmentation: number[][] = []
@@ -37,7 +38,7 @@ export class WeightCalculator {
public initialize_caches (): void {
this.cached_median_child_bone_positions = this.bones.map(b => this.midpoint_to_child(b))
this.bones.forEach((b, idx) => this.bone_object_to_index.set(b, idx))
- this.distance_to_bottom_of_hip = this.calculate_distance_to_bottom_of_hip()
+ this.distance_to_bottom_of_position_tracking_bone = this.calculate_distance_to_bottom_of_position_tracking_bone()
}
public get_cached_median_child_bone_positions (): Vector3[] {
@@ -68,7 +69,7 @@ export class WeightCalculator {
(bone.name.includes('hips') || bone.name.includes('pelvis'))) {
// if the intersection point is lower than the vertex position, that means the vertex is below
// the hips area, and is part of the left or right leg...ignore that result
- if (this.distance_to_bottom_of_hip !== null && this.distance_to_bottom_of_hip < vertex_position.y) {
+ if (this.distance_to_bottom_of_position_tracking_bone !== null && this.distance_to_bottom_of_position_tracking_bone < vertex_position.y) {
return// this vertex is below our crotch area, so it cannot be part of our hips
}
}
@@ -102,28 +103,23 @@ export class WeightCalculator {
// every vertex checks to see if it is below the hips area,
// so do this calculation once and cache it for the lookup later
- private calculate_distance_to_bottom_of_hip (): number {
- let hip_bone_object: Bone | undefined = this.bones.find(b => {
- const name = b.name.toLowerCase()
- return name.includes('hips') || name.includes('pelvis')
- })
- if (hip_bone_object === undefined) {
+ private calculate_distance_to_bottom_of_position_tracking_bone (): number {
+ const position_tracking_bone_name: string = RigConfig.by_skeleton_type(this.skeleton_type as SkeletonType)?.position_tracking_bone_name || 'UNKNOWN POSITION BONE'
- // if there are no hips bones, we are going to assume it is a rig
- // that doesnt' have hips like a snake. Let's check the head
- // that will be used for the distance calculation instead and hope for the best
- hip_bone_object = this.bones.find(b => b.name.toLowerCase().includes('head'))
- if (hip_bone_object === undefined) {
- throw new Error('Hip bone (or head bone) not found')
- }
+ let position_tracking_bone_object: Bone | undefined = this.bones.find(b => {
+ const name = b.name.toLowerCase()
+ return name.includes(position_tracking_bone_name.toLowerCase())
+ })
+ if (position_tracking_bone_object === undefined) {
+ throw new Error('Position tracking bone not found')
}
-
- const intesection_point: Vector3 | null = this.cast_intersection_ray_down_from_bone(hip_bone_object)
+
+ const intesection_point: Vector3 | null = this.cast_intersection_ray_down_from_bone(position_tracking_bone_object)
// get the distance from the bone point to the intersection point
- const bone_index = this.bones.findIndex(b => b === hip_bone_object)
+ const bone_index = this.bones.findIndex(b => b === position_tracking_bone_object)
const bone_position: Vector3 = this.cached_median_child_bone_positions[bone_index]
let distance_to_bottom: number = intesection_point?.distanceTo(bone_position) ?? 0
diff --git a/src/preview-generator/preview-generator.ts b/src/preview-generator/preview-generator.ts
index da93b5a..fc5427a 100644
--- a/src/preview-generator/preview-generator.ts
+++ b/src/preview-generator/preview-generator.ts
@@ -10,6 +10,7 @@ import { Generators } from '../lib/Generators.ts'
import { ThemeManager } from '../lib/ThemeManager.ts'
import { AnimationUtility } from '../lib/processes/animations-listing/AnimationUtility.ts'
import { RigConfig } from '../lib/RigConfig.ts'
+import { SkeletonType } from '../lib/enums/SkeletonType.ts'
class PreviewGenerator {
private readonly renderer_: THREE.WebGLRenderer
@@ -126,7 +127,7 @@ class PreviewGenerator {
// TODO: with cleaning up data, the fox sitting has a weird height offset
// I would think the cleanup should work with all skeleton types
if (file_to_load.includes('human')) {
- AnimationUtility.clean_track_data(this.animation_clips)
+ AnimationUtility.clean_track_data(this.animation_clips, SkeletonType.Human )
}
const clip = this.animation_clips[0]
diff --git a/src/retarget/RetargetAnimationPreview.ts b/src/retarget/RetargetAnimationPreview.ts
index 668277b..08f4188 100644
--- a/src/retarget/RetargetAnimationPreview.ts
+++ b/src/retarget/RetargetAnimationPreview.ts
@@ -124,7 +124,9 @@ export class RetargetAnimationPreview extends EventTarget {
// 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])
+ AnimationUtility.clean_track_data([this.current_animation_clip],
+ AnimationRetargetService.getInstance().get_skeleton_type()
+ )
}
this.update_preview_animation()
resolve()