Spaces:
Runtime error
Runtime error
Update app.py
Browse files3 unmodified lines4567891019 unmodified lines303132333435363738394041424344454613 unmodified lines6061626364653 unmodified linesfrom PIL import Imageimport tempfiledef apply_texture_with_uv(section):# Load the original meshmesh = trimesh.load('train.glb', force='mesh')im = Image.open('rust_steel.png').convert('RGB')19 unmodified lines# Select the indices for the specified sectionselected_indices = sections[section]# Check if the mesh has UV coordinatesif mesh.visual.kind == 'texture':original_uv = mesh.visual.uv # Use existing UV mappingelse:original_uv = np.random.rand(len(mesh.vertices), 2) # Random UV mapping# Prepare a new UV mapping that selectively applies the new texturenew_uv = np.copy(original_uv)new_uv[selected_indices, :] = np.random.rand(len(selected_indices), 2) # Generate new UV for the selected region# Create a new material with the texture imagematerial = trimesh.visual.texture.SimpleMaterial(image=im)new_visuals = trimesh.visual.TextureVisuals(uv=new_uv, material=material)13 unmodified linessection_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)app.launch()3 unmodified lines4567891019 unmodified lines303132333435363738394041424344454613 unmodified lines6061626364653 unmodified linesfrom PIL import Imageimport tempfiledef apply_texture_to_selected(section):# Load the original meshmesh = trimesh.load('train.glb', force='mesh')im = Image.open('rust_steel.png').convert('RGB')19 unmodified lines# Select the indices for the specified sectionselected_indices = sections[section]# Initialize the UV mapping with default or existing valuesif mesh.visual.kind == 'texture':original_uv = mesh.visual.uvelse:original_uv = np.random.rand(len(mesh.vertices), 2)# Generate new UV coordinates for the selected region onlynew_uv = np.copy(original_uv)new_uv[selected_indices, :] = np.random.rand(len(selected_indices), 2)# Create a new material using the texture imagematerial = trimesh.visual.texture.SimpleMaterial(image=im)new_visuals = trimesh.visual.TextureVisuals(uv=new_uv, material=material)13 unmodified linessection_dropdown = gr.Dropdown(choices=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section")button = gr.Button("Apply Texture to Section")button.click(apply_texture_to_selected, inputs=section_dropdown, outputs=modified_model)app.launch()
app.py+8-8