Custom SkeletonModifier3D causes deformed bones

Godot Version

4.7.2.stable

Question

I have a question about Skeleton3D and modifiers.

I coded a small SkeletonModifier3D to modify a Skeleton after a FABRIK modifier to avoid clipping into the target.

The modifier changes the position of the bones to be outside of the target mesh (start image on the left, end image on the right), the problem this results is a weirdly deformed mesh and I suspect the problem is my bones are deformed after my modification (blue lines not aligned with the bone).

How do I go to correct these lines once I have moved the bones with set_bone_global_pose?

What I tried:

  • Rotating the previous bone to look at the new one but it didn’t work
  • Applying another modifier after (SpringBoneSimulator3D) to see if it would correct it.

Here is the code snippet:

@tool
class_name NoClipModifier
extends SkeletonModifier3D


var target_coordinate: Vector3 = Vector3(0, 0, 0)
var radius:float = 1.0


func _process_modification() -> void:
	var skeleton: Skeleton3D = get_skeleton()
		
	for i in range(skeleton.get_bone_count()):

		var bone_idx: int = i
		var parent_idx: int = skeleton.get_bone_parent(bone_idx)
		var pose: Transform3D = skeleton.global_transform * skeleton.get_bone_global_pose(bone_idx)
				
		var distance_to_target_center:float = target_coordinate.distance_squared_to(pose.origin)
		var direction_to_target_center:Vector3 = (pose.origin-target_coordinate).normalized()
			
		if distance_to_target_center < radius*radius:
			var current_global_pose: Transform3D = skeleton.get_bone_global_pose(bone_idx)
			var bone_tansform = pose
			bone_tansform.origin = skeleton.to_local(target_coordinate+direction_to_target_center * radius*1.1)
			bone_tansform.basis = bone_tansform.basis.orthonormalized()
			skeleton.set_bone_global_pose(bone_idx, bone_tansform)

Any help solving this would be greatly appreciated.

And a MRE:

Can you post the MRE? But if the bones go outside the mesh, then the mesh is supposed to deform weirdly in that case — that’s normal behavior. The bones should always be inside the mesh.

What is the MRE ?

Sorry I wasn’t clear, the mesh the bones are pushed out of is the target as to avoid clipping not the skin mesh.

Here is the smallest MRE I could do:

Well, your target is too close to the base (root). If you move the target farther away, everything will be fine.

Note: When the target is close to the root, it tends to produce zig-zag patterns, resulting in unnatural visual movement.

Ah ok so its not really possible to correct it, I will have to find a workaround then.