My Blender file has only one object, a sort of quanset hut looking mesh sitting on and above the XY plane, with its local origin at the world origin, (0,0,0). It is centered over the Y axis and extends into positive and negative Y territory.
I'm trying to use ray_cast to measure the distance of points on my mesh from the Y axis, so I have written this test to start at (0,0,0), and shoot a ray up at a 45 degree angle (1,0,1), which should intersect with my mesh.
When I ran this program, I got a hit, but it was way too close to the origin to be my mesh, which has a diameter of about 2". The hit was at (0.0182, 0.0000, 0.0182), which is the right direction all right, but way too close. When I scaled my mesh, the hit distance did not change, which meant that it was not "hitting" my mesh.
So what IS it hitting? There is no other object.
I tried moving up the Y axis to (0,1,0), no hit. Then I tried some other starting positions:
origin at (0,0,0) produced a hit at (0.0182, 0.0000, 0.0182) origin at (0,.1,0) produced a hit at (0.0171, 0.1000, 0.0171) origin at (0,.2,0) produced a hit at (0.0161, 0.2000, 0.0161) origin at (0,.21,0) produced a hit at (0.0160, 0.2100, 0.0160) origin at (0,.22,0) produced a hit at (0.0159, 0.2200, 0.0159) origin at (0,.23,0) produced no hit
Could I be hitting the 3D cursor or something? That would be weird
Here's the code:
import bpy
from mathutils import Vector
ob = bpy.data.objects['Brep.010']
origin = Vector((0,.23,0))
direction = Vector((1,0,1))
hit, loc, norm, face = ob.ray_cast(origin, direction)
if hit:
print("Hit at ", loc, " (local)")
else:
print("No HIT")