CoplayDev/unity-mcp · error · InvalidOperationException

Captured Scene view viewport is empty.

Error message

Captured Scene view viewport is empty.

What it means

CaptureSceneViewViewportToProject resolves the Scene View viewport pixel rect, then checks that viewportWidth and viewportHeight are positive. If both dimensions resolved to zero or negative, it throws InvalidOperationException. This means the rect computation succeeded (no earlier exception) but produced an empty area — the Scene View exists but has no drawable surface.

Source

Thrown at MCPForUnity/Editor/Helpers/EditorWindowScreenshotUtility.cs:65

            bool includeImage,
            int maxResolution,
            out int viewportWidth,
            out int viewportHeight,
            string folderOverride = null)
        {
            if (sceneView == null)
                throw new ArgumentNullException(nameof(sceneView));

            int effectiveSuperSize = NormalizeSceneViewSuperSize(superSize);

            FocusAndRepaint(sceneView);

            Rect viewportRectPixels = GetSceneViewViewportPixelRect(sceneView);
            viewportWidth = Mathf.RoundToInt(viewportRectPixels.width);
            viewportHeight = Mathf.RoundToInt(viewportRectPixels.height);

            if (viewportWidth <= 0 || viewportHeight <= 0)
                throw new InvalidOperationException("Captured Scene view viewport is empty.");

            Texture2D captured = null;
            Texture2D downscaled = null;
            try
            {
                captured = CaptureViewRect(sceneView, viewportRectPixels);

                var result = PrepareCaptureResult(fileName, effectiveSuperSize, ensureUniqueFileName, folderOverride);
                byte[] png = captured.EncodeToPNG();
                File.WriteAllBytes(result.FullPath, png);

                if (includeImage)
                {
                    int targetMax = maxResolution > 0 ? maxResolution : 640;
                    string imageBase64;
                    int imageWidth;
                    int imageHeight;

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Ensure the Scene View window is visible and has non-zero area before capturing.
  2. Do not call scene capture in batchmode or headless Unity sessions.
  3. If in CI, run with a display or use off-screen rendering alternatives.

Example fix

// before
var result = EditorWindowScreenshotUtility.CaptureSceneViewViewportToProject(sceneView, ...);

// after
if (sceneView.position.width <= 0 || sceneView.position.height <= 0)
    throw new InvalidOperationException("Scene View is not visible.");
var result = EditorWindowScreenshotUtility.CaptureSceneViewViewportToProject(sceneView, ...);
Defensive patterns

Strategy: validation

Validate before calling

Rect pos = sceneView.position;
if (pos.width <= 0 || pos.height <= 0) return;
if (Application.isBatchMode) return;

Type guard

public static bool IsSceneViewCapturable(SceneView sv) =>
    sv != null && sv.position.width > 0 && sv.position.height > 0 && !Application.isBatchMode;

Try / catch

try { CaptureSceneViewViewportToProject(sceneView, ...); }
catch (InvalidOperationException ex) when (ex.Message.Contains("viewport is empty"))
{ Debug.LogWarning("Scene View has no visible area; cannot capture."); }

Prevention

When it happens

Trigger: Capturing a Scene View that is docked but has zero visible area (fully covered by other tabs/panels), minimized, or in a headless/batchmode Editor session where no actual viewport is rendered.

Common situations: Running Unity in batchmode (-batchmode flag) where there is no rendered Scene View. The Scene View tab is hidden behind a maximized Game View or Inspector. Capturing during a layout transition. Running in a CI environment without a display.

Related errors


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