Title: Snap crash: TypeError 'cannot unpack non-iterable NoneType' in ray_cast_by_proximity_2d when edges fall behind the camera plane · Issue #9286 · IfcOpenShell/IfcOpenShell · GitHub
Open Graph Title: Snap crash: TypeError 'cannot unpack non-iterable NoneType' in ray_cast_by_proximity_2d when edges fall behind the camera plane · Issue #9286 · IfcOpenShell/IfcOpenShell
X Title: Snap crash: TypeError 'cannot unpack non-iterable NoneType' in ray_cast_by_proximity_2d when edges fall behind the camera plane · Issue #9286 · IfcOpenShell/IfcOpenShell
Description: Bug Description Placing a window (or any product placed with the polyline/draw-occurrence tool) intermittently fails with: File "bonsai\tool\raycast.py", line 442, in ray_cast_by_proximity_2d p0x, p0y = p0 TypeError: cannot unpack non-it...
Open Graph Description: Bug Description Placing a window (or any product placed with the polyline/draw-occurrence tool) intermittently fails with: File "bonsai\tool\raycast.py", line 442, in ray_cast_by_proximity_2d p0x, ...
X Description: Bug Description Placing a window (or any product placed with the polyline/draw-occurrence tool) intermittently fails with: File "bonsai\tool\raycast.py", line 442, in ray_cast_by_proximit...
Opengraph URL: https://github.com/IfcOpenShell/IfcOpenShell/issues/9286
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Snap crash: TypeError 'cannot unpack non-iterable NoneType' in ray_cast_by_proximity_2d when edges fall behind the camera plane","articleBody":"### Bug Description\n\nPlacing a window (or any product placed with the polyline/draw-occurrence tool) intermittently fails with:\n\n```\nFile \"bonsai\\tool\\raycast.py\", line 442, in ray_cast_by_proximity_2d\n p0x, p0y = p0\nTypeError: cannot unpack non-iterable NoneType object\n```\n\nIt is not tied to a particular IFC file — it depends on where the camera sits, which is why it only happens sometimes. It occurs in a normal perspective 3D view (not camera view).\n\n**To reproduce**\n\n1. Open a model containing wireframe snap objects (meshes with no faces, curves — grid axes, annotations, axis lines).\n2. Put the viewport in perspective and move the camera so that it sits *right next to / inside* such an object, i.e. part of the object is behind the camera plane.\n3. Start `Add Occurrence` (e.g. a window) and move the mouse over the wall.\n4. The modal operator aborts with the traceback above.\n\nFull traceback:\n\n```\nFile \"bonsai\\bim\\module\\model\\workspace.py\", line 1192, in hotkey_S_A\n return bpy.ops.bim.draw_occurrence(\"INVOKE_DEFAULT\")\nFile \"bonsai\\bim\\module\\model\\polyline.py\", line 470, in invoke\n detected_snaps = tool.Snap.detect_snapping_points(context, event, self.objs_2d_bbox, self.tool_state)\nFile \"bonsai\\tool\\snap.py\", line 394, in detect_snapping_points\n closest_snaps = tool.Raycast.ray_cast_and_get_closest_to_camera_snaps(context, event, objs_to_raycast)\nFile \"bonsai\\tool\\raycast.py\", line 884, in ray_cast_and_get_closest_to_camera_snaps\n hit_obj, hit = cls.process_wireframe_snap_obj(context, event, snap_obj, ray_origin, closest_snaps)\nFile \"bonsai\\tool\\raycast.py\", line 829, in process_wireframe_snap_obj\n snap_points = tool.Raycast.ray_cast_by_proximity_2d(context, event, snap_obj)\nFile \"bonsai\\tool\\raycast.py\", line 442, in ray_cast_by_proximity_2d\n p0x, p0y = p0\nTypeError: cannot unpack non-iterable NoneType object\n```\n\n### Root cause\n\n`Raycast.ray_cast_by_proximity_2d` builds `edge_verts[e] = (p0, p1)` with the 2D screen positions of every edge returned by the BVH, then unpacks them unconditionally at line 442. `view3d_utils.location_3d_to_region_2d` returns `None` for any point behind the camera plane, and **two** paths store a `None` in that map:\n\nhttps://github.com/IfcOpenShell/IfcOpenShell/blob/v0.8.0/src/bonsai/bonsai/tool/raycast.py#L401-L415\n\n1. **Both endpoints behind the camera plane.** `(v1_2d is None) ^ (v2_2d is None)` is `False`, so the `else` branch stores `(None, None)`.\n2. **One endpoint behind the camera plane, the other in front of it but nearer than `clip_start`.** The xor branch is taken, but the segment never crosses the near plane, so `intersect_edge_region_border()` returns `None` (printing *\"No intersection with viewport near plane found for the segment.\"* to the console) and `(None, v2_2d)` is stored.\n\nEither way line 442 unpacks `None`.\n\nWhy an edge that is behind the camera reaches this code at all: the BVH boxes are axis-aligned in **world** space, while the camera plane generally is not. A leaf box can therefore straddle the camera plane, be hit by the mouse ray (`t >= 0`), and hand over edges that lie entirely behind the viewer. This is what makes the crash camera-position dependent, hence intermittent.\n\nNote that the other caller of the same helper already guards against the `None` return:\n\nhttps://github.com/IfcOpenShell/IfcOpenShell/blob/v0.8.0/src/bonsai/bonsai/tool/raycast.py#L114-L120\n\nThis call site never did. The bug predates the recent BVH/2D-projection optimisations — those did not touch this logic.\n\n### Reproduction (headless)\n\nI reproduced it outside the UI with a fake viewport (perspective, camera looking along the world `+X+Y` diagonal so the camera plane is not world-aligned) and a 4-vertex / 2-edge wireframe object with one edge sitting just behind the camera plane, calling `ray_cast_by_proximity_2d` directly. Against pristine `v0.8.0`:\n\n```\n=== CASE_A_both_endpoints_behind ===\n edge 1 endpoints behind camera plane: True, True\n BVH leaf boxes hit by the ray: 63, edges collected: [0, 1]\n raycast.py:442 TypeError: cannot unpack non-iterable NoneType object\n\n=== CASE_B_near_plane_not_crossed ===\n edge 1 endpoints behind camera plane: True, False\n No intersection with viewport near plane found for the segment.\n raycast.py:442 TypeError: cannot unpack non-iterable NoneType object\n\n2 of 2 cases crashed\n```\n\n### Suggested fix\n\nSkip edges with no usable on-screen projection, and iterate `edge_verts` instead of `edges` (some edges are now absent from it). This matches what the vertex loop just above already does — a vertex that does not project is skipped — and an edge that is not on screen has no 2D proximity snap to offer anyway.\n\n```diff\n v1_2d = verts_2d.get(verts_idx[0])\n v2_2d = verts_2d.get(verts_idx[1])\n+ if v1_2d is None and v2_2d is None:\n+ # Edge is entirely behind the camera plane, there is nothing on screen to snap to.\n+ continue\n if (v1_2d is None) ^ (v2_2d is None):\n point, _ = cls.intersect_edge_region_border(region, context.space_data, rv3d, v1, v2)\n+ if point is None:\n+ # No point of the edge could be mapped to the region border.\n+ continue\n if v1_2d is None:\n edge_verts[e] = (point, v2_2d)\n else:\n@@\n count = 0\n selected_edges = {}\n- for e in edges:\n- p0, p1 = edge_verts[e]\n+ for e, (p0, p1) in edge_verts.items():\n p0x, p0y = p0\n```\n\nWith this applied, both cases above return snap points normally instead of crashing (`0 of 2 cases crashed`). I can open a PR if the approach looks right.\n\n### Unrelated latent bug spotted nearby\n\n`intersect_edge_region_border` picks the on-screen endpoint with a truthiness test:\n\nhttps://github.com/IfcOpenShell/IfcOpenShell/blob/v0.8.0/src/bonsai/bonsai/tool/raycast.py#L190\n\nA vertex projecting exactly to `(0, 0)` gives a falsy `Vector`, so the wrong endpoint is chosen. Should be `is not None`. Not the cause of this crash — mentioning it for completeness.\n\n### Debug and Error Output\n\nBonsai 0.8.6-alpha260720 (source at v0.8.0, 8dc1ee55b)\nBlender 5.1.0\nWindows 11, Python 3.13\n","author":{"url":"https://github.com/carlopav","@type":"Person","name":"carlopav"},"datePublished":"2026-08-11T19:05:17Z","url":"https://github.com/IfcOpenShell/IfcOpenShell/issues/9286"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:09d95008-609a-67a9-1c58-751604d274b7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EC98:64377:1C74CD:261403:6B04B402 |
| html-safe-nonce | 5fffe67ee70c4fc2fe02373974729c37c9cc3ad89b71bd4c881cd27c616ca23f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQzk4OjY0Mzc3OjFDNzRDRDoyNjE0MDM6NkIwNEI0MDIiLCJ2aXNpdG9yX2lkIjoiMjI2ODc2NDgwNzQ1MDg5OTQ1OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5b5e470b112a96ede62edc88f6d04d8a42bf1ee5576fc1adc94a2c8dcbe844d1 |
| hovercard-subject-tag | issue:5123643306 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/IfcOpenShell/IfcOpenShell/9286/issue_layout |
| twitter:image | https://opengraph.githubassets.com/926e083ec4fec96a7bf15941f6a2420420b2c056390391dd56851af04fb10a31/IfcOpenShell/IfcOpenShell/issues/9286 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/926e083ec4fec96a7bf15941f6a2420420b2c056390391dd56851af04fb10a31/IfcOpenShell/IfcOpenShell/issues/9286 |
| og:image:alt | Bug Description Placing a window (or any product placed with the polyline/draw-occurrence tool) intermittently fails with: File "bonsai\tool\raycast.py", line 442, in ray_cast_by_proximity_2d p0x, ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | carlopav |
| hostname | github.com |
| expected-hostname | github.com |
| None | 378bd702ac792b3550e484ad39d9a898fcd25bb52ba6962b0c9cc457c1ea71c7 |
| turbo-cache-control | no-preview |
| go-import | github.com/IfcOpenShell/IfcOpenShell git https://github.com/IfcOpenShell/IfcOpenShell.git |
| octolytics-dimension-user_id | 13692563 |
| octolytics-dimension-user_login | IfcOpenShell |
| octolytics-dimension-repository_id | 40473201 |
| octolytics-dimension-repository_nwo | IfcOpenShell/IfcOpenShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 40473201 |
| octolytics-dimension-repository_network_root_nwo | IfcOpenShell/IfcOpenShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | b032e77f1ea437ede6c115446a0a467709b468e7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width