Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import trimesh | |
| import tempfile | |
| import os | |
| def convert_to_3mf(input_path_str): | |
| """ | |
| Convert a mesh file (OBJ) to 3MF format. | |
| Parameters: | |
| input_path_str (str): Path to the input OBJ file. | |
| Returns: | |
| str: Path to the exported 3MF file. | |
| """ | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| input_path = input_path_str | |
| output_path = os.path.join(tmpdir, "output.3mf") | |
| # Load mesh | |
| mesh = trimesh.load(input_path, force='mesh') | |
| if mesh.is_empty: | |
| raise ValueError("Invalid or empty OBJ mesh.") | |
| # Export to 3MF | |
| mesh.export(output_path, file_type='3mf') | |
| if not os.path.exists(output_path): | |
| raise FileNotFoundError("Export failed: output.3mf not created.") | |
| # Copy to persistent temporary file to avoid auto-deletion | |
| persistent_tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".3mf") | |
| persistent_tmp.close() | |
| os.replace(output_path, persistent_tmp.name) | |
| return persistent_tmp.name | |
| # Gradio UI setup | |
| demo = gr.Interface( | |
| fn=convert_to_3mf, | |
| inputs=gr.File(file_types=[".obj"], type="filepath"), | |
| outputs=gr.File(file_types=[".3mf"]), | |
| title="OBJ to 3MF Converter" | |
| ) | |
| demo.launch() | |