Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,28 +3,45 @@ import trimesh
|
|
| 3 |
import tempfile
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 8 |
input_path = input_path_str
|
| 9 |
-
output_path = os.path.join(tmpdir, "output.
|
| 10 |
|
|
|
|
| 11 |
mesh = trimesh.load(input_path, force='mesh')
|
| 12 |
|
| 13 |
if mesh.is_empty:
|
| 14 |
raise ValueError("Invalid or empty OBJ mesh.")
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
if not os.path.exists(output_path):
|
| 19 |
-
raise FileNotFoundError("Export failed: output.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
return
|
| 22 |
|
|
|
|
| 23 |
demo = gr.Interface(
|
| 24 |
-
fn=
|
| 25 |
inputs=gr.File(file_types=[".obj"], type="filepath"),
|
| 26 |
-
outputs=gr.File(file_types=[".
|
| 27 |
-
title="OBJ to
|
| 28 |
)
|
| 29 |
|
| 30 |
demo.launch()
|
|
|
|
| 3 |
import tempfile
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
def convert_to_3mf(input_path_str):
|
| 7 |
+
"""
|
| 8 |
+
Convert a mesh file (OBJ) to 3MF format.
|
| 9 |
+
|
| 10 |
+
Parameters:
|
| 11 |
+
input_path_str (str): Path to the input OBJ file.
|
| 12 |
+
|
| 13 |
+
Returns:
|
| 14 |
+
str: Path to the exported 3MF file.
|
| 15 |
+
"""
|
| 16 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 17 |
input_path = input_path_str
|
| 18 |
+
output_path = os.path.join(tmpdir, "output.3mf")
|
| 19 |
|
| 20 |
+
# Load mesh
|
| 21 |
mesh = trimesh.load(input_path, force='mesh')
|
| 22 |
|
| 23 |
if mesh.is_empty:
|
| 24 |
raise ValueError("Invalid or empty OBJ mesh.")
|
| 25 |
|
| 26 |
+
# Export to 3MF
|
| 27 |
+
mesh.export(output_path, file_type='3mf')
|
| 28 |
|
| 29 |
if not os.path.exists(output_path):
|
| 30 |
+
raise FileNotFoundError("Export failed: output.3mf not created.")
|
| 31 |
+
|
| 32 |
+
# Copy to persistent temporary file to avoid auto-deletion
|
| 33 |
+
persistent_tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".3mf")
|
| 34 |
+
persistent_tmp.close()
|
| 35 |
+
os.replace(output_path, persistent_tmp.name)
|
| 36 |
|
| 37 |
+
return persistent_tmp.name
|
| 38 |
|
| 39 |
+
# Gradio UI setup
|
| 40 |
demo = gr.Interface(
|
| 41 |
+
fn=convert_to_3mf,
|
| 42 |
inputs=gr.File(file_types=[".obj"], type="filepath"),
|
| 43 |
+
outputs=gr.File(file_types=[".3mf"]),
|
| 44 |
+
title="OBJ to 3MF Converter"
|
| 45 |
)
|
| 46 |
|
| 47 |
demo.launch()
|