Comfy-Org/ComfyUI · error · ValueError
Invalid rotation matrix shape {matrix.shape}.
Error message
Invalid rotation matrix shape {matrix.shape}. What it means
mat_to_quat() converts rotation matrices to xyzw quaternions and requires the input's last two dimensions to be exactly 3x3. Any other trailing shape raises ValueError with the received shape. It is a hard precondition of the conversion math, which unbinds exactly nine matrix entries.
Source
Thrown at comfy/ldm/depth_anything_3/transform.py:80
1 - two_s * (j * j + k * k),
two_s * (i * j - k * r),
two_s * (i * k + j * r),
two_s * (i * j + k * r),
1 - two_s * (i * i + k * k),
two_s * (j * k - i * r),
two_s * (i * k - j * r),
two_s * (j * k + i * r),
1 - two_s * (i * i + j * j),
),
-1,
)
return o.reshape(quaternions.shape[:-1] + (3, 3))
def mat_to_quat(matrix: torch.Tensor) -> torch.Tensor:
"""Convert (...,3,3) rotation matrices to quaternions (xyzw)."""
if matrix.size(-1) != 3 or matrix.size(-2) != 3:
raise ValueError(f"Invalid rotation matrix shape {matrix.shape}.")
batch_dim = matrix.shape[:-2]
m00, m01, m02, m10, m11, m12, m20, m21, m22 = torch.unbind(
matrix.reshape(batch_dim + (9,)), dim=-1
)
q_abs = _sqrt_positive_part(
torch.stack(
[
1.0 + m00 + m11 + m22,
1.0 + m00 - m11 - m22,
1.0 - m00 + m11 - m22,
1.0 - m00 - m11 + m22,
],
dim=-1,
)
)
View on GitHub (pinned to 1c6d8d45b3)
Solutions
- Slice the rotation block first: mat_to_quat(pose[..., :3, :3]).
- Verify with matrix.shape[-2:] == (3, 3) before the call when the source of the tensor is uncertain.
- If input is (3,4) extrinsics, drop the translation column before converting.
Example fix
# before q = mat_to_quat(pose_4x4) # after q = mat_to_quat(pose_4x4[..., :3, :3])
Defensive patterns
Strategy: type-guard
Validate before calling
assert matrix.shape[-2:] == (3, 3), f"need (...,3,3), got {tuple(matrix.shape)}" Type guard
def is_rotation_matrix_tensor(m: "torch.Tensor") -> bool:
return m.ndim >= 2 and m.shape[-2:] == (3, 3) Prevention
- Always slice pose[..., :3, :3] before quaternion conversion.
- Keep rotation matrices (3x3) and homogeneous poses (4x4) in separately named variables.
When it happens
Trigger: Calling mat_to_quat on 4x4 pose matrices (forgetting to slice [...,:3,:3]), on (...,3) Euler angles, on batched tensors whose reshape earlier collapsed the matrix dims, or on transposed translation-augmented (3,4) tensors.
Common situations: Camera-pose pipelines that store homogeneous 4x4 matrices and pass them straight through; mixing up quaternion-to-matrix (output 3x3) and matrix-to-quaternion (input must be 3x3) call directions.
Related errors
- Invalid affine shape: {ext.shape}
- Unexpected token width: {out_x.shape[-1]}
- Unknown DINOv2 backbone variant: {backbone_name!r}
- Unsupported process_res_method: {method}
- At least 4 points are required to compute a homography.
AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14).
Data as JSON: /api/errors/e0ed41a7d2424b20.
Report an issue: GitHub.