File size: 1,126 Bytes
279a4bd e3f28f7 279a4bd | 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 | 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
|