Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -4,19 +4,21 @@ import numpy as np | |
| 4 | 
             
            from PIL import Image
         | 
| 5 | 
             
            import tempfile
         | 
| 6 |  | 
| 7 | 
            -
            def  | 
| 8 | 
             
                # Load the original mesh
         | 
| 9 | 
             
                mesh = trimesh.load('train.glb', force='mesh')
         | 
|  | |
|  | |
| 10 | 
             
                im = Image.open('rust_steel.png').convert('RGB')
         | 
| 11 |  | 
| 12 | 
            -
                # Calculate  | 
| 13 | 
             
                bounds = mesh.bounds
         | 
| 14 | 
             
                min_bounds, max_bounds = bounds[0], bounds[1]
         | 
| 15 | 
             
                mid_x = (max_bounds[0] + min_bounds[0]) / 2
         | 
| 16 | 
             
                mid_y = (max_bounds[1] + min_bounds[1]) / 2
         | 
| 17 | 
             
                mid_z = (max_bounds[2] + min_bounds[2]) / 2
         | 
| 18 |  | 
| 19 | 
            -
                # Define sections | 
| 20 | 
             
                sections = {
         | 
| 21 | 
             
                    'upper': np.where(mesh.vertices[:, 2] > mid_z)[0],
         | 
| 22 | 
             
                    'lower': np.where(mesh.vertices[:, 2] <= mid_z)[0],
         | 
| @@ -30,23 +32,20 @@ def apply_section_texture(section): | |
| 30 | 
             
                # Get indices for the selected section
         | 
| 31 | 
             
                selected_indices = sections[section]
         | 
| 32 |  | 
| 33 | 
            -
                #  | 
| 34 | 
            -
                 | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
                mask = np.zeros(len(mesh.vertices), dtype=bool)
         | 
| 39 | 
            -
                mask[selected_indices] = True
         | 
| 40 | 
            -
                new_uv[~mask, :] = uv[~mask]  # Keep original UVs for non-selected regions
         | 
| 41 |  | 
| 42 | 
            -
                # Apply new  | 
| 43 | 
            -
                 | 
| 44 | 
            -
             | 
| 45 |  | 
| 46 | 
            -
                #  | 
| 47 | 
            -
                textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual= | 
| 48 |  | 
| 49 | 
            -
                # Save  | 
| 50 | 
             
                temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')
         | 
| 51 | 
             
                textured_mesh.export(temp_file.name, file_type='glb')
         | 
| 52 | 
             
                temp_file.close()
         | 
| @@ -59,6 +58,6 @@ with gr.Blocks() as app: | |
| 59 |  | 
| 60 | 
             
                section_dropdown = gr.Dropdown(choices=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section")
         | 
| 61 | 
             
                button = gr.Button("Apply Texture to Section")
         | 
| 62 | 
            -
                button.click( | 
| 63 |  | 
| 64 | 
             
            app.launch()
         | 
|  | |
| 4 | 
             
            from PIL import Image
         | 
| 5 | 
             
            import tempfile
         | 
| 6 |  | 
| 7 | 
            +
            def apply_masked_texture(section):
         | 
| 8 | 
             
                # Load the original mesh
         | 
| 9 | 
             
                mesh = trimesh.load('train.glb', force='mesh')
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                # Load the new texture image
         | 
| 12 | 
             
                im = Image.open('rust_steel.png').convert('RGB')
         | 
| 13 |  | 
| 14 | 
            +
                # Calculate bounding box coordinates
         | 
| 15 | 
             
                bounds = mesh.bounds
         | 
| 16 | 
             
                min_bounds, max_bounds = bounds[0], bounds[1]
         | 
| 17 | 
             
                mid_x = (max_bounds[0] + min_bounds[0]) / 2
         | 
| 18 | 
             
                mid_y = (max_bounds[1] + min_bounds[1]) / 2
         | 
| 19 | 
             
                mid_z = (max_bounds[2] + min_bounds[2]) / 2
         | 
| 20 |  | 
| 21 | 
            +
                # Define sections
         | 
| 22 | 
             
                sections = {
         | 
| 23 | 
             
                    'upper': np.where(mesh.vertices[:, 2] > mid_z)[0],
         | 
| 24 | 
             
                    'lower': np.where(mesh.vertices[:, 2] <= mid_z)[0],
         | 
|  | |
| 32 | 
             
                # Get indices for the selected section
         | 
| 33 | 
             
                selected_indices = sections[section]
         | 
| 34 |  | 
| 35 | 
            +
                # Copy the original visual properties (colors, UVs)
         | 
| 36 | 
            +
                if mesh.visual.vertex_colors is not None:
         | 
| 37 | 
            +
                    new_vertex_colors = np.copy(mesh.visual.vertex_colors)
         | 
| 38 | 
            +
                else:
         | 
| 39 | 
            +
                    new_vertex_colors = np.ones((len(mesh.vertices), 4)) * 255  # Assuming original is white or neutral
         | 
|  | |
|  | |
|  | |
| 40 |  | 
| 41 | 
            +
                # Apply the new texture to only the selected section
         | 
| 42 | 
            +
                for idx in selected_indices:
         | 
| 43 | 
            +
                    new_vertex_colors[idx, :] = [255, 255, 0, 255]  # Example yellow RGBA color
         | 
| 44 |  | 
| 45 | 
            +
                # Apply the new visual properties
         | 
| 46 | 
            +
                textured_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, visual=trimesh.visual.ColorVisuals(vertex_colors=new_vertex_colors), validate=True, process=False)
         | 
| 47 |  | 
| 48 | 
            +
                # Save to a temporary file for Gradio visualization
         | 
| 49 | 
             
                temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.glb')
         | 
| 50 | 
             
                textured_mesh.export(temp_file.name, file_type='glb')
         | 
| 51 | 
             
                temp_file.close()
         | 
|  | |
| 58 |  | 
| 59 | 
             
                section_dropdown = gr.Dropdown(choices=['upper', 'lower', 'middle', 'top', 'right'], label="Select Section")
         | 
| 60 | 
             
                button = gr.Button("Apply Texture to Section")
         | 
| 61 | 
            +
                button.click(apply_masked_texture, inputs=section_dropdown, outputs=modified_model)
         | 
| 62 |  | 
| 63 | 
             
            app.launch()
         |