CoplayDev/unity-mcp · error · ValueError

set-pixels width and height must be positive

Error message

set-pixels width and height must be positive

What it means

Thrown by _normalize_set_pixels() when pixels are provided and width/height are present but one or both are <= 0 after int() coercion. Texture dimensions must be positive integers; zero or negative sizes are rejected before pixel normalization runs.

Solutions

  1. Set width and height to positive integers that multiply to the pixel count.
  2. If dimensions are computed, add an assertion `assert width > 0 and height > 0` before building the payload.
  3. Cross-check the pixel array length equals width*height.

Example fix

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

Strategy: validation

Validate before calling

w, h = int(sp["width"]), int(sp["height"])
if w <= 0 or h <= 0:
    raise ValueError("set-pixels width and height must be positive")

Type guard

def dims_positive(v: dict) -> bool:
    return all(int(v[k]) > 0 for k in ("width", "height") if k 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 width or height as 0, a negative number, or a numeric string that parses to a non-positive int, e.g. `--set-pixels '{"pixels":[...],"width":0,"height":4}'`. Also when a non-numeric value triggers an earlier int() failure (that surfaces as a different ValueError).

Common situations: Dimensions computed from another source produce zero (e.g. an empty image's width), or a typo/sign error yields negatives. Common in generated commands where a size variable defaulted to 0.

Related errors


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

Appendix: source

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

    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"])
    if "y" in value:
        result["y"] = int(value["y"])

    if "width" in value and "pixels" not in value:
        result["width"] = int(value["width"])

View on GitHub (pinned to c21bf496bc)