Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Put the rig file location in the configuration instead of the enum

Scott Petrovice07efab76add
diff --git a/src/lib/RigConfig.ts b/src/lib/RigConfig.ts
index 5dc4570..e6df223 100644
--- a/src/lib/RigConfig.ts
+++ b/src/lib/RigConfig.ts
@@ -7,6 +7,8 @@ export interface RigConfigEntry {
skeleton_type: SkeletonType
// Model file path relative to the static root, e.g. 'models/model-human.glb'
model_file: string
+ // Rig/skeleton GLB file path relative to the static root, e.g. 'rigs/rig-human.glb'
+ rig_file: string
// Display name shown in both the model and skeleton dropdowns
rig_display_name: string
// Animation filenames (no base path) loaded for this rig type
@@ -33,6 +35,7 @@ export class RigConfig {
key: 'human',
skeleton_type: SkeletonType.Human,
model_file: 'models/model-human.glb',
+ rig_file: 'rigs/rig-human.glb',
rig_display_name: 'Human',
animation_files: ['human-base-animations.glb', 'human-addon-animations.glb'],
animation_preview_folder: 'human',
@@ -44,6 +47,7 @@ export class RigConfig {
key: 'fox',
skeleton_type: SkeletonType.Fox,
model_file: 'models/model-fox.glb',
+ rig_file: 'rigs/rig-fox.glb',
rig_display_name: 'Fox',
animation_files: ['fox-animations.glb'],
animation_preview_folder: 'fox',
@@ -55,6 +59,7 @@ export class RigConfig {
key: 'bird',
skeleton_type: SkeletonType.Bird,
model_file: 'models/model-bird.glb',
+ rig_file: 'rigs/rig-bird.glb',
rig_display_name: 'Bird',
animation_files: ['bird-animations.glb'],
animation_preview_folder: 'bird',
@@ -66,6 +71,7 @@ export class RigConfig {
key: 'dragon',
skeleton_type: SkeletonType.Dragon,
model_file: 'models/model-dragon.glb',
+ rig_file: 'rigs/rig-dragon.glb',
rig_display_name: 'Dragon',
animation_files: ['dragon-animations.glb'],
animation_preview_folder: 'dragon',
@@ -77,6 +83,7 @@ export class RigConfig {
key: 'kaiju',
skeleton_type: SkeletonType.Kaiju,
model_file: 'models/model-kaiju.glb',
+ rig_file: 'rigs/rig-kaiju.glb',
rig_display_name: 'Kaiju',
animation_files: ['kaiju-animations.glb'],
animation_preview_folder: 'kaiju',
@@ -96,6 +103,11 @@ export class RigConfig {
return this.all.find(r => r.skeleton_type === skeleton_type)
}
+ /** Get the rig GLB file path for a given skeleton type. Returns undefined for Error/None. */
+ static rig_file_for (skeleton_type: SkeletonType): string | undefined {
+ return this.by_skeleton_type(skeleton_type)?.rig_file
+ }
+
/**
* Populate a <select> with one <option> per rig using model display names.
* Existing options are replaced.
diff --git a/src/lib/enums/SkeletonType.ts b/src/lib/enums/SkeletonType.ts
index ddbf314..4a690a3 100644
--- a/src/lib/enums/SkeletonType.ts
+++ b/src/lib/enums/SkeletonType.ts
@@ -1,13 +1,12 @@
// Supported skeleton types
-// the file names for the skeleton types are associated with the const
export enum SkeletonType {
- Fox = 'rigs/rig-fox.glb',
- Human = 'rigs/rig-human.glb',
- Bird = 'rigs/rig-bird.glb',
- Dragon = 'rigs/rig-dragon.glb',
- Kaiju = 'rigs/rig-kaiju.glb',
- Error = 'ERROR',
- None = 'NONE'
+ Fox = 'fox',
+ Human = 'human',
+ Bird = 'bird',
+ Dragon = 'dragon',
+ Kaiju = 'kaiju',
+ Error = 'error',
+ None = 'none'
}
// Hand skeleton variation types for human skeleton
diff --git a/src/lib/processes/load-skeleton/PreviewSkeletonManager.ts b/src/lib/processes/load-skeleton/PreviewSkeletonManager.ts
index 5aa477a..27098c2 100644
--- a/src/lib/processes/load-skeleton/PreviewSkeletonManager.ts
+++ b/src/lib/processes/load-skeleton/PreviewSkeletonManager.ts
@@ -3,6 +3,7 @@ import { type GLTF, GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
import type GLTFResult from './interfaces/GLTFResult'
import { type HandSkeletonType, SkeletonType } from '../../enums/SkeletonType'
import { HandHelper } from './HandHelper'
+import { RigConfig } from '../../RigConfig'
const skeleton_group_name: string = 'preview_skeleton_group'
@@ -11,7 +12,7 @@ const skeleton_group_name: string = 'preview_skeleton_group'
// and then add the new one based on the selected type
export async function add_preview_skeleton (
root: Scene,
- skeleton_file_path: SkeletonType,
+ skeleton_type: SkeletonType,
hand_skeleton_type: HandSkeletonType,
skeleton_scale: number = 1.0
): Promise<Object3D<Object3DEventMap>> {
@@ -20,9 +21,9 @@ export async function add_preview_skeleton (
// Check if preview skeleton group exists and matches requested skeleton
if (preview_skeleton_group !== undefined) {
// Read previous skeleton info from userData
- const previous_file_path = preview_skeleton_group.userData.skeleton_file_path
+ const previous_file_path = preview_skeleton_group.userData.skeleton_type
const previous_hand_type = preview_skeleton_group.userData.hand_skeleton_type
- if (previous_file_path === skeleton_file_path && previous_hand_type === hand_skeleton_type) {
+ if (previous_file_path === skeleton_type && previous_hand_type === hand_skeleton_type) {
// Only update scale
preview_skeleton_group.scale.set(skeleton_scale, skeleton_scale, skeleton_scale)
// Return the first child (should be loaded_scene)
@@ -37,13 +38,19 @@ export async function add_preview_skeleton (
preview_skeleton_group = new Group()
preview_skeleton_group.name = skeleton_group_name
// Store current skeleton info for future comparison
- preview_skeleton_group.userData.skeleton_file_path = skeleton_file_path
+ preview_skeleton_group.userData.skeleton_type = skeleton_type
preview_skeleton_group.userData.hand_skeleton_type = hand_skeleton_type
root.add(preview_skeleton_group)
+ // Resolve the rig file path from the central config
+ const rig_file = RigConfig.rig_file_for(skeleton_type)
+ if (rig_file === undefined) {
+ throw new Error(`No rig file found for skeleton type: ${skeleton_type}`)
+ }
+
// Load and customize skeleton
- const loaded_scene: Object3D<Object3DEventMap> = await load_skeleton(skeleton_file_path)
- if (skeleton_file_path === SkeletonType.Human) {
+ const loaded_scene: Object3D<Object3DEventMap> = await load_skeleton(rig_file)
+ if (skeleton_type === SkeletonType.Human) {
const helper = new HandHelper()
helper.modify_hand_skeleton(loaded_scene, hand_skeleton_type)
}
diff --git a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
index a881992..41d07fa 100644
--- a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
+++ b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
@@ -32,7 +32,7 @@ export class StepLoadSkeleton extends EventTarget {
return this.manual_set_skeleton_type
}
- return this.skeleton_file_path() // this is actually the type/filepath combo
+ return this.skeleton_file_path()
}
public set_skeleton_type (type: SkeletonType): void {
@@ -166,7 +166,10 @@ export class StepLoadSkeleton extends EventTarget {
}
// add back loading information here
- this.load_skeleton_file(this.skeleton_file_path())
+ const rig_file = RigConfig.rig_file_for(this.skeleton_file_path())
+ if (rig_file !== undefined) {
+ this.load_skeleton_file(rig_file)
+ }
})
}// end if statement
diff --git a/src/lib/solvers/BoneWeightsByMedianDistance.ts b/src/lib/solvers/BoneWeightsByMedianDistance.ts
index a5e741f..60457e0 100644
--- a/src/lib/solvers/BoneWeightsByMedianDistance.ts
+++ b/src/lib/solvers/BoneWeightsByMedianDistance.ts
@@ -18,7 +18,7 @@ export default class BoneWeightsByMedianDistance implements IAutoSkinSolver {
private bone_idx_test: number = -1
private readonly bones_master_data: BoneCalculationData[] = []
private debugging_scene_object: Object3D = new Object3D()
- private skeleton_type: SkeletonType = SkeletonType.BipedalSimple
+ private skeleton_type: SkeletonType = SkeletonType.Human
constructor (bone_hier: Object3D, skeleton_type: SkeletonType) {
this.set_skeleton_type(skeleton_type)
diff --git a/src/marketing/MarketingBootstrap.ts b/src/marketing/MarketingBootstrap.ts
index d9d6eea..cacb92d 100644
--- a/src/marketing/MarketingBootstrap.ts
+++ b/src/marketing/MarketingBootstrap.ts
@@ -2,6 +2,7 @@ import { Vector3 } from 'three'
import { ProcessStep } from '../lib/enums/ProcessStep'
import { SkeletonType } from '../lib/enums/SkeletonType'
import { Mesh2MotionEngine } from '../Mesh2MotionEngine'
+import { RigConfig } from '../lib/RigConfig'
export class MarketingBootstrap {
private mesh2motion_engine: Mesh2MotionEngine
@@ -81,9 +82,12 @@ export class MarketingBootstrap {
// we are re-creating the engine, so need to manually add the event listeners again
this.mesh2motion_engine.load_model_step.addEventListener('modelLoaded', () => {
- // this (this.skeleton_type) value contains the filename for the skeleton rig
+ // resolve the rig file path from the central config
+ const rig_file = RigConfig.rig_file_for(this.skeleton_type)
this.mesh2motion_engine.process_step_changed(ProcessStep.LoadSkeleton)
- this.mesh2motion_engine.load_skeleton_step.load_skeleton_file('../' + this.skeleton_type)
+ if (rig_file !== undefined) {
+ this.mesh2motion_engine.load_skeleton_step.load_skeleton_file('../' + rig_file)
+ }
this.mesh2motion_engine.load_skeleton_step.set_skeleton_type(this.skeleton_type)
})
diff --git a/src/retarget/steps/StepLoadSourceSkeleton.ts b/src/retarget/steps/StepLoadSourceSkeleton.ts
index fe50a9b..8d6af81 100644
--- a/src/retarget/steps/StepLoadSourceSkeleton.ts
+++ b/src/retarget/steps/StepLoadSourceSkeleton.ts
@@ -43,8 +43,8 @@ export class StepLoadSourceSkeleton extends EventTarget {
private load_default_skeleton (): void {
// Set the skeleton type to human and load it automatically
this.skeleton_type = SkeletonType.Human
-
- this.load_skeleton_from_path(`/${SkeletonType.Human}`).catch((error) => {
+ const rig_file = RigConfig.rig_file_for(SkeletonType.Human) ?? ''
+ this.load_skeleton_from_path(`/${rig_file}`).catch((error) => {
console.error('Failed to load default human skeleton:', error)
})
@@ -70,8 +70,9 @@ export class StepLoadSourceSkeleton extends EventTarget {
// Clear any previously loaded skeleton
this.clear_previous_skeleton()
- // Load the selected skeleton using the file path from the enum
- this.load_skeleton_from_path(`/${this.skeleton_type}`).catch((error) => {
+ // Load the selected skeleton using the rig file path from RigConfig
+ const rig_file = RigConfig.rig_file_for(this.skeleton_type) ?? ''
+ this.load_skeleton_from_path(`/${rig_file}`).catch((error) => {
console.error('Failed to load skeleton:', error)
})