File size: 1,277 Bytes
40db630
 
 
 
 
64d7f2b
 
 
 
 
 
 
 
 
 
40db630
410acfd
64d7f2b
40db630
64d7f2b
40db630
410acfd
 
 
 
64d7f2b
 
40db630
410acfd
64d7f2b
 
 
 
 
 
410acfd
64d7f2b
40db630
64d7f2b
40db630
64d7f2b
410acfd
64d7f2b
 
40db630
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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()