CoplayDev/unity-mcp · error · ValueError

{context} must have {expected_count} entries, got {len(value

Error message

{context} must have {expected_count} entries, got {len(value)}

What it means

Raised by _normalize_pixels (texture.py:170) when the pixels list length does not equal width*height. Each entry is one pixel; the count must match the declared dimensions exactly. Context is 'set-pixels pixels'. The expected_count is computed as width*height from the set-pixels payload (which already validated width/height > 0).

Source

Thrown at Server/src/cli/commands/texture.py:170

        raise ValueError(f"{context} must be a list of colors")
    return [_normalize_color(color, f"{context} item") for color in value]


def _normalize_pixels(value: Any, width: int, height: int, context: str) -> list[list[int]] | str:
    if value is None:
        raise ValueError(f"{context} is required")
    if isinstance(value, str):
        if value.startswith("base64:"):
            return value
        trimmed = value.strip()
        if trimmed.startswith("[") and trimmed.endswith("]"):
            value = try_parse_json(trimmed, context)
        else:
            return f"base64:{value}"
    if isinstance(value, list):
        expected_count = width * height
        if len(value) != expected_count:
            raise ValueError(
                f"{context} must have {expected_count} entries, got {len(value)}")
        return [_normalize_color(pixel, f"{context} pixel") for pixel in value]
    raise ValueError(f"{context} must be a list or base64 string")


def _normalize_set_pixels(value: Any) -> dict[str, Any]:
    if value is None:
        raise ValueError("set-pixels is required")
    if isinstance(value, str):
        value = try_parse_json(value, "set-pixels")
    if not isinstance(value, dict):
        raise ValueError("set-pixels must be a JSON object")

    result: dict[str, Any] = dict(value)

    if "pixels" in value:
        width = value.get("width")
        height = value.get("height")

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Make the pixels array length exactly width*height (row-major).
  2. Recompute the array from your source image at the declared width/height.
  3. Double-check width and height in the payload match the data you supplied.

Example fix

# before
unity-mcp texture modify Assets/Tex.png --set-pixels '{"x":0,"y":0,"width":2,"height":2,"pixels":[[255,0,0,255]]}'

# after
unity-mcp texture modify Assets/Tex.png --set-pixels '{"x":0,"y":0,"width":2,"height":2,"pixels":[[255,0,0,255],[0,255,0,255],[0,0,255,255],[255,255,0,255]]}'
Defensive patterns

Strategy: validation

Validate before calling

# Python - check pixel count before _normalize_pixels
expected = width * height
if isinstance(pixels, list) and len(pixels) != expected:
    raise ValueError(f"pixels must have {expected} entries, got {len(pixels)}")

Type guard

def pixel_count_matches(pixels, width, height) -> bool: return isinstance(pixels, list) and len(pixels) == width * height

Try / catch

try:
    result = _normalize_set_pixels(set_pixels)
except ValueError as e:
    print_error(str(e)); sys.exit(1)

Prevention

When it happens

Trigger: Passing a pixels array whose length != width*height, e.g. `--set-pixels '{"width":2,"height":2,"pixels":[[255,0,0,255]]}'` (1 pixel for a 2x2=4 target), or a 4-pixel array for a 3x3=9 target.

Common situations: Mismatch between declared width/height and the actual pixel count; flattening a 2D grid incorrectly; off-by-one in row/column counts; copying a pixels array from a different-sized texture.

Related errors


AI-assisted analysis of CoplayDev/unity-mcp@c21bf496bc (2026-08-13). Data as JSON: /api/errors/5d2b9dd6859ef35e. Report an issue: GitHub.