From c80c7c6d36995f780e8686701c008c8705a5ff3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Perdig=C3=A3o?= Date: Thu, 13 Mar 2025 16:46:09 -0300 Subject: [PATCH] Refactor - move `mouse_offset` from snap.py to raycast.py --- src/bonsai/bonsai/tool/raycast.py | 42 ++++++++++++++++++++----------- src/bonsai/bonsai/tool/snap.py | 21 +++------------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/bonsai/bonsai/tool/raycast.py b/src/bonsai/bonsai/tool/raycast.py index 99cae37461..4618096a47 100644 --- a/src/bonsai/bonsai/tool/raycast.py +++ b/src/bonsai/bonsai/tool/raycast.py @@ -28,6 +28,19 @@ from typing import Union class Raycast(bonsai.core.tool.Raycast): + offset = 10 + mouse_offset = ( + (-offset, offset), + (0, offset), + (offset, offset), + (-offset, 0), + (0, 0), + (offset, 0), + (-offset, -offset), + (0, -offset), + (offset, -offset), + ) + @classmethod def get_visible_objects(cls, context: bpy.types.Context): depsgraph = context.evaluated_depsgraph_get() @@ -70,19 +83,17 @@ class Raycast(bonsai.core.tool.Raycast): @classmethod def intersect_mouse_2d_bounding_box( - cls, mouse_pos: tuple[int, int], bbox: list[float, float, float, float], offset: int = None + cls, mouse_pos: tuple[int, int], bbox: list[float, float, float, float] ): - print("bbox", type(bbox)) - print(bbox) x, y = mouse_pos xmin, xmax, ymin, ymax = bbox # extends bbox boundaries to improve snap - if offset: - xmin -= offset - xmax += offset - ymin -= offset - ymax += offset + if cls.offset: + xmin -= cls.offset + xmax += cls.offset + ymin -= cls.offset + ymax += cls.offset if xmin < x < xmax and ymin < y < ymax: return True @@ -371,13 +382,12 @@ class Raycast(bonsai.core.tool.Raycast): context: bpy.types.Context, event: bpy.types.Event, objs_2d_bbox: Union[tuple[bpy.types.Object, list[float]]], - offset: int = None, ) -> list[bpy.types.Object]: mouse_pos = event.mouse_region_x, event.mouse_region_y objs_to_raycast = [] for obj, bbox_2d in objs_2d_bbox: if obj.type in {"MESH", "EMPTY", "CURVE"} and bbox_2d: - if tool.Raycast.intersect_mouse_2d_bounding_box(mouse_pos, bbox_2d, offset): + if tool.Raycast.intersect_mouse_2d_bounding_box(mouse_pos, bbox_2d): if ( obj.visible_in_viewport_get(bpy.context.space_data) or obj.library ): # Check for local view and local collections for this viewport and object @@ -390,7 +400,6 @@ class Raycast(bonsai.core.tool.Raycast): context: bpy.types.Context, event: bpy.types.Event, obj: bpy.types.Object, - mouse_offset: tuple[tuple[int, int]] = None, ) -> Union[tuple[bpy.types.Object, Vector, int], tuple[None, None, None]]: mouse_pos = event.mouse_region_x, event.mouse_region_y @@ -411,7 +420,7 @@ class Raycast(bonsai.core.tool.Raycast): if hit is None: # Tried original mouse position. Now it will try the offsets. original_mouse_pos = mouse_pos - for value in mouse_offset: + for value in cls.mouse_offset: mouse_pos = tuple(x + y for x, y in zip(original_mouse_pos, value)) hit, normal, face_index = tool.Raycast.obj_ray_cast(context, event, obj, mouse_pos) if hit: @@ -429,7 +438,7 @@ class Raycast(bonsai.core.tool.Raycast): context: bpy.types.Context, event: bpy.types.Event, objs_to_raycast: list[bpy.types.Object], - mouse_offset: tuple[tuple[int, int]] = None, + include_wireframes: bool=True, ) -> Union[tuple[bpy.types.Object, Vector, int], tuple[None, None, None]]: best_length_squared = 1.0 best_obj = None @@ -439,7 +448,12 @@ class Raycast(bonsai.core.tool.Raycast): ray_origin, ray_target, ray_direction = cls.get_viewport_ray_data(context, event) for obj in objs_to_raycast: - snap_obj, hit, face_index = cls.cast_rays_to_single_object(context, event, obj, mouse_offset) + if not include_wireframes and ( + obj.type in {"EMPTY", "CURVE"} or (hasattr(obj.data, "polygons") and len(obj.data.polygons) == 0) + ): + continue + + snap_obj, hit, face_index = cls.cast_rays_to_single_object(context, event, obj) if hit is not None: length_squared = (hit - ray_origin).length_squared diff --git a/src/bonsai/bonsai/tool/snap.py b/src/bonsai/bonsai/tool/snap.py index 4adf4f1fd3..20d551c121 100644 --- a/src/bonsai/bonsai/tool/snap.py +++ b/src/bonsai/bonsai/tool/snap.py @@ -273,23 +273,8 @@ class Snap(bonsai.core.tool.Snap): def detect_snapping_points(cls, context: bpy.types.Context, event: bpy.types.Event, objs_2d_bbox, tool_state): rv3d = context.region_data space = context.space_data - mouse_pos = event.mouse_region_x, event.mouse_region_y detected_snaps = [] - snap_threshold = 0.3 - offset = 10 - mouse_offset = ( - (-offset, offset), - (0, offset), - (offset, offset), - (-offset, 0), - (0, 0), - (offset, 0), - (-offset, -offset), - (0, -offset), - (offset, -offset), - ) - def select_plane_method(): if not last_polyline_point: plane_origin = Vector((0, 0, 0)) @@ -412,16 +397,16 @@ class Snap(bonsai.core.tool.Snap): detected_snaps.append(point) # Objects - objs_to_raycast = tool.Raycast.filter_objects_to_raycast(context, event, objs_2d_bbox, offset) + objs_to_raycast = tool.Raycast.filter_objects_to_raycast(context, event, objs_2d_bbox) if (space.shading.type == "SOLID" and space.shading.show_xray) or ( space.shading.type == "WIREFRAME" and space.shading.show_xray_wireframe ): results = [] for obj in objs_to_raycast: - results.append(tool.Raycast.cast_rays_to_single_object(context, event, obj, mouse_offset)) + results.append(tool.Raycast.cast_rays_to_single_object(context, event, obj)) else: results = [] - results.append(tool.Raycast.cast_rays_and_get_best_object(context, event, objs_to_raycast, mouse_offset)) + results.append(tool.Raycast.cast_rays_and_get_best_object(context, event, objs_to_raycast)) for result in results: snap_obj = result[0]