Imrao commited on
Commit
bcadffb
·
1 Parent(s): f134ba3

generation fix pyversion

Browse files
Files changed (1) hide show
  1. app.py +37 -36
app.py CHANGED
@@ -144,49 +144,50 @@ async def generate_model(request: GenerateRequest):
144
  # OR we just pass them and let PyPRT handle if it's geographic.
145
  # Standard CityEngine works best with metric local coordinates.
146
 
147
- # Vertices list for PyPRT
148
- vertices = []
149
- for x, y in coords: # web mercator or lat/long
150
- vertices.extend([x, 0, y]) # Simple mapping to X, Y=0, Z
151
-
152
- indices = list(range(len(coords)))
153
- face_counts = [len(coords)]
154
-
155
- initial_shape = pyprt.InitialShape(vertices, indices, face_counts)
156
-
157
  # Setup Model Generator
158
  model_generator = pyprt.ModelGenerator([initial_shape])
159
 
160
- # Generate
 
 
 
161
  # Generate
162
  # PyPRT generate_model signature:
163
  # (self: ModelGenerator, shapeAttributes: list[dict], rulePackagePath: str, geometryEncoderName: str, geometryEncoderOptions: dict)
164
 
165
- with tempfile.NamedTemporaryFile(suffix=".glb", delete=False) as tmp:
166
- output_path = os.path.dirname(tmp.name)
167
- output_filename = os.path.basename(tmp.name)
168
-
169
- models = model_generator.generate_model(
170
- [request.attributes],
171
- rpk_path,
172
- "com.esri.prt.codecs.GLBEncoder",
173
- {
174
- 'outputPath': output_path,
175
- 'outputFilename': output_filename,
176
- 'emitReport': False
177
- }
178
- )
179
-
180
- # The file should now exist at tmp.name (or slightly different name depending on encoder)
181
- # GLB encoder often appends .glb if missing or uses exact name
182
- expected_file = os.path.join(output_path, output_filename + ".glb")
183
- if not os.path.exists(expected_file):
184
- expected_file = os.path.join(output_path, output_filename)
185
-
186
- if os.path.exists(expected_file):
187
- return FileResponse(expected_file, media_type="model/gltf-binary", filename="output.glb")
188
- else:
189
- raise HTTPException(status_code=500, detail="GLB file generation failed")
190
 
191
  except Exception as e:
192
  logger.error(f"Generation error: {e}")
 
144
  # OR we just pass them and let PyPRT handle if it's geographic.
145
  # Standard CityEngine works best with metric local coordinates.
146
 
147
+ # Flatten coordinates: [(x, y), ...] -> [x, 0, y, x, 0, y, ...]
148
+ # PyPRT expects a flattened list of coords.
149
+ flattened_coords = []
150
+ for x, y in coords:
151
+ flattened_coords.extend([x, 0, y])
152
+
153
+ initial_shape = pyprt.InitialShape(flattened_coords, indices, face_counts)
154
+
 
 
155
  # Setup Model Generator
156
  model_generator = pyprt.ModelGenerator([initial_shape])
157
 
158
+ # Determine output path - Create a unique temp directory
159
+ output_path = tempfile.mkdtemp()
160
+ output_filename = "model"
161
+
162
  # Generate
163
  # PyPRT generate_model signature:
164
  # (self: ModelGenerator, shapeAttributes: list[dict], rulePackagePath: str, geometryEncoderName: str, geometryEncoderOptions: dict)
165
 
166
+ models = model_generator.generate_model(
167
+ [request.attributes],
168
+ rpk_path,
169
+ "com.esri.prt.codecs.GLBEncoder",
170
+ {
171
+ 'outputPath': output_path,
172
+ 'outputFilename': output_filename,
173
+ 'emitReport': False
174
+ }
175
+ )
176
+
177
+ # Check for output
178
+ # GLB encoder usually creates {outputFilename}.glb or {outputFilename}_0.glb depending on rule
179
+ # We need to find *any* .glb file in that directory
180
+
181
+ generated_files = [f for f in os.listdir(output_path) if f.endswith(".glb")]
182
+
183
+ if generated_files:
184
+ file_path = os.path.join(output_path, generated_files[0])
185
+ # Note: We are returning the file but not cleaning up the temp dir immediately.
186
+ # In production, consider a background task or context manager to cleanup.
187
+ return FileResponse(file_path, media_type="model/gltf-binary", filename="output.glb")
188
+ else:
189
+ logger.error(f"No GLB file found in {output_path}")
190
+ raise HTTPException(status_code=500, detail="GLB generation failed: No file created")
191
 
192
  except Exception as e:
193
  logger.error(f"Generation error: {e}")