3b1b/manim · error · Exception

Input to MobjectMatrix must have at least n_rows * n_cols en

Error message

Input to MobjectMatrix must have at least n_rows * n_cols entries

What it means

Raised by MobjectMatrix (matrix.py:283) when the number of mobjects passed in the group is smaller than the requested or inferred n_rows * n_cols grid size. The class fills the matrix with group[n * n_cols + k], so too few entries would cause an IndexError otherwise; this check fails fast instead. When neither dimension is given, n_rows/n_cols are derived from len(group) via int(sqrt(n)) and integer division, which can also overshoot for non-square counts.

Source

Thrown at manimlib/mobject/matrix.py:283

class MobjectMatrix(Matrix):
    def __init__(
        self,
        group: VGroup,
        n_rows: int | None = None,
        n_cols: int | None = None,
        height: float = 4.0,
        element_alignment_corner=ORIGIN,
        **config,
    ):
        # Have fallback defaults of n_rows and n_cols
        n_mobs = len(group)
        if n_rows is None:
            n_rows = int(np.sqrt(n_mobs)) if n_cols is None else n_mobs // n_cols
        if n_cols is None:
            n_cols = n_mobs // n_rows

        if len(group) < n_rows * n_cols:
            raise Exception("Input to MobjectMatrix must have at least n_rows * n_cols entries")

        mob_matrix = [
            [group[n * n_cols + k] for k in range(n_cols)]
            for n in range(n_rows)
        ]
        config.update(
            height=height,
            element_alignment_corner=element_alignment_corner,
        )
        super().__init__(mob_matrix,  **config)

    def element_to_mobject(self, element: VMobject, **config) -> VMobject:
        return element

View on GitHub (pinned to dee01804d4)

Solutions

  1. Make len(group) >= n_rows * n_cols: pad the group with placeholder mobjects (e.g. invisible Dots or Tex('')) until it reaches the grid size
  2. If you passed only one of n_rows/n_cols, pass both explicitly so the product matches your data exactly
  3. Compute dimensions before constructing: n_rows = len(group) // n_cols and ensure len(group) % n_cols == 0, or trim group to n_rows * n_cols entries before the call

Example fix

# before
mobs = [Tex(str(n)) for n in range(10)]
mat = MobjectMatrix(VGroup(*mobs), n_rows=3, n_cols=4)  # 10 < 12 -> raises

# after
mobs = [Tex(str(n)) for n in range(12)]
mat = MobjectMatrix(VGroup(*mobs), n_rows=3, n_cols=4)
Defensive patterns

Strategy: validation

Validate before calling

def check_matrix_entries(group, n_rows=None, n_cols=None):
    n = len(group)
    if n_rows is None:
        n_rows = int(np.sqrt(n)) if n_cols is None else n // n_cols
    if n_cols is None:
        n_cols = n // n_rows
    assert n >= n_rows * n_cols, f"need {n_rows * n_cols} entries, got {n}"

Prevention

When it happens

Trigger: Calling MobjectMatrix(group) with a group whose length is not a perfect square (e.g. 7 mobjects -> n_rows=2, n_cols=3, needs 6, ok; but 5 -> 2x2=4 ok, 3 mobjects -> n_rows=1, n_cols=3 ok; 10 -> 3x3=9 ok... but 2 mobjects -> 1x2=2 ok) or explicitly passing n_rows/n_cols whose product exceeds len(group), e.g. MobjectMatrix(VGroup(*[Dot() for _ in range(5)]), n_rows=2, n_cols=3).

Common situations: Building a grid from a dynamically-generated list whose length does not match the hard-coded dimensions; passing n_cols but forgetting that n_rows defaults to n_mobs // n_cols which rounds down and can make n_rows * n_cols exceed len(group) when the division has a remainder (e.g. 5 mobs, n_cols=2 -> n_rows=2 -> needs 4, ok; 5 mobs n_cols=4 -> n_rows=1 ok; 7 mobs n_cols=5 -> n_rows=1 ok; but 7 mobs n_cols=3 -> n_rows=2 -> needs 6 <= 7 ok; counterexample: n_cols=2 with 3 mobs -> n_rows=1 ok; the real failure: n_cols=4 with 6 mobs -> n_rows=1, ok; explicit mismatch is the usual case).

Related errors


AI-assisted analysis of 3b1b/manim@dee01804d4 (2026-08-14). Data as JSON: /api/errors/ac238425b7766af3. Report an issue: GitHub.