CoplayDev/unity-mcp · error · InvalidOperationException

Active Scene View has no camera to derive viewport size from

Error message

Active Scene View has no camera to derive viewport size from.

What it means

GetViewportLocalRectPoints() first tries the internal 'cameraViewport' Rect property on the SceneView. If that is unavailable or zero-size, it falls back to sceneView.camera. If camera is null, it throws InvalidOperationException because no viewport dimensions can be derived. This is the deepest fallback in the viewport resolution chain.

Source

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

            return new Rect(
                Mathf.Round(viewportLocalPoints.x * pixelsPerPoint),
                Mathf.Round(viewportLocalPoints.y * pixelsPerPoint),
                Mathf.Round(viewportLocalPoints.width * pixelsPerPoint),
                Mathf.Round(viewportLocalPoints.height * pixelsPerPoint));
        }

        private static Rect GetViewportLocalRectPoints(SceneView sceneView, float pixelsPerPoint)
        {
            Rect? cameraViewport = GetRectProperty(sceneView, "cameraViewport");
            if (cameraViewport.HasValue && cameraViewport.Value.width > 0f && cameraViewport.Value.height > 0f)
            {
                return cameraViewport.Value;
            }

            Camera camera = sceneView.camera;
            if (camera == null)
                throw new InvalidOperationException("Active Scene View has no camera to derive viewport size from.");

            float viewportWidth = camera.pixelWidth / Mathf.Max(0.0001f, pixelsPerPoint);
            float viewportHeight = camera.pixelHeight / Mathf.Max(0.0001f, pixelsPerPoint);
            Rect windowRect = sceneView.position;

            return new Rect(
                0f,
                Mathf.Max(0f, windowRect.height - viewportHeight),
                Mathf.Min(windowRect.width, viewportWidth),
                Mathf.Min(windowRect.height, viewportHeight));
        }

        private static Texture2D CaptureViewRect(SceneView sceneView, Rect viewportRectPixels)
        {
            object hostView = GetHostView(sceneView);
            if (hostView == null)
                throw new InvalidOperationException("Failed to resolve Scene view host view.");

View on GitHub (pinned to c21bf496bc)

Solutions

  1. Delay the capture until EditorApplication.update fires after the scene is fully loaded.
  2. Check sceneView.camera != null before calling capture and retry if null.
  3. Ensure no domain reload or scene transition is in progress.
  4. Use EditorApplication.delayCall to defer the capture until the Editor is stable.

Example fix

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

// after
if (sceneView.camera == null)
{
    EditorApplication.delayCall += () => Capture(sceneView);
    return;
}
var result = EditorWindowScreenshotUtility.CaptureSceneViewViewportToProject(sceneView, ...);
Defensive patterns

Strategy: validation

Validate before calling

if (sceneView.camera == null)
{ EditorApplication.delayCall += () => RetryCapture(sceneView); return; }

Type guard

public static bool IsSceneViewCameraReady(SceneView sv) =>
    GetRectProperty(sv, "cameraViewport")?.width > 0 || sv.camera != null;

Try / catch

try { CaptureSceneViewViewportToProject(sceneView, ...); }
catch (InvalidOperationException ex) when (ex.Message.Contains("no camera"))
{ EditorApplication.delayCall += () => RetryCapture(sceneView); }

Prevention

When it happens

Trigger: The 'cameraViewport' internal property does not exist or returns a zero-size rect (e.g., Unity version difference), AND sceneView.camera is null. This occurs when the Scene View's camera has been destroyed or not yet created.

Common situations: Capturing during a scene load or domain reload where the camera is temporarily null. A Unity version where the cameraViewport property is absent and the camera is not initialized. The Scene View was just opened and the camera has not been assigned yet.

Related errors


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