File size: 1,243 Bytes
c79165d
 
08602a0
c79165d
 
 
 
 
32cc383
80a7dd4
 
 
08602a0
 
 
 
 
 
 
 
80a7dd4
08602a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c79165d
 
 
 
08602a0
c79165d
 
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
38
39
40
41
42
43
44
import gradio as gr
import trimesh
from PIL import Image
import base64
import io

def modify_texture():
    # Load the GLB file
    scene = trimesh.load_mesh("train.glb")
    geometries = list(scene.geometry.values())
    geometry = geometries[0]
    adjacency_matrix = geometry.edges_sparse
    
    # Open the texture image
    im = Image.open("defect.jpg")
    
    # Create a material with the texture image
    material = trimesh.visual.texture.SimpleMaterial(image=im)
    
    # Get the uv coordinates from the mesh
    uv = adjacency_matrix.visual.uv
    
    # Create TextureVisuals with the uv coordinates and the texture image
    color_visuals = trimesh.visual.TextureVisuals(uv=uv, image=im, material=material)
    
    # Apply the visuals to the mesh
    mesh.visual = color_visuals
    
    # Export the mesh to a .glb file
    mesh.export(file_obj='modified_train.glb')
    
    # Convert the GLB file to base64 for Gradio Model3D output
    with open('modified_train.glb', 'rb') as f:
        glb_base64 = base64.b64encode(f.read()).decode()
    
    return glb_base64

gr.Interface(
    fn=modify_texture,
    inputs=[],
    outputs=gr.Model3D(label="Download Modified GLB File"),
    title="GLB Texture Modifier"
).launch()