mirror of
https://github.com/blender/blender
synced 2026-11-22 03:45:13 +03:00
Currently the snapping system converts BMesh to Mesh for snapping, then does some filtering of the Mesh before building the necessary BVH trees. That's generally a good design that avoids a bunch of duplicate code, but the filtering step is a bit complex. A few global callbacks are stored that filter the Mesh data based on the equivalent indices in the BMesh, and those callbacks change the visibility attributes on the mesh, which indirectly control the created BVH tree. It's clearer to just calculate an IndexMask of desired snapping elements and create the BVH tree from that. That should be more efficient anyway, and fits much better with a future where edit mode doesn't imply that a BMesh is available (see #163678). Pull Request: https://projects.blender.org/blender/blender/pulls/164031
244 lines
8.4 KiB
Python
244 lines
8.4 KiB
Python
# SPDX-FileCopyrightText: 2026 Blender Authors
|
|
#
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
"""
|
|
This file does not run anything, its methods are accessed for tests by ``run_blender_setup.py``.
|
|
"""
|
|
import modules.ui_test_utils as ui
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Utilities
|
|
|
|
|
|
def _view3d_region(window):
|
|
area = ui.get_window_area_by_type(window, 'VIEW_3D')
|
|
return next(region for region in area.regions if region.type == 'WINDOW')
|
|
|
|
|
|
def _view3d_calc_screen_space_location(window, co_world):
|
|
"""Window coordinates of a world-space location, for positioning the cursor."""
|
|
from bpy_extras.view3d_utils import location_3d_to_region_2d
|
|
|
|
region = _view3d_region(window)
|
|
co = location_3d_to_region_2d(region, region.data, co_world)
|
|
return int(region.x + co[0]), int(region.y + co[1])
|
|
|
|
|
|
def _view3d_context_override(window):
|
|
import bpy
|
|
area = ui.get_window_area_by_type(window, 'VIEW_3D')
|
|
return bpy.context.temp_override(window=window, area=area, region=_view3d_region(window))
|
|
|
|
|
|
def _snapping_setup(e, window):
|
|
"""Empty scene holding a single plane, with vertex snapping to edited geometry enabled."""
|
|
import bpy
|
|
|
|
yield e.shift.f5() # 3D Viewport.
|
|
yield e.ctrl.alt.space() # Full-screen.
|
|
yield e.a() # Select all.
|
|
yield e.delete().ret() # Delete all.
|
|
|
|
with _view3d_context_override(window):
|
|
bpy.ops.mesh.primitive_plane_add(size=2.0)
|
|
|
|
tool_settings = bpy.context.scene.tool_settings
|
|
tool_settings.use_snap = True
|
|
tool_settings.snap_elements = {'VERTEX'}
|
|
tool_settings.snap_target = 'CLOSEST'
|
|
# Snapping to the mesh being edited is what exercises the edit-mode snap path.
|
|
tool_settings.use_snap_edit = True
|
|
yield
|
|
|
|
|
|
def _mesh_edit_verts(window):
|
|
import bmesh
|
|
ob = window.view_layer.objects.active
|
|
bm = bmesh.from_edit_mesh(ob.data)
|
|
bm.verts.ensure_lookup_table()
|
|
return ob, bm
|
|
|
|
|
|
def _mesh_edit_select_single_vert(window, index):
|
|
"""Leave only the vertex at \\a index selected, returning its world-space location."""
|
|
import bmesh
|
|
|
|
ob, bm = _mesh_edit_verts(window)
|
|
for vert in bm.verts:
|
|
vert.select = False
|
|
bm.verts[index].select = True
|
|
bm.select_flush(False)
|
|
bmesh.update_edit_mesh(ob.data)
|
|
return ob.matrix_world @ bm.verts[index].co
|
|
|
|
|
|
def _snap_vert_onto_location(e, window, vert_index, co_world_target):
|
|
"""Drag \\a vert_index with the mouse over \\a co_world_target, letting snapping take over."""
|
|
location_src = _mesh_edit_select_single_vert(window, vert_index)
|
|
yield
|
|
|
|
src_co = _view3d_calc_screen_space_location(window, location_src)
|
|
dst_co = _view3d_calc_screen_space_location(window, co_world_target)
|
|
|
|
e.cursor_position_set(*src_co, move=True)
|
|
yield
|
|
yield e.g()
|
|
# Move in steps. A single jump doesn't give the modal operator a chance to find a snap target,
|
|
# and the transform would then complete unsnapped.
|
|
steps = 8
|
|
for i in range(1, steps + 1):
|
|
factor = i / steps
|
|
e.cursor_position_set(
|
|
int(src_co[0] + (dst_co[0] - src_co[0]) * factor),
|
|
int(src_co[1] + (dst_co[1] - src_co[1]) * factor),
|
|
move=True,
|
|
)
|
|
yield
|
|
yield e.ret()
|
|
|
|
|
|
def _assert_snapped_onto_other_vert(t, window, vert_index):
|
|
"""
|
|
The moved vertex must sit exactly on top of another vertex of the same mesh.
|
|
|
|
Which one it picks depends on the path the cursor took, so this doesn't assume a particular
|
|
target. Without snapping the vertex moves in the view plane and lands on none of them.
|
|
"""
|
|
_ob, bm = _mesh_edit_verts(window)
|
|
moved = bm.verts[vert_index].co.copy()
|
|
others = [vert.co.copy() for i, vert in enumerate(bm.verts) if i != vert_index]
|
|
t.assertTrue(
|
|
any((moved - co).length < 1e-4 for co in others),
|
|
"vertex {:d} at {!r} snapped onto none of {!r}".format(
|
|
vert_index, moved[:], [co[:] for co in others],
|
|
),
|
|
)
|
|
return moved
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Tests
|
|
|
|
|
|
def view3d_mesh_edit_snap_to_self():
|
|
"""Snap a vertex onto another vertex of the same mesh being edited."""
|
|
from mathutils import Vector
|
|
e, t, window = ui.test_window()
|
|
|
|
yield from _snapping_setup(e, window)
|
|
yield e.tab() # Edit mode.
|
|
yield
|
|
|
|
# The plane's corners are at (+/-1, +/-1, 0), aim at the opposite one.
|
|
yield from _snap_vert_onto_location(e, window, 0, Vector((1.0, 1.0, 0.0)))
|
|
co = _assert_snapped_onto_other_vert(t, window, 0)
|
|
t.assertAlmostEqual(co.z, 0.0, places=4)
|
|
|
|
yield e.tab() # Object mode.
|
|
|
|
# Snapping builds its own copy of the mesh to find the elements that can be snapped to.
|
|
# None of that may reach the mesh being edited.
|
|
mesh = window.view_layer.objects.active.data
|
|
t.assertFalse(any(vert.hide for vert in mesh.vertices))
|
|
t.assertFalse(any(edge.hide for edge in mesh.edges))
|
|
t.assertFalse(any(face.hide for face in mesh.polygons))
|
|
|
|
|
|
def view3d_mesh_edit_snap_to_self_shape_key():
|
|
"""As above, but snapping must use the shape being edited rather than the basis."""
|
|
import bpy
|
|
from mathutils import Vector
|
|
e, t, window = ui.test_window()
|
|
|
|
yield from _snapping_setup(e, window)
|
|
|
|
ob = window.view_layer.objects.active
|
|
with _view3d_context_override(window):
|
|
bpy.ops.object.shape_key_add(from_mix=False) # Basis.
|
|
bpy.ops.object.shape_key_add(from_mix=False) # Key 1.
|
|
key_block = ob.data.shape_keys.key_blocks[1]
|
|
# Raise the active shape key values.
|
|
for point in key_block.data:
|
|
point.co = point.co + Vector((0.0, 0.0, 2.0))
|
|
key_block.value = 1.0
|
|
ob.active_shape_key_index = 1
|
|
yield
|
|
|
|
yield e.tab() # Edit mode.
|
|
yield
|
|
|
|
yield from _snap_vert_onto_location(e, window, 0, Vector((1.0, 1.0, 2.0)))
|
|
co = _assert_snapped_onto_other_vert(t, window, 0)
|
|
# The basis is at zero, so this is what tells the two shapes apart.
|
|
t.assertAlmostEqual(co.z, 2.0, places=4)
|
|
|
|
yield e.tab() # Object mode.
|
|
|
|
|
|
def view3d_mesh_edit_snap_skips_hidden():
|
|
"""Hidden geometry must never be a snap target, even when the cursor is right over it."""
|
|
import bmesh
|
|
from mathutils import Vector
|
|
e, t, window = ui.test_window()
|
|
|
|
yield from _snapping_setup(e, window)
|
|
yield e.tab() # Edit mode.
|
|
yield
|
|
|
|
# Hide everything but the vertex that is about to be moved, so the only thing the cursor
|
|
# passes over on its way is hidden and nothing is left to snap to.
|
|
ob, bm = _mesh_edit_verts(window)
|
|
for vert in bm.verts:
|
|
vert.hide_set(vert.index != 0)
|
|
bmesh.update_edit_mesh(ob.data)
|
|
yield
|
|
|
|
target = Vector((1.0, 1.0, 0.0))
|
|
yield from _snap_vert_onto_location(e, window, 0, target)
|
|
|
|
_ob, bm = _mesh_edit_verts(window)
|
|
moved = bm.verts[0].co.copy()
|
|
t.assertGreater((moved - target).length, 1e-4,
|
|
"vertex 0 snapped onto the hidden vertex at {!r}".format(target[:]))
|
|
|
|
yield e.tab() # Object mode.
|
|
|
|
|
|
def view3d_mesh_edit_snap_skips_selection():
|
|
"""
|
|
The selection is what's being transformed, so it must not be a snap target.
|
|
|
|
If it were a target, nudging a vertex a few pixels would snap it straight back onto
|
|
its own original location and the transform would do nothing at all.
|
|
"""
|
|
e, t, window = ui.test_window()
|
|
|
|
yield from _snapping_setup(e, window)
|
|
yield e.tab() # Edit mode.
|
|
yield
|
|
|
|
location_src = _mesh_edit_select_single_vert(window, 0)
|
|
_ob, bm = _mesh_edit_verts(window)
|
|
co_orig = bm.verts[0].co.copy()
|
|
yield
|
|
|
|
src_co = _view3d_calc_screen_space_location(window, location_src)
|
|
e.cursor_position_set(*src_co, move=True)
|
|
yield
|
|
yield e.g()
|
|
# Stay close, so the vertex's own original location is well within snapping range while the
|
|
# other three corners of the plane are far outside it.
|
|
for offset in range(1, 9):
|
|
e.cursor_position_set(src_co[0] + offset, src_co[1] + offset, move=True)
|
|
yield
|
|
yield e.ret()
|
|
|
|
_ob, bm = _mesh_edit_verts(window)
|
|
moved = bm.verts[0].co.copy()
|
|
t.assertGreater((moved - co_orig).length, 1e-4,
|
|
"vertex 0 snapped back onto its own original location {!r}".format(
|
|
co_orig[:]))
|
|
|
|
yield e.tab() # Object mode.
|