Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Refactor: extract IndependentBoneMovement into its own class

copilot-swe-agent[bot]1ace8df2e191
diff --git a/src/Mesh2MotionEngine.ts b/src/Mesh2MotionEngine.ts
index e1efa13..91c7c42 100644
--- a/src/Mesh2MotionEngine.ts
+++ b/src/Mesh2MotionEngine.ts
@@ -253,17 +253,12 @@ export class Mesh2MotionEngine {
this.edit_skeleton_step.apply_mirror_mode(selected_bone, this.transform_controls.getMode())
}
- if (this.edit_skeleton_step.is_independent_bone_movement_enabled() &&
+ if (this.edit_skeleton_step.independent_bone_movement.is_enabled() &&
this.transform_controls.getMode() === 'translate') {
- this.edit_skeleton_step.apply_independent_bone_movement(selected_bone)
-
- // When mirror mode is also active, keep the mirror bone's children in place too
- if (this.edit_skeleton_step.is_mirror_mode_enabled()) {
- const mirror_bone = this.edit_skeleton_step.find_mirror_bone(selected_bone)
- if (mirror_bone !== undefined) {
- this.edit_skeleton_step.apply_independent_bone_movement(mirror_bone)
- }
- }
+ const mirror_bone = this.edit_skeleton_step.is_mirror_mode_enabled()
+ ? this.edit_skeleton_step.find_mirror_bone(selected_bone)
+ : undefined
+ this.edit_skeleton_step.independent_bone_movement.apply(selected_bone, mirror_bone)
}
}
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.ts
index 284cfda..ea14246 100644
--- a/src/lib/EventListeners.ts
+++ b/src/lib/EventListeners.ts
@@ -81,19 +81,13 @@ export class EventListeners {
this.bootstrap.edit_skeleton_step.store_bone_state_for_undo()
// Record children's initial world positions for independent bone movement
- if (this.bootstrap.edit_skeleton_step.is_independent_bone_movement_enabled()) {
+ if (this.bootstrap.edit_skeleton_step.independent_bone_movement.is_enabled()) {
const selected_bone = this.bootstrap.transform_controls.object
if (selected_bone !== undefined && selected_bone !== null) {
- this.bootstrap.edit_skeleton_step.record_children_initial_positions(selected_bone as Bone)
-
- // When mirror mode is also active, record the mirror bone's children
- // so they can also be kept in place during independent movement
- if (this.bootstrap.edit_skeleton_step.is_mirror_mode_enabled()) {
- const mirror_bone = this.bootstrap.edit_skeleton_step.find_mirror_bone(selected_bone as Bone)
- if (mirror_bone !== undefined) {
- this.bootstrap.edit_skeleton_step.record_mirror_bone_children_initial_positions(mirror_bone)
- }
- }
+ const mirror_bone = this.bootstrap.edit_skeleton_step.is_mirror_mode_enabled()
+ ? this.bootstrap.edit_skeleton_step.find_mirror_bone(selected_bone as Bone)
+ : undefined
+ this.bootstrap.edit_skeleton_step.independent_bone_movement.record_drag_start(selected_bone as Bone, mirror_bone)
}
}
}
diff --git a/src/lib/processes/edit-skeleton/IndependentBoneMovement.ts b/src/lib/processes/edit-skeleton/IndependentBoneMovement.ts
new file mode 100644
index 0000000..a6e5812
--- /dev/null
+++ b/src/lib/processes/edit-skeleton/IndependentBoneMovement.ts
@@ -0,0 +1,76 @@
+import { Vector3, type Bone } from 'three'
+
+/*
+ * IndependentBoneMovement
+ * Encapsulates the "Move Bone Independently" feature for the Edit Skeleton step.
+ *
+ * When enabled, moving a bone will not drag its children along with it. Instead,
+ * each direct bone-child's world position is snapshotted at the start of a drag,
+ * and then re-expressed in the (moving) parent's local frame every frame so that
+ * the children appear stationary in world space.
+ *
+ * If mirror mode is also active the same behaviour is applied to the mirror bone's
+ * children so that both sides of the skeleton stay in sync.
+ */
+export class IndependentBoneMovement {
+ private _enabled: boolean = false
+ private readonly _children_initial_world_positions: Map<string, Vector3> = new Map()
+
+ public is_enabled (): boolean {
+ return this._enabled
+ }
+
+ public set_enabled (value: boolean): void {
+ this._enabled = value
+ }
+
+ /**
+ * Snapshot the world positions of the direct bone children at drag start.
+ * Clears any previously stored positions first.
+ * When mirror mode is also active, pass the mirror bone as the second argument
+ * so its children are tracked in the same pass.
+ */
+ public record_drag_start (bone: Bone, mirror_bone?: Bone): void {
+ this._children_initial_world_positions.clear()
+ this._append_children_world_positions(bone)
+ if (mirror_bone !== undefined) {
+ this._append_children_world_positions(mirror_bone)
+ }
+ }
+
+ /**
+ * Re-pin the direct children of a bone to their snapshotted world positions.
+ * Call this every frame while the bone is being dragged.
+ * When mirror mode is also active, pass the mirror bone as the second argument
+ * so its children are pinned in the same call.
+ */
+ public apply (bone: Bone, mirror_bone?: Bone): void {
+ this._apply_to_bone(bone)
+ if (mirror_bone !== undefined) {
+ this._apply_to_bone(mirror_bone)
+ }
+ }
+
+ private _append_children_world_positions (bone: Bone): void {
+ bone.children.forEach((child) => {
+ if ('isBone' in child && child.isBone) {
+ const world_pos = new Vector3()
+ child.getWorldPosition(world_pos)
+ this._children_initial_world_positions.set(child.uuid, world_pos.clone())
+ }
+ })
+ }
+
+ private _apply_to_bone (bone: Bone): void {
+ bone.children.forEach((child) => {
+ if (!('isBone' in child) || !child.isBone) { return }
+ const initial_world_pos = this._children_initial_world_positions.get(child.uuid)
+ if (initial_world_pos === undefined) { return }
+ const local_pos = initial_world_pos.clone()
+ bone.worldToLocal(local_pos)
+ child.position.copy(local_pos)
+ // updateWorldMatrix(updateParents, updateChildren) - propagate changes up and down the hierarchy
+ child.updateWorldMatrix(true, true)
+ })
+ }
+}
diff --git a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
index e32caa5..5422e52 100644
--- a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
+++ b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
@@ -3,6 +3,7 @@ import { Generators } from '../../Generators.ts'
import { Utility } from '../../Utilities.ts'
import { UndoRedoSystem } from './UndoRedoSystem.ts'
import { PreviewPlaneManager } from './PreviewPlaneManager.ts'
+import { IndependentBoneMovement } from './IndependentBoneMovement.ts'
import {
Vector3,
Euler,
@@ -37,8 +38,6 @@ export class StepEditSkeleton extends EventTarget {
// Skeleton created from the armature that Three.js uses
private threejs_skeleton: Skeleton = new Skeleton()
private mirror_mode_enabled: boolean = true
- private independent_bone_movement_enabled: boolean = false
- private _children_initial_world_positions: Map<string, Vector3> = new Map()
private skinning_algorithm: string | null = null
private show_debug: boolean = true
@@ -55,6 +54,7 @@ export class StepEditSkeleton extends EventTarget {
private _added_event_listeners: boolean = false
private readonly preview_plane_manager: PreviewPlaneManager = PreviewPlaneManager.getInstance()
+ public readonly independent_bone_movement: IndependentBoneMovement = new IndependentBoneMovement()
constructor () {
super()
@@ -208,14 +208,6 @@ export class StepEditSkeleton extends EventTarget {
return this.mirror_mode_enabled
}
- public set_independent_bone_movement_enabled (value: boolean): void {
- this.independent_bone_movement_enabled = value
- }
-
- public is_independent_bone_movement_enabled (): boolean {
- return this.independent_bone_movement_enabled
- }
-
/**
* Find the mirrored counterpart of a bone by stripping side suffixes and
* matching against the rest of the skeleton. Returns undefined for centre-line
@@ -229,57 +221,6 @@ export class StepEditSkeleton extends EventTarget {
})
}
- /**
- * Record the current world positions of the direct bone children.
- * Clears any previously recorded positions, then stores the children of the
- * given bone. Call this when dragging starts so children can be restored to
- * these positions during independent bone movement.
- * Use record_mirror_bone_children_initial_positions() afterwards when mirror
- * mode is also active to append the mirror bone's children to the same map.
- */
- public record_children_initial_positions (bone: Bone): void {
- this._children_initial_world_positions.clear()
- this._append_children_world_positions(bone)
- }
-
- /**
- * Append the current world positions of a bone's direct children to the
- * existing position map without clearing it. Use this after
- * record_children_initial_positions() to also track the mirror bone's
- * children when both mirror mode and independent bone movement are active.
- */
- public record_mirror_bone_children_initial_positions (bone: Bone): void {
- this._append_children_world_positions(bone)
- }
-
- private _append_children_world_positions (bone: Bone): void {
- bone.children.forEach((child) => {
- if ('isBone' in child && child.isBone) {
- const world_pos = new Vector3()
- child.getWorldPosition(world_pos)
- this._children_initial_world_positions.set(child.uuid, world_pos.clone())
- }
- })
- }
-
- /**
- * Keep direct bone children at their initial world positions while the
- * parent bone is being moved. Call this each frame during a drag when
- * independent bone movement mode is active.
- */
- public apply_independent_bone_movement (bone: Bone): void {
- bone.children.forEach((child) => {
- if (!('isBone' in child) || !child.isBone) { return }
- const initial_world_pos = this._children_initial_world_positions.get(child.uuid)
- if (initial_world_pos === undefined) { return }
- const local_pos = initial_world_pos.clone()
- bone.worldToLocal(local_pos)
- child.position.copy(local_pos)
- // updateWorldMatrix(updateParents, updateChildren) - propagate changes up and down the hierarchy
- child.updateWorldMatrix(true, true)
- })
- }
-
public algorithm (): string | null {
return this.skinning_algorithm
}
@@ -335,7 +276,7 @@ export class StepEditSkeleton extends EventTarget {
if (this.ui.dom_independent_bone_movement_checkbox !== null) {
this.ui.dom_independent_bone_movement_checkbox.addEventListener('change', (event) => {
- this.set_independent_bone_movement_enabled(event.target.checked)
+ this.independent_bone_movement.set_enabled(event.target.checked)
})
}