|
import streamlit as st |
|
import trimesh |
|
from pythreejs import * |
|
import numpy as np |
|
|
|
|
|
st.title("Jewelry 3D Model Viewer") |
|
st.write("Upload an OBJ file to visualize the 3D model of jewelry.") |
|
|
|
|
|
uploaded_file = st.file_uploader("Choose an OBJ file", type="obj") |
|
|
|
if uploaded_file is not None: |
|
try: |
|
|
|
mesh = trimesh.load(uploaded_file, file_type='obj') |
|
|
|
|
|
if mesh.is_empty: |
|
st.error("The OBJ file does not contain valid geometry. Please upload a different file.") |
|
else: |
|
|
|
vertices = mesh.vertices |
|
faces = mesh.faces |
|
|
|
|
|
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) |
|
|
|
|
|
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) |
|
|
|
|
|
st.success("The OBJ file was successfully loaded and visualized.") |
|
|
|
except Exception as e: |
|
|
|
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.") |