Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Started adding rig configuration

Scott Petrovicec25c1bcfe50
diff --git a/src/Mesh2MotionEngine.ts b/src/Mesh2MotionEngine.ts
index 17df4a8..9c61ccf 100644
--- a/src/Mesh2MotionEngine.ts
+++ b/src/Mesh2MotionEngine.ts
@@ -25,6 +25,7 @@ import { type Bone, Group, Scene, type Skeleton, type Vector3 } from 'three'
import type BoneTesterData from './lib/interfaces/BoneTesterData.ts'
import { SkeletonType } from './lib/enums/SkeletonType.ts'
+import { RigConfig } from './lib/RigConfig.ts'
import { CustomSkeletonHelper } from './lib/CustomSkeletonHelper.ts'
import { EventListeners } from './lib/EventListeners.ts'
@@ -271,7 +272,8 @@ export class Mesh2MotionEngine {
public update_a_pose_options_visibility (): void {
if (this.ui.dom_a_pose_correction_options != null) {
- if (this.load_skeleton_step.skeleton_type() === SkeletonType.Human) {
+ const config = RigConfig.by_skeleton_type(this.load_skeleton_step.skeleton_type())
+ if (config?.has_a_pose_correction === true) {
this.ui.dom_a_pose_correction_options.style.display = 'block'
} else {
this.ui.dom_a_pose_correction_options.style.display = 'none'
diff --git a/src/create.html b/src/create.html
index ee87f28..bd6a0b4 100644
--- a/src/create.html
+++ b/src/create.html
@@ -95,11 +95,7 @@
<div class="alternate-background-section">
<p style="margin: 0">Reference model</p>
<select id="model-selection">
- <option value="models/model-human.glb">Human</option>
- <option value="models/model-fox.glb">Fox</option>
- <option value="models/model-bird.glb">Bird</option>
- <option value="models/model-dragon.glb">Dragon</option>
- <option value="models/model-kaiju.glb">Kaiju</option>
+ <!-- populated dynamically from RigConfig -->
</select>
<button id="load-model-button">Load</button>
</div>
@@ -143,12 +139,7 @@
<div style="display: flex; flex-direction: column; gap: 1rem;">
<span>Skeleton template</span>
<select id="skeleton-selection">
- <option value="select-skeleton">Select a skeleton</option>
- <option value="human">Human</option>
- <option value="quadraped">4 Leg Creature</option>
- <option value="bird">Bird</option>
- <option value="dragon">Dragon</option>
- <option value="kaiju">Kaiju</option>
+ <!-- populated dynamically from RigConfig -->
</select>
</div>
diff --git a/src/index.html b/src/index.html
index 48c836d..0f0aceb 100644
--- a/src/index.html
+++ b/src/index.html
@@ -94,11 +94,7 @@
<div style="display: flex; flex-direction: column; gap: 1rem;">
<span>Skeleton template:</span>
<select id="skeleton-selection">
- <option value="select-skeleton">Select a skeleton</option>
- <option value="human">Human</option>
- <option value="quadraped">4 Leg Creature</option>
- <option value="bird">Bird</option>
- <option value="dragon">Dragon</option>
+ <!-- populated dynamically from RigConfig -->
</select>
</div>
diff --git a/src/lib/RigConfig.ts b/src/lib/RigConfig.ts
new file mode 100644
index 0000000..b245770
--- /dev/null
+++ b/src/lib/RigConfig.ts
@@ -0,0 +1,140 @@
+import { SkeletonType } from './enums/SkeletonType'
+
+export interface RigConfigEntry {
+ // String key used as the HTML select option value, e.g. 'human', 'quadraped'
+ key: string
+ // The SkeletonType enum member for this rig
+ skeleton_type: SkeletonType
+ // Model file path relative to the static root, e.g. 'models/model-human.glb'
+ model_file: string
+ // Display name shown in the model dropdown, e.g. 'Human', 'Fox'
+ model_display_name: string
+ // Display name shown in the skeleton-type dropdown, e.g. 'Human', '4 Leg Creature'
+ skeleton_display_name: string
+ // Animation filenames (no base path) loaded for this rig type
+ animation_files: string[]
+ // Sub-folder name used when referencing animation preview thumbnails
+ animation_preview_folder: string
+ // Only Human has per-finger hand skeleton options
+ has_hand_options: boolean
+ // Only Human shows the head weight correction panel
+ has_head_weight_correction: boolean
+ // Only Human shows the A-pose arm-extension correction slider
+ has_a_pose_correction: boolean
+}
+
+/**
+ * Single source of truth for every supported rig type.
+ * To add a new rig, append one entry to `RigConfig.all` and add the
+ * corresponding GLB/rig files — no other TypeScript changes are required.
+ */
+// eslint-disable-next-line @typescript-eslint/no-extraneous-class
+export class RigConfig {
+ static readonly all: RigConfigEntry[] = [
+ {
+ key: 'human',
+ skeleton_type: SkeletonType.Human,
+ model_file: 'models/model-human.glb',
+ model_display_name: 'Human',
+ skeleton_display_name: 'Human',
+ animation_files: ['human-base-animations.glb', 'human-addon-animations.glb'],
+ animation_preview_folder: 'human',
+ has_hand_options: true,
+ has_head_weight_correction: true,
+ has_a_pose_correction: true
+ },
+ {
+ key: 'quadraped',
+ skeleton_type: SkeletonType.Quadraped,
+ model_file: 'models/model-fox.glb',
+ model_display_name: 'Fox',
+ skeleton_display_name: '4 Leg Creature',
+ animation_files: ['quad-creature-animations.glb'],
+ animation_preview_folder: 'four-legged',
+ has_hand_options: false,
+ has_head_weight_correction: false,
+ has_a_pose_correction: false
+ },
+ {
+ key: 'bird',
+ skeleton_type: SkeletonType.Bird,
+ model_file: 'models/model-bird.glb',
+ model_display_name: 'Bird',
+ skeleton_display_name: 'Bird',
+ animation_files: ['bird-animations.glb'],
+ animation_preview_folder: 'bird',
+ has_hand_options: false,
+ has_head_weight_correction: false,
+ has_a_pose_correction: false
+ },
+ {
+ key: 'dragon',
+ skeleton_type: SkeletonType.Dragon,
+ model_file: 'models/model-dragon.glb',
+ model_display_name: 'Dragon',
+ skeleton_display_name: 'Dragon',
+ animation_files: ['dragon-animations.glb'],
+ animation_preview_folder: 'dragon',
+ has_hand_options: false,
+ has_head_weight_correction: false,
+ has_a_pose_correction: false
+ },
+ {
+ key: 'kaiju',
+ skeleton_type: SkeletonType.Kaiju,
+ model_file: 'models/model-kaiju.glb',
+ model_display_name: 'Kaiju',
+ skeleton_display_name: 'Kaiju',
+ animation_files: ['kaiju-animations.glb'],
+ animation_preview_folder: 'kaiju',
+ has_hand_options: false,
+ has_head_weight_correction: false,
+ has_a_pose_correction: false
+ }
+ ]
+
+ /** Look up a rig by its HTML select option value. */
+ static by_key (key: string): RigConfigEntry | undefined {
+ return this.all.find(r => r.key === key)
+ }
+
+ /** Look up a rig by its SkeletonType enum value. */
+ static by_skeleton_type (skeleton_type: SkeletonType): RigConfigEntry | undefined {
+ return this.all.find(r => r.skeleton_type === skeleton_type)
+ }
+
+ /**
+ * Populate a <select> with one <option> per rig using model display names.
+ * Existing options are replaced.
+ */
+ static populate_model_select (select: HTMLSelectElement): void {
+ select.innerHTML = ''
+ for (const rig of this.all) {
+ const option = document.createElement('option')
+ option.value = rig.model_file
+ option.textContent = rig.model_display_name
+ select.appendChild(option)
+ }
+ }
+
+ /**
+ * Populate a <select> with one <option> per rig using skeleton display names.
+ * Pass `include_placeholder = false` to omit the "Select a skeleton" entry.
+ * Existing options are replaced.
+ */
+ static populate_skeleton_select (select: HTMLSelectElement, include_placeholder = true): void {
+ select.innerHTML = ''
+ if (include_placeholder) {
+ const placeholder = document.createElement('option')
+ placeholder.value = 'select-skeleton'
+ placeholder.textContent = 'Select a skeleton'
+ select.appendChild(placeholder)
+ }
+ for (const rig of this.all) {
+ const option = document.createElement('option')
+ option.value = rig.key
+ option.textContent = rig.skeleton_display_name
+ select.appendChild(option)
+ }
+ }
+}
diff --git a/src/lib/processes/animations-listing/AnimationLoader.ts b/src/lib/processes/animations-listing/AnimationLoader.ts
index 4a199f1..6cf7236 100644
--- a/src/lib/processes/animations-listing/AnimationLoader.ts
+++ b/src/lib/processes/animations-listing/AnimationLoader.ts
@@ -2,6 +2,7 @@ import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { type AnimationClip } from 'three'
import { AnimationUtility } from './AnimationUtility.ts'
import { SkeletonType } from '../../enums/SkeletonType.ts'
+import { RigConfig } from '../../RigConfig.ts'
import { type AnimationClipMetadata, type TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair.ts'
import { LoadError, NoAnimationsError } from './AnimationImportErrors.ts'
@@ -222,25 +223,12 @@ export class AnimationLoader extends EventTarget {
*/
private get_animation_file_paths (skeleton_type: SkeletonType): string[] {
const base_path = this.animations_file_path
-
- switch (skeleton_type) {
- case SkeletonType.Human:
- return [
- `${base_path}human-base-animations.glb`,
- `${base_path}human-addon-animations.glb`
- ]
- case SkeletonType.Quadraped:
- return [`${base_path}quad-creature-animations.glb`]
- case SkeletonType.Bird:
- return [`${base_path}bird-animations.glb`]
- case SkeletonType.Dragon:
- return [`${base_path}dragon-animations.glb`]
- case SkeletonType.Kaiju:
- return [`${base_path}kaiju-animations.glb`]
- default:
- console.error('Unknown skeleton type for loading animations:', skeleton_type)
- return []
+ const config = RigConfig.by_skeleton_type(skeleton_type)
+ if (config === undefined) {
+ console.error('Unknown skeleton type for loading animations:', skeleton_type)
+ return []
}
+ return config.animation_files.map(f => `${base_path}${f}`)
}
/**
diff --git a/src/lib/processes/animations-listing/AnimationSearch.ts b/src/lib/processes/animations-listing/AnimationSearch.ts
index 6d7635c..28b0458 100644
--- a/src/lib/processes/animations-listing/AnimationSearch.ts
+++ b/src/lib/processes/animations-listing/AnimationSearch.ts
@@ -1,5 +1,6 @@
import { type ThemeManager } from '../../ThemeManager'
import { SkeletonType } from '../../enums/SkeletonType'
+import { RigConfig } from '../../RigConfig'
import { type AnimationWithState } from './interfaces/AnimationWithState'
import { type TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair'
@@ -154,27 +155,9 @@ export class AnimationSearch extends EventTarget {
// build out where the video previews will be stored
// each skeleton type has its own folder
- let preview_folder: string = ''
- switch (this.skeleton_type) {
- case SkeletonType.Human:
- preview_folder = 'human'
- break
- case SkeletonType.Quadraped:
- preview_folder = 'four-legged'
- break
- case SkeletonType.Bird:
- preview_folder = 'bird'
- break
- case SkeletonType.Dragon:
- preview_folder = 'dragon'
- break
- case SkeletonType.Kaiju:
- preview_folder = 'kaiju'
- break
- default:
- preview_folder = 'error'
- console.error('Unknown skeleton type for animation previews. Update AnimationSearch.ts switch statement.')
- break
+ const preview_folder: string = RigConfig.by_skeleton_type(this.skeleton_type)?.animation_preview_folder ?? 'error'
+ if (preview_folder === 'error') {
+ console.error('Unknown skeleton type for animation previews. Add the rig to RigConfig.ts.')
}
const anim_name: string = animation_clip.name
diff --git a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
index 0094049..f0b1b6e 100644
--- a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
+++ b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
@@ -19,6 +19,7 @@ import {
type Camera
} from 'three'
import { SkeletonType } from '../../enums/SkeletonType.ts'
+import { RigConfig } from '../../RigConfig.ts'
/*
* StepEditSkeleton
@@ -103,7 +104,8 @@ export class StepEditSkeleton extends EventTarget {
// keep track of skeleton type to show/hide certain UI elements
// only human skeletons have the head weight correction option
if (this.ui.dom_use_head_weight_correction_container != null) {
- if (skeleton_type === SkeletonType.Human) {
+ const config = RigConfig.by_skeleton_type(skeleton_type)
+ if (config?.has_head_weight_correction === true) {
this.ui.dom_use_head_weight_correction_container.style.display = 'block'
} else {
this.ui.dom_use_head_weight_correction_container.style.display = 'none'
diff --git a/src/lib/processes/load-model/StepLoadModel.ts b/src/lib/processes/load-model/StepLoadModel.ts
index 827e8bc..6cdc01a 100644
--- a/src/lib/processes/load-model/StepLoadModel.ts
+++ b/src/lib/processes/load-model/StepLoadModel.ts
@@ -2,6 +2,7 @@ import { UI } from '../../UI.ts'
import { ModelZipLoader } from './ModelZipLoader.ts'
import { CustomFBXLoader, type FBXResults } from './CustomFBXLoader.ts'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
+import { RigConfig } from '../../RigConfig.ts'
import { Scene } from 'three/src/scenes/Scene.js'
import { Mesh } from 'three/src/objects/Mesh.js'
@@ -158,6 +159,12 @@ export class StepLoadModel extends EventTarget {
}
public add_event_listeners (): void {
+ // Populate the model reference dropdown from the central rig config
+ const model_select = document.querySelector<HTMLSelectElement>('#model-selection')
+ if (model_select !== null) {
+ RigConfig.populate_model_select(model_select)
+ }
+
if (this.ui.dom_upload_model_button !== null) {
// handle file upload
this.ui.dom_upload_model_button.addEventListener('change', (event: Event) => {
diff --git a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
index 23d6408..a881992 100644
--- a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
+++ b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
@@ -2,6 +2,7 @@ import { UI } from '../../UI.ts'
import { Object3D, type Scene, type Object3DEventMap } from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { SkeletonType, type HandSkeletonType } from '../../enums/SkeletonType.js'
+import { RigConfig } from '../../RigConfig.ts'
import type GLTFResult from './interfaces/GLTFResult.ts'
import { add_origin_markers, remove_origin_markers } from './OriginMarkerManager'
import { add_preview_skeleton, remove_preview_skeleton } from './PreviewSkeletonManager.ts'
@@ -106,25 +107,14 @@ export class StepLoadSkeleton extends EventTarget {
const skeleton_selection = this.ui.dom_skeleton_drop_type.options
const skeleton_file: string = skeleton_selection[skeleton_selection.selectedIndex].value
- // set the skeleton type. This will be used for the animations listing later
- // so it knows what animations to load
- switch (skeleton_file) {
- case 'quadraped':
- return SkeletonType.Quadraped
- case 'human':
- return SkeletonType.Human
- case 'bird':
- return SkeletonType.Bird
- case 'select-skeleton':
- return SkeletonType.None
- case 'dragon':
- return SkeletonType.Dragon
- case 'kaiju':
- return SkeletonType.Kaiju
- default:
- console.error('unknown skeleton type selected: ', skeleton_file)
- return SkeletonType.Error
+ if (skeleton_file === 'select-skeleton') return SkeletonType.None
+
+ const config = RigConfig.by_key(skeleton_file)
+ if (config === undefined) {
+ console.error('unknown skeleton type selected: ', skeleton_file)
+ return SkeletonType.Error
}
+ return config.skeleton_type
}
private hand_skeleton_type (): HandSkeletonType {
@@ -133,6 +123,11 @@ export class StepLoadSkeleton extends EventTarget {
}
private add_event_listeners (): void {
+ // Populate the skeleton template dropdown from the central rig config
+ if (this.ui.dom_skeleton_drop_type !== null) {
+ RigConfig.populate_skeleton_select(this.ui.dom_skeleton_drop_type)
+ }
+
// Add event listener for skeleton type changes to show/hide hand options
if (this.ui.dom_skeleton_drop_type !== null) {
this.ui.dom_skeleton_drop_type.addEventListener('change', () => {
@@ -292,7 +287,8 @@ export class StepLoadSkeleton extends EventTarget {
return
}
- if (this.skeleton_file_path() === SkeletonType.Human) {
+ const config = RigConfig.by_skeleton_type(this.skeleton_file_path())
+ if (config?.has_hand_options === true) {
this.ui.dom_hand_skeleton_options.style.display = 'flex'
} else {
this.ui.dom_hand_skeleton_options.style.display = 'none'
diff --git a/src/retarget/RetargetUtils.ts b/src/retarget/RetargetUtils.ts
index 034ad54..8009331 100644
--- a/src/retarget/RetargetUtils.ts
+++ b/src/retarget/RetargetUtils.ts
@@ -1,6 +1,7 @@
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'
+import { RigConfig } from '../lib/RigConfig.ts'
export interface TrackNameParts {
bone_name: string
@@ -118,18 +119,9 @@ export class RetargetUtils {
* Get the animation file path based on skeleton type
*/
static get_animation_file_path (skeleton_type: SkeletonType): string | null {
- switch (skeleton_type) {
- case SkeletonType.Human:
- return '/animations/human-base-animations.glb'
- case SkeletonType.Quadraped:
- return '/animations/quad-creature-animations.glb'
- case SkeletonType.Bird:
- return '/animations/bird-animations.glb'
- case SkeletonType.Dragon:
- return '/animations/dragon-animations.glb'
- default:
- return null
- }
+ const config = RigConfig.by_skeleton_type(skeleton_type)
+ if (config === undefined || config.animation_files.length === 0) return null
+ return `/animations/${config.animation_files[0]}`
}
/**
diff --git a/src/retarget/index.html b/src/retarget/index.html
index e8e9866..56d2301 100644
--- a/src/retarget/index.html
+++ b/src/retarget/index.html
@@ -82,11 +82,7 @@
<div class="bone-map-content">
<div id="skeleton-selection-container" style="margin-bottom: 1.0rem;">
<select id="skeleton-type-select" class="button">
- <option value="human" selected>Human</option>
- <option value="quadraped">Quadruped (Fox)</option>
- <option value="bird">Bird</option>
- <option value="dragon">Dragon</option>
- <option value="kaiju">Kaiju</option>
+ <!-- populated dynamically from RigConfig -->
</select>
</div>
diff --git a/src/retarget/steps/StepLoadSourceSkeleton.ts b/src/retarget/steps/StepLoadSourceSkeleton.ts
index f7b3169..fe50a9b 100644
--- a/src/retarget/steps/StepLoadSourceSkeleton.ts
+++ b/src/retarget/steps/StepLoadSourceSkeleton.ts
@@ -1,6 +1,7 @@
import { Group, Object3D, Scene, SkeletonHelper } from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { SkeletonType } from '../../lib/enums/SkeletonType.ts'
+import { RigConfig } from '../../lib/RigConfig.ts'
import type GLTFResult from '../../lib/processes/load-skeleton/interfaces/GLTFResult.ts'
import { ModalDialog } from '../../lib/ModalDialog.ts'
@@ -25,6 +26,11 @@ export class StepLoadSourceSkeleton extends EventTarget {
// Get DOM references
this.skeleton_type_select = document.getElementById('skeleton-type-select') as HTMLSelectElement
+ // Populate the skeleton dropdown from the central rig config (no placeholder needed)
+ if (this.skeleton_type_select !== null) {
+ RigConfig.populate_skeleton_select(this.skeleton_type_select, false)
+ }
+
if (!this._added_event_listeners) {
this.add_event_listeners()
this._added_event_listeners = true
@@ -74,20 +80,7 @@ export class StepLoadSourceSkeleton extends EventTarget {
}
private get_skeleton_type_enum (selection: string): SkeletonType {
- switch (selection) {
- case 'human':
- return SkeletonType.Human
- case 'quadraped':
- return SkeletonType.Quadraped
- case 'bird':
- return SkeletonType.Bird
- case 'dragon':
- return SkeletonType.Dragon
- case 'kaiju':
- return SkeletonType.Kaiju
- default:
- return SkeletonType.None
- }
+ return RigConfig.by_key(selection)?.skeleton_type ?? SkeletonType.None
}
private async load_skeleton_from_path (file_path: string): Promise<void> {