Skip to content

Commit

Permalink
Tool now works in local view, and with orthographic camera
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienHeijmans committed Oct 23, 2022
1 parent 621ea17 commit f5bcbe8
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions quicksnap.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ def initialize(self, context):
if not self.object_mode and not quicksnap_utils.has_points_selected(context, self.selection_objects):
return False

# Hide objects to ignore if we are in local view.
if context.space_data.local_view is not None:
all_scene_objects = [obj for obj in context.scene.objects if not obj.hide_get()]
ignored_objs = set([obj for obj in all_scene_objects if obj not in context.visible_objects])
self.ignored_obj_names = set([obj.name for obj in ignored_objs])
for obj in ignored_objs:
obj.hide_set(True)

# Create SnapData objects that will store all the vertex/point info (World space, view space, and kdtree to
# search the closest point)
self.snapdata_source = SnapData(context, region, self.settings, self.selection_objects)
Expand Down Expand Up @@ -200,8 +208,10 @@ def update(self, context, region):
# Update 3DView camera information
region3d = context.space_data.region_3d
self.camera_position = region3d.view_matrix.inverted().translation
self.mouse_vector = view3d_utils.region_2d_to_vector_3d(region, context.space_data.region_3d,
self.mouse_position_world = view3d_utils.region_2d_to_origin_3d(region, region.data, self.mouse_position)
self.mouse_vector = view3d_utils.region_2d_to_vector_3d(region, region.data,
self.mouse_position)

mouse_coord_screen_flat = Vector((self.mouse_position[0], self.mouse_position[1], 0))

depsgraph = context.evaluated_depsgraph_get()
Expand All @@ -210,7 +220,7 @@ def update(self, context, region):
# Find object under the mouse
(direct_hit, _, _, self.target_face_index, direct_hit_object, _) = context.scene.ray_cast(
context.evaluated_depsgraph_get(),
origin=self.camera_position,
origin=self.mouse_position_world,
direction=self.mouse_vector)
# If found, we push this object on top of the stack of objects to process
if direct_hit and direct_hit_object.name in self.selection_objects:
Expand Down Expand Up @@ -278,9 +288,8 @@ def update(self, context, region):
set_first_priority=True)

# Look for object under the mouse, if found, bring it in top of the list of objects to process.
(direct_hit, _, _, self.target_face_index, direct_hit_object, _) = context.scene.ray_cast(depsgraph,
origin=self.camera_position,
direction=self.mouse_vector)
(direct_hit, _, _, self.target_face_index, direct_hit_object, _) = \
context.scene.ray_cast(depsgraph, origin=self.mouse_position_world, direction=self.mouse_vector)
if direct_hit:
hover_object = direct_hit_object.name

Expand Down Expand Up @@ -354,10 +363,10 @@ def apply(self, context, region, use_auto_merge=False):
else:
# The 3D location in this direction
if len(self.snapping) == 0 or not self.snapping_local:
self.target = quicksnap_utils.get_target_free(origin, self.camera_position, self.mouse_vector,
self.target = quicksnap_utils.get_target_free(origin, self.mouse_position_world, self.mouse_vector,
self.snapping)
else:
self.target = quicksnap_utils.get_target_free(origin, self.camera_position, self.mouse_vector,
self.target = quicksnap_utils.get_target_free(origin, self.mouse_position_world, self.mouse_vector,
self.snapping,
bpy.data.objects[self.selection_objects[0]])

Expand All @@ -374,6 +383,8 @@ def apply(self, context, region, use_auto_merge=False):
context.space_data.region_3d)

def __init__(self):
self.mouse_position_world = None
self.ignored_obj_names = set()
self.clicktime = 0
self.last_event = None
self.clickdrag = None
Expand Down Expand Up @@ -615,6 +626,9 @@ def terminate(self, context, revert=False):
if revert:
self.revert_data(context, apply=True)

if context.space_data.local_view is not None:
for obj_name in self.ignored_obj_names:
bpy.data.objects[obj_name].hide_set(False)
self.set_object_display("", "")
context.area.header_text_set(None)
if self.object_mode:
Expand Down

0 comments on commit f5bcbe8

Please sign in to comment.