3b1b/manim · error · Exception
Matrix has bad dimensions
Error message
Matrix has bad dimensions
What it means
ApplyMatrix animates a linear transformation of a mobject's points. initialize_matrix accepts exactly a (2,2) matrix (promoted to 3x3 in the xy-plane) or a (3,3) matrix; anything else — 1x3, 4x4, non-square, wrong dtype shape — raises 'Matrix has bad dimensions'.
Source
Thrown at manimlib/animation/transform.py:288
matrix: npt.ArrayLike,
mobject: Mobject,
**kwargs
):
matrix = self.initialize_matrix(matrix)
def func(p):
return np.dot(p, matrix.T)
super().__init__(func, mobject, **kwargs)
def initialize_matrix(self, matrix: npt.ArrayLike) -> np.ndarray:
matrix = np.array(matrix)
if matrix.shape == (2, 2):
new_matrix = np.identity(3)
new_matrix[:2, :2] = matrix
matrix = new_matrix
elif matrix.shape != (3, 3):
raise Exception("Matrix has bad dimensions")
return matrix
class ApplyComplexFunction(ApplyMethod):
def __init__(
self,
function: Callable[[complex], complex],
mobject: Mobject,
**kwargs
):
self.function = function
method = mobject.apply_complex_function
super().__init__(method, function, **kwargs)
def init_path_func(self) -> None:
func1 = self.function(complex(1))
self.path_arc = np.log(func1).imag
super().init_path_func()View on GitHub (pinned to dee01804d4)
Solutions
- Reshape to exactly 2x2 or 3x3: np.array(mat).reshape(3, 3)
- For homogeneous transforms, drop the last row and column before passing
- For pure rotations, prefer ApplyMethod(mob.rotate, angle, axis=Z) or Rotation(angle) which handle the matrix for you
Example fix
# before self.play(ApplyMatrix(rotation_matrix_4x4, mob)) # after self.play(ApplyMatrix(rotation_matrix_4x4[:3, :3], mob))
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
mat = np.asarray(mat)
assert mat.shape in ((2, 2), (3, 3)), f"matrix must be 2x2 or 3x3, got {mat.shape}" Type guard
import numpy as np
def is_valid_transform_matrix(mat) -> bool:
return np.asarray(mat).shape in ((2, 2), (3, 3)) Prevention
- Build matrices with manim's rotation_matrix(...) helpers, which return 3x3
- Strip the homogeneous row/column when copying 4x4 matrices from other graphics code
When it happens
Trigger: ApplyMatrix([[1, 0, 0], [0, 1, 0]], mob) (2x3); passing a rotation_about_z(...) result plus an extra row; passing a 4x4 homogeneous matrix from graphics code; passing a flat list of 9 numbers without reshaping.
Common situations: Mixing up homogeneous 4x4 matrices from other graphics libraries; building matrices by hand and getting row counts wrong; intending ApplyPointwiseFunction or apply_matrix on the mobject instead.
Related errors
- {self.__class__.__name__}.create_target not properly impleme
- MoveToTarget called on mobject without attribute 'target'
- Whoops, looks like you accidentally invoked the method you w
- Trying to restore without having saved
- Functions passed to ApplyFunction must return object of type
AI-assisted analysis of 3b1b/manim@dee01804d4 (2026-08-14).
Data as JSON: /api/errors/3b0c7754fda6e39c.
Report an issue: GitHub.