Skip to content

b3d

Smooth mesh using Blender's Laplacian smoothing.

PARAMETER DESCRIPTION
x
        Mesh object to simplify.

TYPE: MeshNeuron | Volume | Trimesh

iterations
        Round of smoothing to apply.

TYPE: int DEFAULT: 5

L
        Diffusion speed constant lambda. Larger = more aggressive
        smoothing.

TYPE: float [0-1] DEFAULT: 0.5

inplace
        If True, will perform simplication on `x`. If False, will
        simplify and return a copy.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
simp

Simplified mesh object.

Source code in navis/meshes/b3d.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
def smooth_mesh_blender(x, iterations=5, L=0.5, inplace=False):
    """Smooth mesh using Blender's Laplacian smoothing.

    Parameters
    ----------
    x :             MeshNeuron | Volume | Trimesh
                    Mesh object to simplify.
    iterations :    int
                    Round of smoothing to apply.
    L :             float [0-1]
                    Diffusion speed constant lambda. Larger = more aggressive
                    smoothing.
    inplace :       bool
                    If True, will perform simplication on `x`. If False, will
                    simplify and return a copy.

    Returns
    -------
    simp
                Simplified mesh object.

    """
    if not tm.interfaces.blender.exists:
        raise ModuleNotFoundError('No Blender 3D unavailable (executable not found).')
    _blender_executable = tm.interfaces.blender._blender_executable

    if L > 1 or L < 0:
        raise ValueError(f'`L` (lambda) must be between 0 and 1, got "{L}"')

    if isinstance(x, core.MeshNeuron):
        mesh = x.trimesh
    elif isinstance(x, core.Volume):
        mesh = tm.Trimesh(x.vertices, x.faces)
    elif isinstance(x, tm.Trimesh):
        mesh = x
    else:
        raise TypeError('Expected MeshNeuron, Volume or trimesh.Trimesh, '
                        f'got "{type(x)}"')

    assert isinstance(mesh, tm.Trimesh)

    # Load the template
    temp_name = 'blender_smooth.py.template'
    if temp_name in _cache:
        template = _cache[temp_name]
    else:
        with open(os.path.join(_pwd, 'templates', temp_name), 'r') as f:
            template = f.read()
        _cache[temp_name] = template

    # Replace placeholder with actual ratio
    script = template.replace('$ITERATIONS', str(iterations))
    script = script.replace('$LAMBDA', str(L))

    # Let trimesh's MeshScript take care of exectution and clean-up
    with tm.interfaces.generic.MeshScript(meshes=[mesh],
                                          script=script,
                                          debug=False) as blend:
        result = blend.run(_blender_executable
                           + ' --background --python $SCRIPT')

    # Blender apparently returns actively incorrect face normals
    result.face_normals = None

    if not inplace:
        x = x.copy()

    x.vertices = result.vertices
    x.faces = result.faces

    return x