File size: 1,281 Bytes
3dd44f5
 
 
 
 
4f43ae1
3dd44f5
4f43ae1
3dd44f5
 
 
4f43ae1
3dd44f5
 
 
4f43ae1
3dd44f5
4f43ae1
3dd44f5
 
 
4f43ae1
3dd44f5
4f43ae1
 
3dd44f5
 
 
4f43ae1
3dd44f5
 
 
4f43ae1
 
 
 
 
 
 
 
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
import gradio as gr
from pygltflib import GLTF2, Texture, Image
import base64
import io

def modify_texture(new_texture):
    # Load the GLB file
    glb = GLTF2().load("train.glb")

    # Convert the new texture image to use in GLB
    img_byte_arr = io.BytesIO()
    new_texture.save(img_byte_arr, format='jpg')
    encoded_img = base64.b64encode(img_byte_arr.getvalue()).decode('ascii')

    # Assuming there's at least one image in the original GLB
    if glb.images:
        # Replace the first image with the new texture
        glb.images[0].uri = f"data:image/png;base64,{encoded_img}"
    else:
        # Add new image if none exists
        new_image = Image(uri=f"data:image/png;base64,{encoded_img}")
        glb.images.append(new_image)
        # Update texture to point to the new image
        new_texture = Texture(source=len(glb.images)-1)
        glb.textures.append(new_texture)

    # Save the modified GLB to a temporary file and return it
    output_file = io.BytesIO()
    glb.save(output_file)
    output_file.seek(0)
    return output_file

gr.Interface(
    fn=modify_texture,
    inputs=[
        gr.Image(label="Upload New Texture Image")
    ],
    outputs=gr.Model3D(label="Modified 3D Model", type="glb"),
    title="GLB Texture Modifier"
).launch()