import bpy
import bmesh
from mathutils import Vector
from mathutils.bvhtree import BVHTree
# Simulate objects in the scene
# Don't change any parameters
# ########### Start
if(len(bpy.data.objects)==0):
bpy.ops.mesh.primitive_plane_add(size=1, enter_editmode=False, location=(0, 2, 0.5))
bpy.ops.mesh.primitive_plane_add(size=1, enter_editmode=False, location=(0, -2, -0.5))
objs = [obj for obj in bpy.data.objects if obj.type == "MESH"]
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
objs[0].location[1] = -2
objs[1].location[1] = 2
# Simulate objects in the scene
# ########### End
# Set the starting position and direction of the ray
ray_begin = Vector((0, 0, 1))
ray_end = Vector((0, 0, -1))
ray_direction = ray_end - ray_begin
ray_direction.normalize()
# New bmesh
bm = bmesh.new()
# Add all objects
bm.from_mesh(objs[0].data)
bm.from_mesh(objs[1].data)
# mat0 = objs[0].matrix_world
# mat1 = objs[1].matrix_world
# # Try
# bm.transform(mat0)
# # Or
# bm.transform(mat1)
# # Or
# bm.transform(mat0 @ mat1)
# # Doesn't solve the problem
bvh = BVHTree.FromBMesh(bm)
# Objects may overlap, which is why I don’t use Object.ray_cast
p,n,i,d = bvh.ray_cast(ray_begin, ray_direction)
# Print: None None None None
print(p,n,i,d)
this doesnt work for objects that are rotated/scaled without this rotation and scale applied, What should I do?