File size: 1,490 Bytes
4c713db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
import trimesh
import numpy as np
import os

def to_double_sided_gray_obj(mesh_path, out_path, gray=180):
    print(f"Processing: {mesh_path}")
    if not os.path.exists(mesh_path):
        print(f"File not found: {mesh_path}")
        return

    # Load with process=True to cleanup
    m = trimesh.load(mesh_path, process=True, force='mesh')
    
    # Handle Scene
    if isinstance(m, trimesh.Scene):
        if len(m.geometry) == 0:
             print("Empty scene")
             return
        m = trimesh.util.concatenate(tuple(m.geometry.values()))
    
    # 1. Fix normals on original
    m.fix_normals()
    
    # 2. Duplicate and flip for backfaces
    m_back = m.copy()
    m_back.faces = np.fliplr(m_back.faces)
    m_back.fix_normals()
    
    # 3. Combine
    m_combined = trimesh.util.concatenate([m, m_back])
    
    # 4. Color
    vc = np.tile(np.array([gray, gray, gray, 255], dtype=np.uint8), (len(m_combined.vertices), 1))
    m_combined.visual = trimesh.visual.ColorVisuals(mesh=m_combined, vertex_colors=vc)
    
    # 5. Export
    m_combined.export(out_path)
    print(f"Saved double-sided mesh to: {out_path}")
    return m_combined

# Test on the reported problematic mesh
mesh_path = "./ours_meshes/treehouse_rmapple.glb"
out_path = "./debug_treehouse_double_sided.glb"

# Run the fix
mesh = to_double_sided_gray_obj(mesh_path, out_path)

# If running in a notebook, this might show the viewer:
if mesh:
    try:
        mesh.show()
    except:
        pass