Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Extend independent bone movement to mirror bone when both modes are active

copilot-swe-agent[bot]503514c01512
diff --git a/src/Mesh2MotionEngine.ts b/src/Mesh2MotionEngine.ts
index 3f8e0e3..e1efa13 100644
--- a/src/Mesh2MotionEngine.ts
+++ b/src/Mesh2MotionEngine.ts
@@ -247,15 +247,23 @@ export class Mesh2MotionEngine {
}
public handle_transform_controls_moving (): void {
+ const selected_bone: Bone = this.transform_controls.object as Bone
+
if (this.edit_skeleton_step.is_mirror_mode_enabled()) {
- const selected_bone: Bone = this.transform_controls.object as Bone
this.edit_skeleton_step.apply_mirror_mode(selected_bone, this.transform_controls.getMode())
}
if (this.edit_skeleton_step.is_independent_bone_movement_enabled() &&
this.transform_controls.getMode() === 'translate') {
- const selected_bone: Bone = this.transform_controls.object as Bone
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)
+ }
+ }
}
}
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.ts
index 522c41b..284cfda 100644
--- a/src/lib/EventListeners.ts
+++ b/src/lib/EventListeners.ts
@@ -85,6 +85,15 @@ export class EventListeners {
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)
+ }
+ }
}
}
}
diff --git a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
index 8366159..e32caa5 100644
--- a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
+++ b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
@@ -216,13 +216,43 @@ export class StepEditSkeleton extends EventTarget {
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
+ * bones (spine, neck, head, etc.) that have no counterpart.
+ */
+ public find_mirror_bone (bone: Bone): Bone | undefined {
+ const base_name = Utility.calculate_bone_base_name(bone.name)
+ return this.threejs_skeleton.bones.find((candidate) => {
+ const candidate_base = Utility.calculate_bone_base_name(candidate.name)
+ return candidate_base === base_name && candidate.name !== bone.name
+ })
+ }
+
/**
* Record the current world positions of the direct bone children.
- * Call this when dragging starts so children can be restored to
+ * 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()
@@ -455,27 +485,10 @@ export class StepEditSkeleton extends EventTarget {
}
public apply_mirror_mode (selected_bone: Bone, transform_type: string): void {
- // if we are on the positive side mirror mode is enabled
- // we need to change the position of the bone on the other side of the mirror
-
- // first step is to find the base bone name
- // strip out the left/right and _L/_R from the name
- // mixamo is a common skeleton that prefixes everything with mixamorig_, so remove that
- const base_bone_name = Utility.calculate_bone_base_name(selected_bone.name)
-
- // Find another bone that has the same base name
- // that should be the mirror
- let mirror_bone: Bone | undefined
-
- this.threejs_skeleton.bones.forEach((bone) => {
- const bone_name_to_compare = Utility.calculate_bone_base_name(bone.name)
- if (bone_name_to_compare === base_bone_name && bone.name !== selected_bone.name) {
- mirror_bone = bone
- }
- })
+ const mirror_bone = this.find_mirror_bone(selected_bone)
if (mirror_bone === undefined) {
- return // we probably something along the axis (head, neck, spine)
+ return // centre-line bone (head, neck, spine) — no counterpart
}
if (transform_type === 'translate') {
@@ -498,6 +511,7 @@ export class StepEditSkeleton extends EventTarget {
mirror_bone.quaternion.setFromEuler(euler)
}
+ // updateWorldMatrix(updateParents, updateChildren) - propagate changes up and down the hierarchy
mirror_bone.updateWorldMatrix(true, true)
}