CoplayDev/unity-mcp · error · ValueError

set-pixels requires width and height when pixels are provide

Error message

set-pixels requires width and height when pixels are provided

What it means

Thrown by _normalize_set_pixels() when the set-pixels object contains a 'pixels' key but is missing 'width' and/or 'height'. Pixel data is meaningless without dimensions, so the normalizer refuses to proceed and requires both fields alongside the pixel array.

Source

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

        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")
        if width is None or height is None:
            raise ValueError(
                "set-pixels requires width and height when pixels are provided")
        width = int(width)
        height = int(height)
        if width <= 0 or height <= 0:
            raise ValueError("set-pixels width and height must be positive")
        result["width"] = width
        result["height"] = height
        result["pixels"] = _normalize_pixels(
            value["pixels"], width, height, "set-pixels pixels")

    if "color" in value:
        result["color"] = _normalize_color(value["color"], "set-pixels color")

    if "pixels" not in value and "color" not in value:
        raise ValueError("set-pixels requires 'color' or 'pixels'")

    if "x" in value:
        result["x"] = int(value["x"])

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Add both width and height: `--set-pixels '{"pixels":[...],"width":2,"height":2}'`.
  2. Ensure width*height equals the pixel count (a separate ValueError guards count mismatch).
  3. For a uniform fill, drop the pixels array and use color instead: `--set-pixels '{"color":"#FF0000","width":2,"height":2}'`.

Example fix

// before
--set-pixels '{"pixels":["#FF0000","#00FF00"]}'
// after
--set-pixels '{"pixels":["#FF0000","#00FF00"],"width":2,"height":1}'
Defensive patterns

Strategy: validation

Validate before calling

if "pixels" in sp and ("width" not in sp or "height" not in sp):
    raise ValueError("set-pixels requires width and height when pixels are provided")

Type guard

def has_pixels_with_dims(v: dict) -> bool:
    return "pixels" not in v or ("width" in v and "height" in v)

Try / catch

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

Prevention

When it happens

Trigger: Passing `--set-pixels '{"pixels":[...]}'` without width/height, or providing only one of width/height. Reproducible whenever the pixels array is present but the dimension keys are absent or named differently.

Common situations: A developer assumes default dimensions exist (they don't — only create() has width/height defaults, not set-pixels), or uses camelCase/alternate key names that aren't recognized.

Related errors


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