"XrCreateSpatialAnchorsAsync in the Magic Leap API failed. Reason: PoseInvalid"

The main object in our app gets an anchor after two seconds of not being manipulated. For most objects this goes without a problem. Some objects completely disappear after getting an anchor.

After logging some stuff, I found the following. First, I exactly print out the lossy scale, word position and world rotation, and I get:

Ensuring anchor for (Transform) Patient3D Hologram ; lossyScale: (1.00, 1.00, 1.00), world pos: (-0.29, 0.78, 0.62), world rot: (0.00000, 1.00000, 0.00000, 0.00000)

Directly after that I add the anchor. Basically, “gameObject.AddComponent<ARAnchor>”

Like I said, for most objects this goes without a hitch. However, some objects completely disappear, and then I see in the log the following three errors:

Msg: Error: XrCreateSpatialAnchorsAsync in the Magic Leap API failed. Reason: PoseInvalid
StackTrace: UnityEngine.DebugLogHandler:Internal_Log(LogType, LogOption, String, Object)
MagicLeap.OpenXR.Utils:DidXrCallSucceed(XrResult, String, Predicate`1, Boolean)
MagicLeap.OpenXR.Subsystems.MagicLeapProvider:TryAddAnchor(Pose, XRAnchor&)
UnityEngine.GameObject:AddComponent()

Msg: XRAnchorSubsystem: Error creating Anchor at ((-0.29, 0.78, 0.62), (0.00000, 1.00000, 0.00000, 0.00000))
StackTrace: UnityEngine.DebugLogHandler:Internal_Log(LogType, LogOption, String, Object)
MagicLeap.OpenXR.Subsystems.MagicLeapProvider:TryAddAnchor(Pose, XRAnchor&)
UnityEngine.GameObject:AddComponent()

XRAnchorSubsystem: Failed Deleting Anchor from Subsystem 0000000000000000-0000000000000000
StackTrace: UnityEngine.DebugLogHandler:Internal_Log(LogType, LogOption, String, Object)
MagicLeap.OpenXR.Subsystems.MagicLeapProvider:TryRemoveAnchor(TrackableId)
UnityEngine.GameObject:AddComponent()

For the sake of being thorough, my OpenXR settings:

What the hell is wrong with this seemingly perfectly valid pose? Also, this happens always with some objects. It is perfectly reproducible. Other objects never fail. So it is not a tracking issue. For some reasons, one object always fails, some objects never. I have to add it is a large-ish compound object. However, with no anchor attached ML2 has no issue displaying it at all.

Edit: I also printed out the world matrix of the offending object:

-1.00000 0.00000 0.00000 -0.06836
0.00000 1.00000 0.00000 0.77463
0.00000 0.00000 -1.00000 0.48522
0.00000 0.00000 0.00000 1.00000

I also did print that from object that has no issues:

-0.99175 0.07537 0.10372 -0.09752
0.07038 0.99622 -0.05101 0.04878
-0.10717 -0.04329 -0.99330 0.22585
0.00000 0.00000 0.00000 1.00000

Using:

  • Unity 6000.49.f1
  • ML2 OS 1.12.0
  • Magic Leap SDK 2.6.0
  • Magic Leap MRTK 1.4.0
  • Host OS: Windows.
1 Like

To answer my own question - apparently your anchoring system has issues with ‘perfect rotations’. After a long debugging session, and some help from Gemini 2.5 pro, this is the solution. Just prior to adding an anchor, run this code:

public static void SanitizeTransformForAnchor(this Transform targetTransform)
{
    // 1. Check for negative scale via the matrix determinant. A negative value indicates a reflection.
    if (targetTransform.localToWorldMatrix.determinant < 0)
    {
        Debug.LogError($"Anchor pose rejected: Transform '{targetTransform.name}' or one of its parents has a negative scale (a reflection), which is not supported. Please correct the scale in the hierarchy.");
        return;
    }

    // 2. Check for "perfect" or "singular" rotations that might be unstable.
    // We detect this by checking if the rotation matrix has an unusually high number of zero elements.
    var m = targetTransform.localToWorldMatrix;
    var nearZeroElements = 0;
    for (var i = 0; i < 3; i++)
    {
        for (var j = 0; j < 3; j++)
        {
            // Check if the absolute value of the element in the 3x3 rotation part is very close to zero.
            if (Mathf.Abs(m[i, j]) < 1e-5f)
            {
                nearZeroElements++;
            }
        }
    }
    
    // A general rotation has few zero elements. A perfect axis-aligned rotation has 6 or more.
    // If we detect such a matrix, we apply a tiny perturbation to nudge it into a more stable state.
    if (nearZeroElements >= 6)
    {
        Debug.LogWarning($"Potential unstable matrix detected for '{targetTransform.name}'. Applying a small rotational perturbation to ensure stability.");
        targetTransform.rotation *= Quaternion.Euler(0.001f, 0.001f, 0.001f);
    }
}

Now this is quite a dirty hack, I suggest you fix that in your code :wink:

Joost, did you try inverting the 1 to -1. This would be the same rotation I think (check please) because a quaternion can be inverted to get the same result. Maybe the ML only can work with one of the two. So it would be (0, -1, 0, 0)