Files
test/tests/python/mesh_validate.py
Hans Goudey 6a829d78fa Mesh: Rewrite validation code
Rewrite the "mesh is valid" and "validate mesh" functions to be more
agnostic of the custom data storage system, align with the changes to
topology storage in the last 5 years, be much clearer overall, more
reusable.

Each check is implemented as a separate pass over the remaining
valid geometry in the mesh, producing an IndexMask of the invalid
elements it finds. At the cost of some extra iteration over mesh elements,
this should make each requirement clearer and make it easier to
optimize and reuse each check if needed.

The code is roughly twice as fast as it was before. I measured 92ms
instead of 200ms for a 1 million vertex cube on a Ryzen 7950X.
There's a bit of low hanging fruit for further optimization too.

There are now automated tests just for the validation code as well.
For now they are very basic but they could be extended in the future.

Some non-obvious points:
- The new face offsets storage (replacing `MPoly`) upholds more
  invariants by itself. Previously faces could easily overlap or leave
  corners unreferenced. That doesn't really happen anymore, but
  bad offset values are a more "global" problem.
- The validation code for the old "MFace" storage was removed. It is
  just rebuilt when it's needed at runtime anyway, so there isn't much
  point in validating it.
- The versioning code for 2.90.1 was calling the mesh validation code
  to fix an issue where the extrude manifold tool could generate bad faces.
  Unfortunately keeping that would mean being unable to remove the old
  code, so now there's a warning to open and save the file in a previous
  version instead.
- One of the main goals of the new code is better const correctness, and
  working better with implicit sharing. The code now only requests mutable
  copies of the mesh data if it has to change.

Part of #122398

Pull Request: https://projects.blender.org/blender/blender/pulls/148063
2025-10-16 19:55:24 +02:00

114 lines
3.4 KiB
Python

# SPDX-FileCopyrightText: 2025 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later
import unittest
import bpy
import sys
class TestMeshValidate(unittest.TestCase):
def setUp(self):
if bpy.context.object and bpy.context.object.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
for mesh in list(bpy.data.meshes):
bpy.data.meshes.remove(mesh)
def tearDown(self):
for mesh in list(bpy.data.meshes):
bpy.data.meshes.remove(mesh)
def test_invalid_edge_vertex_indices(self):
verts = [(0, 0, 0), (1, 1, 1)]
edges = [(0, 99)] # Invalid vertex 99
mesh = bpy.data.meshes.new("test_mesh")
mesh.from_pydata(verts, edges, [])
self.assertTrue(mesh.validate(verbose=True))
self.assertFalse(mesh.validate(verbose=True))
def test_duplicate_edge_vertex_indices(self):
verts = [(0, 0, 0), (1, 1, 1)]
edges = [(0, 0)] # Invalid edge from a vertex to itself
mesh = bpy.data.meshes.new("test_mesh")
mesh.from_pydata(verts, edges, [])
self.assertTrue(mesh.validate(verbose=True))
self.assertFalse(mesh.validate(verbose=True))
def test_bad_face_offsets(self):
bpy.ops.mesh.primitive_cube_add()
mesh = bpy.context.active_object.data
mesh.polygons[0].loop_start = 100
self.assertTrue(mesh.validate(verbose=True))
self.assertFalse(mesh.validate(verbose=True))
def test_bad_material_indices(self):
bpy.ops.mesh.primitive_plane_add()
obj = bpy.context.active_object
mesh = obj.data
attr = mesh.attributes.new(name="material_index", type='INT', domain='FACE')
attr.data[0].value = -4
self.assertTrue(mesh.validate(verbose=True))
self.assertFalse(mesh.validate(verbose=True))
def test_duplicate_faces(self):
verts = [(0, 0, 0), (1, 0, 0), (1, 1, 0)]
faces = [(0, 1, 2), (2, 0, 1)]
mesh = bpy.data.meshes.new("test_mesh")
mesh.from_pydata(verts, [], faces)
self.assertTrue(mesh.validate(verbose=True))
self.assertFalse(mesh.validate(verbose=True))
def test_invalid_float_attributes(self):
bpy.ops.mesh.primitive_plane_add()
mesh = bpy.context.active_object.data
mesh.vertices[0].co.x = float('nan')
self.assertTrue(mesh.validate(verbose=True))
self.assertFalse(mesh.validate(verbose=True))
def test_duplicate_edges(self):
verts = [(0, 0, 0), (1, 1, 1)]
edges = [(0, 1), (1, 0)]
mesh = bpy.data.meshes.new("test_mesh")
mesh.from_pydata(verts, edges, [])
self.assertTrue(mesh.validate(verbose=True))
self.assertFalse(mesh.validate(verbose=True))
def test_faces_with_bad_edge_references(self):
verts = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)]
edges = []
faces = [(0, 1, 2, 3)]
mesh = bpy.data.meshes.new("test_mesh")
mesh.from_pydata(verts, edges, faces)
corner_edges = mesh.attributes[".corner_edge"].data
corner_edges[2].value = 0
self.assertTrue(mesh.validate(verbose=True))
self.assertFalse(mesh.validate(verbose=True))
if __name__ == '__main__':
sys.argv = [__file__] + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [])
unittest.main()