CoolFace
Apppublic

engrrifatullah/Engineering_Drawing_object_views_app

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
backend.py37 linesDownload Raw Back to root
1import trimesh2import numpy as np3from PIL import Image4import io5 6# Function to load a random object7def load_random_object():8    # Replace with the path to your 3D model file9    object_path = "example.obj"  # Ensure this file exists in the project directory10    return trimesh.load(object_path)11 12# Function to capture views13def capture_views(obj):14    views = {}15    # Define angles for each view16    view_angles = {17        'front': [0, 0, 0],18        'top': [90, 0, 0],19        'bottom': [-90, 0, 0],20        'side': [0, 90, 0]21    }22 23    for view_name, angles in view_angles.items():24        # Create a copy of the object to apply transformations25        obj_copy = obj.copy()26        # Apply rotation27        rotation_matrix = trimesh.transformations.euler_matrix(*np.radians(angles))[:3, :3]28        obj_copy.apply_transform(rotation_matrix)29 30        # Render to an image (using off-screen rendering)31        scene = obj_copy.scene()32        png_data = scene.save_image(resolution=[600, 600])33 34        # Convert PNG data to a PIL Image35        views[view_name] = Image.open(io.BytesIO(png_data))36    return views37