Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

move skeleton dialog into its own class

Scott Petrovic2ef0075eef48
diff --git a/src/retarget/TargetBoneTreeDialog.ts b/src/retarget/TargetBoneTreeDialog.ts
new file mode 100644
index 0000000..8342e50
--- /dev/null
+++ b/src/retarget/TargetBoneTreeDialog.ts
@@ -0,0 +1,86 @@
+import { type Bone } from 'three'
+import { ModalDialog } from '../lib/ModalDialog.ts'
+
+export class TargetBoneTreeDialog {
+ private readonly get_target_bones: () => Map<string, Bone>
+ private view_bone_tree_button: HTMLSpanElement | null = null
+ private has_added_event_listeners: boolean = false
+
+ constructor (get_target_bones: () => Map<string, Bone>) {
+ this.get_target_bones = get_target_bones
+ }
+
+ public begin (): void {
+ this.view_bone_tree_button = document.getElementById('view-bone-tree-button') as HTMLSpanElement
+
+ if (!this.has_added_event_listeners) {
+ this.view_bone_tree_button?.addEventListener('click', () => {
+ this.show()
+ })
+
+ this.has_added_event_listeners = true
+ }
+ }
+
+ public show (): void {
+ const target_bones = this.get_target_bones()
+ const root_target_bones = TargetBoneTreeDialog.get_root_bones(target_bones)
+
+ if (target_bones.size === 0) {
+ new ModalDialog('Target Skeleton Hierarchy', '<p>No target skeleton loaded.</p>', { customClass: 'bone-tree-modal' }).show()
+ return
+ }
+
+ const tree_html = root_target_bones
+ .map((root_bone) => TargetBoneTreeDialog.create_bone_tree_html(root_bone, target_bones))
+ .join('')
+
+ const content_html = `
+ <div class="bone-tree-dialog-summary">
+ <span>Total bones: ${target_bones.size}</span>
+ <span>Root chains: ${root_target_bones.length}</span>
+ </div>
+ <ul class="bone-tree-dialog-list">
+ ${tree_html}
+ </ul>
+ `
+
+ new ModalDialog('Target Skeleton Hierarchy', content_html, { customClass: 'bone-tree-modal' }).show()
+ }
+
+ private static get_root_bones (target_bones: Map<string, Bone>): Bone[] {
+ const roots: Bone[] = []
+
+ target_bones.forEach((bone) => {
+ const parent = bone.parent
+ const has_bone_parent_in_target = parent !== null && parent.type === 'Bone' && target_bones.has(parent.uuid)
+
+ if (!has_bone_parent_in_target) {
+ roots.push(bone)
+ }
+ })
+
+ return roots.sort((a, b) => a.name.localeCompare(b.name))
+ }
+
+ private static create_bone_tree_html (bone: Bone, target_bones: Map<string, Bone>): string {
+ const child_bones = bone.children
+ .filter((child): child is Bone => child.type === 'Bone' && target_bones.has(child.uuid))
+ .sort((a, b) => a.name.localeCompare(b.name))
+
+ const children_html = child_bones.length > 0
+ ? `<ul>${child_bones.map((child_bone) => this.create_bone_tree_html(child_bone, target_bones)).join('')}</ul>`
+ : ''
+
+ return `<li><span class="bone-tree-node-name">${this.escape_html(bone.name)}</span>${children_html}</li>`
+ }
+
+ private static escape_html (value: string): string {
+ return value
+ .replace(/&/g, '&amp;')
+ .replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;')
+ .replace(/"/g, '&quot;')
+ .replace(/'/g, '&#39;')
+ }
+}
diff --git a/src/retarget/steps/StepBoneMapping.ts b/src/retarget/steps/StepBoneMapping.ts
index 093812e..b1bb527 100644
--- a/src/retarget/steps/StepBoneMapping.ts
+++ b/src/retarget/steps/StepBoneMapping.ts
@@ -1,9 +1,8 @@
-import { type Bone, type Scene, type SkinnedMesh } from 'three'
+import { type Scene, type SkinnedMesh } from 'three'
import { BoneAutoMapper } from '../bone-automap/BoneAutoMapper.ts'
import { MixamoMapper } from '../bone-automap/MixamoMapper.ts'
import { AnimationRetargetService } from '../AnimationRetargetService.ts'
import { Mesh2MotionMapper } from '../bone-automap/Mesh2MotionMapper.ts'
-import { ModalDialog } from '../../lib/ModalDialog.ts'
// when we are auto-mapping, keep track of what rig type we matched target against
export enum TargetBoneMappingType {
@@ -68,10 +67,6 @@ export class StepBoneMapping extends EventTarget {
this.auto_map_bones()
})
- this.view_bone_tree_button?.addEventListener('click', () => {
- this.show_target_bone_tree_dialog()
- })
-
this.has_added_event_listeners = true
}
}
@@ -250,80 +245,6 @@ export class StepBoneMapping extends EventTarget {
})
}
- private get_unique_target_bones (): Map<string, Bone> {
- const unique_bones: Map<string, Bone> = new Map<string, Bone>()
-
- AnimationRetargetService.getInstance().get_target_skinned_meshes().forEach((skinned_mesh) => {
- skinned_mesh.skeleton.bones.forEach((bone) => {
- unique_bones.set(bone.uuid, bone)
- })
- })
-
- return unique_bones
- }
-
- private get_target_root_bones (unique_target_bones: Map<string, Bone>): Bone[] {
- const roots: Bone[] = []
-
- unique_target_bones.forEach((bone) => {
- const parent = bone.parent
- const has_bone_parent_in_target = parent !== null && parent.type === 'Bone' && unique_target_bones.has(parent.uuid)
-
- if (!has_bone_parent_in_target) {
- roots.push(bone)
- }
- })
-
- return roots.sort((a, b) => a.name.localeCompare(b.name))
- }
-
- private show_target_bone_tree_dialog (): void {
- const unique_target_bones = this.get_unique_target_bones()
- const root_target_bones = this.get_target_root_bones(unique_target_bones)
-
- if (unique_target_bones.size === 0) {
- new ModalDialog('Target Skeleton Hierarchy', '<p>No target skeleton loaded.</p>', { customClass: 'bone-tree-modal' }).show()
- return
- }
-
- const tree_html = root_target_bones
- .map((root_bone) => this.create_target_bone_tree_html(root_bone, unique_target_bones))
- .join('')
-
- const content_html = `
- <div class="bone-tree-dialog-summary">
- <span>Total bones: ${unique_target_bones.size}</span>
- <span>Root chains: ${root_target_bones.length}</span>
- </div>
- <ul class="bone-tree-dialog-list">
- ${tree_html}
- </ul>
- `
-
- new ModalDialog('Target Skeleton Hierarchy', content_html, { customClass: 'bone-tree-modal' }).show()
- }
-
- private create_target_bone_tree_html (bone: Bone, unique_target_bones: Map<string, Bone>): string {
- const child_bones = bone.children
- .filter((child): child is Bone => child.type === 'Bone' && unique_target_bones.has(child.uuid))
- .sort((a, b) => a.name.localeCompare(b.name))
-
- const children_html = child_bones.length > 0
- ? `<ul>${child_bones.map((child_bone) => this.create_target_bone_tree_html(child_bone, unique_target_bones)).join('')}</ul>`
- : ''
-
- return `<li><span class="bone-tree-node-name">${this.escape_html(bone.name)}</span>${children_html}</li>`
- }
-
- private escape_html (value: string): string {
- return value
- .replace(/&/g, '&amp;')
- .replace(/</g, '&lt;')
- .replace(/>/g, '&gt;')
- .replace(/"/g, '&quot;')
- .replace(/'/g, '&#39;')
- }
-
// Drag and drop event handlers
private handle_drag_start (event: DragEvent): void {
const target = event.currentTarget as HTMLElement
diff --git a/src/retarget/steps/StepLoadTargetModel.ts b/src/retarget/steps/StepLoadTargetModel.ts
index 0e09061..0630de2 100644
--- a/src/retarget/steps/StepLoadTargetModel.ts
+++ b/src/retarget/steps/StepLoadTargetModel.ts
@@ -1,13 +1,15 @@
-import { type Bone, Box3, type Group, type Object3DEventMap, type Scene, type SkinnedMesh, Vector3 } from 'three'
-import { type Mesh2MotionEngine } from '../Mesh2MotionEngine.ts'
+import { type Bone, Box3, type Scene, type SkinnedMesh, Vector3 } from 'three'
+import { type Mesh2MotionEngine } from '../../Mesh2MotionEngine.ts'
import { ModalDialog } from '../../lib/ModalDialog.ts'
import { RetargetUtils } from '../RetargetUtils.ts'
+import { TargetBoneTreeDialog } from '../TargetBoneTreeDialog.ts'
/**
* Handles loading the target model (user-uploaded model) for retargeting
*/
export class StepLoadTargetModel extends EventTarget {
private readonly mesh2motion_engine: Mesh2MotionEngine
+ private readonly target_bone_tree_dialog: TargetBoneTreeDialog
private file_input: HTMLInputElement | null = null
private load_model_button: HTMLLabelElement | null = null
@@ -16,16 +18,39 @@ export class StepLoadTargetModel extends EventTarget {
constructor (mesh2motion_engine: Mesh2MotionEngine) {
super()
this.mesh2motion_engine = mesh2motion_engine
+ this.target_bone_tree_dialog = new TargetBoneTreeDialog(() => this.get_unique_target_bones())
}
public begin (): void {
this.add_event_listeners()
+ this.target_bone_tree_dialog.begin()
}
public get_retargetable_meshes (): Scene | null {
return this.retargetable_meshes
}
+ public get_unique_target_bones (): Map<string, Bone> {
+ const unique_bones: Map<string, Bone> = new Map<string, Bone>()
+
+ this.retargetable_meshes?.traverse((child) => {
+ if (child.type !== 'SkinnedMesh') {
+ return
+ }
+
+ const skinned_mesh = child as SkinnedMesh
+ skinned_mesh.skeleton.bones.forEach((bone) => {
+ unique_bones.set(bone.uuid, bone)
+ })
+ })
+
+ return unique_bones
+ }
+
+ public show_target_bone_tree_dialog (): void {
+ this.target_bone_tree_dialog.show()
+ }
+
private add_event_listeners (): void {
// Get DOM elements
this.file_input = document.getElementById('upload-file') as HTMLInputElement