{"record":{"id":"3014fd122e2962a1","repo":"CoplayDev/unity-mcp","slug":"active-scene-view-has-no-camera-to-derive-viewport","errorCode":null,"errorMessage":"Active Scene View has no camera to derive viewport size from.","messagePattern":"Active Scene View has no camera to derive viewport size from\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MCPForUnity/Editor/Helpers/EditorWindowScreenshotUtility.cs","lineNumber":167,"sourceCode":"\n            return new Rect(\n                Mathf.Round(viewportLocalPoints.x * pixelsPerPoint),\n                Mathf.Round(viewportLocalPoints.y * pixelsPerPoint),\n                Mathf.Round(viewportLocalPoints.width * pixelsPerPoint),\n                Mathf.Round(viewportLocalPoints.height * pixelsPerPoint));\n        }\n\n        private static Rect GetViewportLocalRectPoints(SceneView sceneView, float pixelsPerPoint)\n        {\n            Rect? cameraViewport = GetRectProperty(sceneView, \"cameraViewport\");\n            if (cameraViewport.HasValue && cameraViewport.Value.width > 0f && cameraViewport.Value.height > 0f)\n            {\n                return cameraViewport.Value;\n            }\n\n            Camera camera = sceneView.camera;\n            if (camera == null)\n                throw new InvalidOperationException(\"Active Scene View has no camera to derive viewport size from.\");\n\n            float viewportWidth = camera.pixelWidth / Mathf.Max(0.0001f, pixelsPerPoint);\n            float viewportHeight = camera.pixelHeight / Mathf.Max(0.0001f, pixelsPerPoint);\n            Rect windowRect = sceneView.position;\n\n            return new Rect(\n                0f,\n                Mathf.Max(0f, windowRect.height - viewportHeight),\n                Mathf.Min(windowRect.width, viewportWidth),\n                Mathf.Min(windowRect.height, viewportHeight));\n        }\n\n        private static Texture2D CaptureViewRect(SceneView sceneView, Rect viewportRectPixels)\n        {\n            object hostView = GetHostView(sceneView);\n            if (hostView == null)\n                throw new InvalidOperationException(\"Failed to resolve Scene view host view.\");\n","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/MCPForUnity/Editor/Helpers/EditorWindowScreenshotUtility.cs#L149-L185","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Delay the capture until EditorApplication.update fires after the scene is fully loaded.","Check sceneView.camera != null before calling capture and retry if null.","Ensure no domain reload or scene transition is in progress.","Use EditorApplication.delayCall to defer the capture until the Editor is stable."],"exampleFix":"// before\nvar result = EditorWindowScreenshotUtility.CaptureSceneViewViewportToProject(sceneView, ...);\n\n// after\nif (sceneView.camera == null)\n{\n    EditorApplication.delayCall += () => Capture(sceneView);\n    return;\n}\nvar result = EditorWindowScreenshotUtility.CaptureSceneViewViewportToProject(sceneView, ...);","handlingStrategy":"validation","validationCode":"if (sceneView.camera == null)\n{ EditorApplication.delayCall += () => RetryCapture(sceneView); return; }","typeGuard":"public static bool IsSceneViewCameraReady(SceneView sv) =>\n    GetRectProperty(sv, \"cameraViewport\")?.width > 0 || sv.camera != null;","tryCatchPattern":"try { CaptureSceneViewViewportToProject(sceneView, ...); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"no camera\"))\n{ EditorApplication.delayCall += () => RetryCapture(sceneView); }","preventionTips":["Verify sceneView.camera is non-null before capturing.","Defer capture until after the scene is fully loaded.","Avoid capturing during domain reloads or scene transitions."],"tags":["screenshot","scene-view","camera","initialization","editor"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}