CoplayDev/unity-mcp · error · ValueError

width and height must be positive

Error message

width and height must be positive

What it means

Raised by _validate_texture_dimensions (texture.py:64) when width or height is <= 0. The function validates the --width/--height options of the `texture create` and `texture sprite` CLI commands before they are sent to Unity as the new Texture2D dimensions. The CLI handlers wrap the call in try/except ValueError and exit(1) on failure, so the user sees the message and a non-zero exit code. (Separately, the same function emits soft warnings when dimensions exceed 1024 or total pixels exceed 1,048,576.)

Source

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

    "low_quality": "CompressedLQ",
    "normal_quality": "Compressed",
    "high_quality": "CompressedHQ",
}

_SPRITE_MODES = {"single": "Single",
                 "multiple": "Multiple", "polygon": "Polygon"}

_SPRITE_MESH_TYPES = {"full_rect": "FullRect", "tight": "Tight"}

_MIPMAP_FILTERS = {"box": "BoxFilter", "kaiser": "KaiserFilter"}

_MAX_TEXTURE_DIMENSION = 1024
_MAX_TEXTURE_PIXELS = 1024 * 1024


def _validate_texture_dimensions(width: int, height: int) -> list[str]:
    if width <= 0 or height <= 0:
        raise ValueError("width and height must be positive")
    warnings: list[str] = []
    if width > _MAX_TEXTURE_DIMENSION or height > _MAX_TEXTURE_DIMENSION:
        warnings.append(
            f"width and height should be <= {_MAX_TEXTURE_DIMENSION} (got {width}x{height})")
    total_pixels = width * height
    if total_pixels > _MAX_TEXTURE_PIXELS:
        warnings.append(
            f"width*height should be <= {_MAX_TEXTURE_PIXELS} (got {width}x{height})")
    return warnings


def _is_normalized_color(values: list[Any]) -> bool:
    if not values:
        return False

    try:
        numeric_values = [float(v) for v in values]
    except (TypeError, ValueError):

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Pass positive integers for --width and --height (e.g. --width 64 --height 64).
  2. If dimensions come from a script, clamp them to a minimum of 1 before invoking the CLI.
  3. Drop --width/--height to accept the default 64 when you have no specific size requirement.

Example fix

# before
unity-mcp texture create Assets/Tex.png --width 0 --height 64

# after
unity-mcp texture create Assets/Tex.png --width 64 --height 64
Defensive patterns

Strategy: validation

Validate before calling

# Python - before calling the CLI / _validate_texture_dimensions
if not isinstance(width, int) or not isinstance(height, int) or width <= 0 or height <= 0:
    raise SystemExit(f"width and height must be positive (got {width}x{height})")

Type guard

def is_positive_int(v) -> bool: return isinstance(v, int) and not isinstance(v, bool) and v > 0

Try / catch

try:
    warnings = _validate_texture_dimensions(width, height)
except ValueError as e:
    print_error(str(e)); sys.exit(1)

Prevention

When it happens

Trigger: Running `unity-mcp texture create <path> --width 0` (or negative), or `--height 0`, or omitting them in a context where they resolve to <= 0 (note Click defaults them to 64, so you must pass an explicit non-positive value). Triggered only on the create/sprite commands, not when --image-path is used (that branch skips dimension validation).

Common situations: Passing `--width 0` or `--width -1`; a script computing width/height that underflows to 0; copy-pasting a command with a typo'd negative number.

Related errors


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