Spaces:
Running
Running
Commit
·
31d88dd
1
Parent(s):
f1cf635
Fix indentation and exception pairing in create_3d_model
Browse files
app.py
CHANGED
|
@@ -123,24 +123,21 @@ def create_3d_model(buildings: List[Dict]) -> trimesh.Scene:
|
|
| 123 |
scene = trimesh.Scene()
|
| 124 |
|
| 125 |
for i, building in enumerate(buildings):
|
| 126 |
-
# only debug the first 5 to avoid log spam
|
| 127 |
debug = (i < 5)
|
| 128 |
-
|
| 129 |
footprint = building["footprint"]
|
| 130 |
height = building.get("height", 10)
|
| 131 |
if debug:
|
| 132 |
print(f"🏗️ Building #{i}: {len(footprint)} points, height={height}")
|
| 133 |
-
|
| 134 |
if height <= 0:
|
| 135 |
if debug: print(" ↳ skipping: non-positive height")
|
| 136 |
continue
|
| 137 |
-
|
| 138 |
-
#
|
| 139 |
try:
|
| 140 |
polygon = sg.Polygon(footprint)
|
| 141 |
if debug:
|
| 142 |
print(f" ↳ polygon valid={polygon.is_valid}, area={polygon.area:.2f}")
|
| 143 |
-
if not polygon.is_valid:
|
| 144 |
polygon = polygon.buffer(0)
|
| 145 |
if debug:
|
| 146 |
print(f" ↳ after buffer valid={polygon.is_valid}, area={polygon.area:.2f}")
|
|
@@ -148,18 +145,21 @@ def create_3d_model(buildings: List[Dict]) -> trimesh.Scene:
|
|
| 148 |
if debug: print(" ↳ skipping: invalid or zero-area polygon")
|
| 149 |
continue
|
| 150 |
except Exception as e:
|
| 151 |
-
if debug: print(f" ↳ skipping: polygon
|
| 152 |
continue
|
| 153 |
|
| 154 |
-
#
|
| 155 |
try:
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
if debug:
|
| 159 |
-
print(f" ↳
|
| 160 |
-
extruded = trimesh.creation.extrude_polygon(polygon, height, engine="earcut")
|
| 161 |
-
if debug:
|
| 162 |
-
print(f" ↳ extruded mesh vertices={len(extruded.vertices)}, faces={len(extruded.faces)}")
|
| 163 |
|
| 164 |
# orientation fix
|
| 165 |
transform_x = trimesh.transformations.rotation_matrix(np.pi/2, (1, 0, 0))
|
|
@@ -171,13 +171,14 @@ def create_3d_model(buildings: List[Dict]) -> trimesh.Scene:
|
|
| 171 |
if debug: print(" ↳ added to scene")
|
| 172 |
|
| 173 |
except Exception as e:
|
| 174 |
-
if debug: print(f" ↳ skipping: extrusion error: {e}")
|
| 175 |
continue
|
| 176 |
-
|
| 177 |
print(f"🔧 Final scene.geometry count: {len(scene.geometry)}")
|
| 178 |
return scene
|
| 179 |
|
| 180 |
|
|
|
|
| 181 |
def save_3d_model(scene: trimesh.Scene, filename: str) -> bool:
|
| 182 |
"""Export the 3D scene to a GLB file."""
|
| 183 |
try:
|
|
|
|
| 123 |
scene = trimesh.Scene()
|
| 124 |
|
| 125 |
for i, building in enumerate(buildings):
|
|
|
|
| 126 |
debug = (i < 5)
|
|
|
|
| 127 |
footprint = building["footprint"]
|
| 128 |
height = building.get("height", 10)
|
| 129 |
if debug:
|
| 130 |
print(f"🏗️ Building #{i}: {len(footprint)} points, height={height}")
|
|
|
|
| 131 |
if height <= 0:
|
| 132 |
if debug: print(" ↳ skipping: non-positive height")
|
| 133 |
continue
|
| 134 |
+
|
| 135 |
+
# build the polygon
|
| 136 |
try:
|
| 137 |
polygon = sg.Polygon(footprint)
|
| 138 |
if debug:
|
| 139 |
print(f" ↳ polygon valid={polygon.is_valid}, area={polygon.area:.2f}")
|
| 140 |
+
if not polygon.is_valid or polygon.area == 0:
|
| 141 |
polygon = polygon.buffer(0)
|
| 142 |
if debug:
|
| 143 |
print(f" ↳ after buffer valid={polygon.is_valid}, area={polygon.area:.2f}")
|
|
|
|
| 145 |
if debug: print(" ↳ skipping: invalid or zero-area polygon")
|
| 146 |
continue
|
| 147 |
except Exception as e:
|
| 148 |
+
if debug: print(f" ↳ skipping: polygon error: {e}")
|
| 149 |
continue
|
| 150 |
|
| 151 |
+
# **Extrude + orient + add all in one try/except**
|
| 152 |
try:
|
| 153 |
+
# attempt triangle, else fall back
|
| 154 |
+
try:
|
| 155 |
+
extruded = trimesh.creation.extrude_polygon(polygon, height, engine="triangle")
|
| 156 |
+
except (ImportError, ValueError) as e:
|
| 157 |
+
if debug:
|
| 158 |
+
print(f" ↳ triangle engine error ({e}), falling back to earcut")
|
| 159 |
+
extruded = trimesh.creation.extrude_polygon(polygon, height, engine="earcut")
|
| 160 |
+
|
| 161 |
if debug:
|
| 162 |
+
print(f" ↳ extruded mesh vertices={len(extruded.vertices)}, faces={len(extruded.faces)}")
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
# orientation fix
|
| 165 |
transform_x = trimesh.transformations.rotation_matrix(np.pi/2, (1, 0, 0))
|
|
|
|
| 171 |
if debug: print(" ↳ added to scene")
|
| 172 |
|
| 173 |
except Exception as e:
|
| 174 |
+
if debug: print(f" ↳ skipping: extrusion/orientation error: {e}")
|
| 175 |
continue
|
| 176 |
+
|
| 177 |
print(f"🔧 Final scene.geometry count: {len(scene.geometry)}")
|
| 178 |
return scene
|
| 179 |
|
| 180 |
|
| 181 |
+
|
| 182 |
def save_3d_model(scene: trimesh.Scene, filename: str) -> bool:
|
| 183 |
"""Export the 3D scene to a GLB file."""
|
| 184 |
try:
|