{"record":{"id":"445437ebfd4819e9","repo":"CoplayDev/unity-mcp","slug":"failed-to-resolve-scene-view-viewport-rect","errorCode":null,"errorMessage":"Failed to resolve Scene view viewport rect.","messagePattern":"Failed to resolve Scene view viewport rect\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MCPForUnity/Editor/Helpers/EditorWindowScreenshotUtility.cs","lineNumber":148,"sourceCode":"                sceneView.Repaint();\n                InvokeMethodIfExists(sceneView, \"RepaintImmediately\");\n                SceneView.RepaintAll();\n                UnityEditorInternal.InternalEditorUtility.RepaintAllViews();\n                EditorApplication.QueuePlayerLoopUpdate();\n                Thread.Sleep(RepaintSettlingDelayMs);\n            }\n            catch (Exception ex)\n            {\n                McpLog.Debug($\"[EditorWindowScreenshotUtility] SceneView repaint failed: {ex.Message}\");\n            }\n        }\n\n        private static Rect GetSceneViewViewportPixelRect(SceneView sceneView)\n        {\n            float pixelsPerPoint = EditorGUIUtility.pixelsPerPoint;\n            Rect viewportLocalPoints = GetViewportLocalRectPoints(sceneView, pixelsPerPoint);\n            if (viewportLocalPoints.width <= 0f || viewportLocalPoints.height <= 0f)\n                throw new InvalidOperationException(\"Failed to resolve Scene view viewport rect.\");\n\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)","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/MCPForUnity/Editor/Helpers/EditorWindowScreenshotUtility.cs#L130-L166","documentation":"GetSceneViewViewportPixelRect() calls GetViewportLocalRectPoints() to get the viewport in local points, then checks width/height. If the local-points rect has zero or negative dimensions, it throws before the pixel-space conversion. This is an earlier failure than [15]: the viewport geometry itself could not be resolved, before even computing pixel dimensions.","triggerScenarios":"GetViewportLocalRectPoints returns a zero-size rect because neither the internal 'cameraViewport' property nor sceneView.camera yields a valid size. This happens when the Scene View is not yet initialized, during scene loading, or in layout transitions.","commonSituations":"Capturing immediately after opening a project before the Scene View finishes initializing. The Scene View is in a transitional state (docking/undocking). The cameraViewport internal property is unavailable in a specific Unity version and camera.pixelWidth/Height are zero.","solutions":["Wait for the Scene View to finish initialization (EditorApplication.update delay) before capturing.","Ensure the Scene View is focused and fully rendered before calling capture.","Retry the capture after a short delay if the viewport is in a transitional state.","Verify sceneView.camera is non-null and has positive pixelWidth/pixelHeight before calling."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"Rect? vp = GetRectProperty(sceneView, \"cameraViewport\");\nCamera cam = sceneView.camera;\nif ((vp == null || vp.Value.width <= 0) && (cam == null || cam.pixelWidth <= 0))\n{ /* viewport not ready; defer capture */ return; }","typeGuard":null,"tryCatchPattern":"try { CaptureSceneViewViewportToProject(sceneView, ...); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Failed to resolve\"))\n{ EditorApplication.delayCall += () => RetryCapture(sceneView); }","preventionTips":["Allow the Scene View to finish initializing before capturing.","Defer capture to EditorApplication.delayCall if the viewport is not ready.","Avoid capturing during scene loads or layout transitions."],"tags":["screenshot","scene-view","viewport","initialization","editor"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}