Bonsai: guard MEP Add Bend against parallel segments (#6393)

Adding a bend between two parallel (or collinear) MEP segments crashed with
`TypeError: 'NoneType' object is not subscriptable` in MEPAddBend._execute.
tool.Cad.intersect_edges returns None when the two segment axes have no
unique intersection (parallel/collinear/zero-length), and the code
immediately subscripted the result with [0]. The auto-dispatch "Add
Fitting" path already guards is_parallel before calling, but the direct
bim.mep_add_bend operator (the "Add Bend" button and the edit-preview flow)
did not.

Capture the intersect_edges result and, when it is None, report a
user-facing error ("The two segments are parallel, so a bend cannot be
computed") and return CANCELLED instead of subscripting None.

Verified live in headless Blender: two parallel duct segments reproduced
the exact TypeError before the fix and now cancel with the clear message;
a perpendicular pair still creates the bend (IfcDuctFitting + Bend type),
so no regression.

Generated with the assistance of an AI coding tool.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Petru Conduraru
2026-07-12 09:02:20 +03:00
parent 0b7e25a3ef
commit 11d57874cf
+13 -2
View File
@@ -1260,10 +1260,21 @@ class MEPAddBend(bpy.types.Operator, tool.Ifc.Operator):
}
get_z_basis = lambda o: tool.Cad.get_basis_vector(o, 2)
segments_intersection_ws = tool.Cad.intersect_edges(
segments_intersection = tool.Cad.intersect_edges(
(start_object.location, start_object.location + get_z_basis(start_object)),
(end_object.location, end_object.location + get_z_basis(end_object)),
)[0]
)
# intersect_edges returns None when the two segment axes are parallel or
# collinear (no unique intersection). Report a clean error instead of
# crashing with "'NoneType' object is not subscriptable".
if segments_intersection is None:
self.report(
{"ERROR"},
"The two segments are parallel, so a bend cannot be computed. "
"Select two non-parallel segments.",
)
return {"CANCELLED"}
segments_intersection_ws = segments_intersection[0]
start_point, first_segment_start = tool.Cad.closest_and_furthest_vectors(
segments_intersection_ws, (start_segment_data["start_point"], start_segment_data["end_point"])