Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,26 +3,24 @@ from pygltflib import GLTF2, Texture, Image
|
|
3 |
import base64
|
4 |
import io
|
5 |
|
6 |
-
def modify_texture(
|
7 |
# Load the GLB file
|
8 |
glb = GLTF2().load("train.glb")
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
encoded_img = base64.b64encode(img_byte_arr.getvalue()).decode('ascii')
|
14 |
|
15 |
# Assuming there's at least one image in the original GLB
|
16 |
if glb.images:
|
17 |
-
# Replace the first image with the
|
18 |
-
glb.images[0]
|
19 |
else:
|
20 |
-
# Add
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
glb.textures.append(new_texture)
|
26 |
|
27 |
# Save the modified GLB to a temporary file and return it
|
28 |
output_file = io.BytesIO()
|
@@ -32,9 +30,7 @@ def modify_texture(new_texture):
|
|
32 |
|
33 |
gr.Interface(
|
34 |
fn=modify_texture,
|
35 |
-
inputs=[
|
36 |
-
gr.Image(label="Upload New Texture Image")
|
37 |
-
],
|
38 |
outputs=gr.Model3D(label="Modified 3D Model"),
|
39 |
title="GLB Texture Modifier"
|
40 |
).launch()
|
|
|
3 |
import base64
|
4 |
import io
|
5 |
|
6 |
+
def modify_texture():
|
7 |
# Load the GLB file
|
8 |
glb = GLTF2().load("train.glb")
|
9 |
|
10 |
+
# Load the default texture image
|
11 |
+
with open("defect.jpg", "rb") as f:
|
12 |
+
default_texture = Image(uri=f"data:image/jpeg;base64,{base64.b64encode(f.read()).decode()}")
|
|
|
13 |
|
14 |
# Assuming there's at least one image in the original GLB
|
15 |
if glb.images:
|
16 |
+
# Replace the first image with the default texture
|
17 |
+
glb.images[0] = default_texture
|
18 |
else:
|
19 |
+
# Add default image if none exists
|
20 |
+
glb.images.append(default_texture)
|
21 |
+
# Update texture to point to the default image
|
22 |
+
default_texture = Texture(source=len(glb.images)-1)
|
23 |
+
glb.textures.append(default_texture)
|
|
|
24 |
|
25 |
# Save the modified GLB to a temporary file and return it
|
26 |
output_file = io.BytesIO()
|
|
|
30 |
|
31 |
gr.Interface(
|
32 |
fn=modify_texture,
|
33 |
+
inputs=[],
|
|
|
|
|
34 |
outputs=gr.Model3D(label="Modified 3D Model"),
|
35 |
title="GLB Texture Modifier"
|
36 |
).launch()
|