From bda66c9ec2c5e898c02365b6b6bbfbdbae3936aa Mon Sep 17 00:00:00 2001 From: Petru Conduraru Date: Thu, 9 Jul 2026 13:25:11 +0300 Subject: [PATCH] Bonsai: fix KeyError in format_distance for kilometre and mile units #8255 The project-unit to Blender-unit mapping in format_distance only knew FOOT/INCH/METRE/DECIMETRE/CENTIMETRE/MILLIMETRE, so creating a project with Kilometers or Miles in the New Project Wizard crashed with KeyError: 'KILOMETRE' (or 'MILE') as soon as the spatial tree formatted an elevation. Add the missing Blender-supported units (kilometre, mile, micrometre) and fall through gracefully for anything else (for example HECTOMETRE) so unknown units use the adaptive formatting branch instead of raising. Co-Authored-By: Claude Fable 5 (cherry picked from commit 980988f208dbb1b674bdee7d2a69279389b23935) --- src/bonsai/bonsai/bim/module/drawing/helper.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/bonsai/bonsai/bim/module/drawing/helper.py b/src/bonsai/bonsai/bim/module/drawing/helper.py index ef72207914..4c707b81a8 100644 --- a/src/bonsai/bonsai/bim/module/drawing/helper.py +++ b/src/bonsai/bonsai/bim/module/drawing/helper.py @@ -189,14 +189,20 @@ def format_distance( if hasattr(length_unit, "Prefix") and length_unit.Prefix: unit_length = length_unit.Prefix + length_unit.Name unit_length_mapping = { + "MILE": "MILES", "FOOT": "FEET", "INCH": "INCHES", + "KILOMETRE": "KILOMETERS", "METRE": "METERS", "DECIMETRE": "DECIMETERS", "CENTIMETRE": "CENTIMETERS", "MILLIMETRE": "MILLIMETERS", + "MICROMETRE": "MICROMETERS", } - unit_length = unit_length_mapping[unit_length] + # Fall through for units without a dedicated formatter (e.g. + # HECTOMETRE) so they use the adaptive branch instead of a + # KeyError (#8255). + unit_length = unit_length_mapping.get(unit_length, unit_length) # For now we only format area in IFC Units if area_unit := ifcopenshell.util.unit.get_project_unit(tool.Ifc.get(), "AREAUNIT"): area_unit_symbol = " " + ifcopenshell.util.unit.get_unit_symbol(area_unit)