3D_Model / app.py
dschandra's picture
Update app.py
170c731 verified
raw
history blame
1.99 kB
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.")