File size: 1,992 Bytes
afe1c50
7d02d9d
 
 
afe1c50
 
 
 
 
 
 
 
 
7d02d9d
ef97656
 
7d02d9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afe1c50
 
170c731
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
49
50
51
52
53
54
import streamlit as st
import trimesh
from pythreejs import *
import numpy as np

# Streamlit title and description
st.title("Jewelry 3D Model Viewer")
st.write("Upload an OBJ file to visualize the 3D model of jewelry.")

# File uploader for the OBJ file
uploaded_file = st.file_uploader("Choose an OBJ file", type="obj")

if uploaded_file is not None:
    try:
        # Load the mesh using trimesh with explicit file_type
        mesh = trimesh.load(uploaded_file, file_type='obj')

        # Check if mesh is valid
        if mesh.is_empty:
            st.error("The OBJ file does not contain valid geometry. Please upload a different file.")
        else:
            # Get vertices and faces from the mesh
            vertices = mesh.vertices
            faces = mesh.faces

            # Create geometry and material for the 3D model
            geometry = BufferGeometry(
                attributes={
                    'position': BufferAttribute(vertices, normalized=False),
                    'index': BufferAttribute(faces.flatten(), normalized=False)
                }
            )

            material = MeshStandardMaterial(color='gold', roughness=0.5, metalness=0.8)
            model = Mesh(geometry, material)

            # Setup the scene
            camera = PerspectiveCamera(position=[3, 3, 3], fov=75)
            scene = Scene(children=[model, camera, AmbientLight(color='#777777')])
            renderer = Renderer(camera=camera, scene=scene, controls=[OrbitControls(controlling=camera)],
                                width=800, height=600)

            st.write("### 3D Model Preview")
            st.write(renderer)
            
            # Print success message
            st.success("The OBJ file was successfully loaded and visualized.")

    except Exception as e:
        # Print error message
        st.error(f"An error occurred while loading the OBJ file: {str(e)}")

else:
    st.write("Please upload an OBJ file to visualize the jewelry model.")