project.operator - reuse ray_cast method

This commit is contained in:
Andrej730
2026-03-04 17:23:46 +05:00
parent a52a329197
commit adaf33b74f
2 changed files with 21 additions and 12 deletions
@@ -2307,7 +2307,9 @@ class QueryLinkedElement(bpy.types.Operator):
coord = (self.mouse_x, self.mouse_y)
origin = region_2d_to_origin_3d(region, rv3d, coord)
direction = region_2d_to_vector_3d(region, rv3d, coord)
hit, location, normal, face_index, obj, instance_matrix = self.ray_cast(context, origin, direction)
hit, location, normal, face_index, obj, instance_matrix = tool.Blender.ray_cast_scene(
context, origin, direction
)
if not hit:
self.report({"INFO"}, "No object found.")
return {"FINISHED"}
@@ -2399,11 +2401,6 @@ class QueryLinkedElement(bpy.types.Operator):
ProjectDecorator.install(bpy.context)
return {"FINISHED"}
def ray_cast(self, context: bpy.types.Context, origin: Vector, direction: Vector):
depsgraph = context.evaluated_depsgraph_get()
result = context.scene.ray_cast(depsgraph, origin, direction)
return result
def find_obj_root(self, obj: bpy.types.Object, matrix: Matrix) -> Union[bpy.types.Object, None]:
collections = set(obj.users_collection)
for o in bpy.data.objects:
@@ -2723,7 +2720,7 @@ class CreateClippingPlane(bpy.types.Operator):
coord = (self.mouse_x, self.mouse_y)
origin = region_2d_to_origin_3d(region, rv3d, coord)
direction = region_2d_to_vector_3d(region, rv3d, coord)
hit, location, normal, face_index, obj, matrix = self.ray_cast(context, origin, direction)
hit, location, normal, face_index, obj, matrix = tool.Blender.ray_cast_scene(context, origin, direction)
if not hit:
self.report({"INFO"}, "No object found.")
return {"FINISHED"}
@@ -2758,11 +2755,6 @@ class CreateClippingPlane(bpy.types.Operator):
bpy.ops.bim.refresh_clipping_planes("INVOKE_DEFAULT")
return {"FINISHED"}
def ray_cast(self, context, origin, direction):
depsgraph = context.evaluated_depsgraph_get()
result = context.scene.ray_cast(depsgraph, origin, direction)
return result
def invoke(self, context, event):
self.mouse_x = event.mouse_region_x
self.mouse_y = event.mouse_region_y
+17
View File
@@ -2184,3 +2184,20 @@ class Blender(bonsai.core.tool.Blender):
for f in files
if (Path(directory) / f.name).is_file()
]
@classmethod
def ray_cast_scene(
cls,
context: bpy.types.Context,
origin: Vector,
direction: Vector,
) -> tuple[bool, Vector, Vector, int, bpy.types.Object, Matrix]:
depsgraph = context.evaluated_depsgraph_get()
assert context.scene
# `matrix` is just `obj.matrix_world`.
result = context.scene.ray_cast(
depsgraph,
origin,
direction,
)
return result