|
|
|
|
|
import tempfile |
|
import trimesh |
|
from trimesh.exchange.gltf import export_glb |
|
import numpy as np |
|
from PIL import Image |
|
|
|
def apply_gradient_color(mesh_text): |
|
""" |
|
Apply a gradient color to the mesh vertices and return as GLB. |
|
Args: |
|
mesh_text (str): The input mesh in OBJ format as a string. |
|
Returns: |
|
str: Path to the GLB file with gradient colors applied. |
|
""" |
|
|
|
temp_file = tempfile.NamedTemporaryFile(suffix=f"", delete=False).name |
|
with open(temp_file+".obj", "w") as f: |
|
f.write(mesh_text) |
|
mesh = trimesh.load_mesh(temp_file+".obj", file_type='obj') |
|
|
|
|
|
vertices = mesh.vertices |
|
y_values = vertices[:, 1] |
|
|
|
|
|
y_normalized = (y_values - y_values.min()) / (y_values.max() - y_values.min()) |
|
|
|
|
|
colors = np.zeros((len(vertices), 4)) |
|
colors[:, 0] = y_normalized |
|
colors[:, 2] = 1 - y_normalized |
|
colors[:, 3] = 1.0 |
|
|
|
|
|
mesh.visual.vertex_colors = colors |
|
|
|
|
|
glb_path = temp_file+".glb" |
|
with open(glb_path, "wb") as f: |
|
f.write(export_glb(mesh)) |
|
|
|
return glb_path |
|
|
|
def create_image_grid(images): |
|
images = [Image.fromarray((img * 255).astype("uint8")) for img in images] |
|
|
|
width, height = images[0].size |
|
grid_img = Image.new("RGB", (2 * width, 2 * height)) |
|
|
|
grid_img.paste(images[0], (0, 0)) |
|
grid_img.paste(images[1], (width, 0)) |
|
grid_img.paste(images[2], (0, height)) |
|
grid_img.paste(images[3], (width, height)) |
|
|
|
return grid_img |