Spaces:
Runtime error
Runtime error
Update app.py
Browse files3 unmodified lines45677891019 unmodified lines303132333334353536373738393940414142434344454613 unmodified lines606162636364653 unmodified linesfrom PIL import Imageimport tempfile
def apply_texture_with_uv(section):def apply_texture_to_selected(section): # Load the original mesh mesh = trimesh.load('train.glb', force='mesh') im = Image.open('rust_steel.png').convert('RGB')19 unmodified lines # Select the indices for the specified section selected_indices = sections[section]
# Check if the mesh has UV coordinates # Initialize the UV mapping with default or existing values if mesh.visual.kind == 'texture': original_uv = mesh.visual.uv # Use existing UV mapping original_uv = mesh.visual.uv else: original_uv = np.random.rand(len(mesh.vertices), 2) # Random UV mapping original_uv = np.random.rand(len(mesh.vertices), 2)
# Prepare a new UV mapping that selectively applies the new texture # Generate new UV coordinates for the selected region only new_uv = np.copy(original_uv) new_uv[selected_indices, :] = np.random.rand(len(selected_indices), 2) # Generate new UV for the selected region new_uv[selected_indices, :] = np.random.rand(len(selected_indices), 2)
# Create a new material with the texture image # Create a new material using the texture image material = trimesh.visual.texture.SimpleMaterial(image=im) new_visuals = trimesh.visual.TextureVisuals(uv=new_uv, material=material)
13 unmodified lines
section_dropdown = gr.Dropdown(choices=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section") button = gr.Button("Apply Texture to Section") button.click(apply_texture_with_uv, inputs=section_dropdown, outputs=modified_model) button.click(apply_texture_to_selected, inputs=section_dropdown, outputs=modified_model)
app.launch()app.py+8-8