engrrifatullah's picture
Update backend.py
e3f28f7 verified
import trimesh
import numpy as np
from PIL import Image
import io
# Function to load a random object
def load_random_object():
# Replace with the path to your 3D model file
object_path = "example.obj" # Ensure this file exists in the project directory
return trimesh.load(object_path)
# Function to capture views
def capture_views(obj):
views = {}
# Define angles for each view
view_angles = {
'front': [0, 0, 0],
'top': [90, 0, 0],
'bottom': [-90, 0, 0],
'side': [0, 90, 0]
}
for view_name, angles in view_angles.items():
# Create a copy of the object to apply transformations
obj_copy = obj.copy()
# Apply rotation
rotation_matrix = trimesh.transformations.euler_matrix(*np.radians(angles))[:3, :3]
obj_copy.apply_transform(rotation_matrix)
# Render to an image (using off-screen rendering)
scene = obj_copy.scene()
png_data = scene.save_image(resolution=[600, 600])
# Convert PNG data to a PIL Image
views[view_name] = Image.open(io.BytesIO(png_data))
return views