I’m trying to teach myself the Blender API and thought it would be fun to try out the ray_cast method.
What I’m trying to do here, is for each polygon of the objects, cast a ray in the direction of the polygon normal to the other object and see if it ‘hits’ it.
Unfortunately my math sucks and I’m a noob to the API, so I’m not sure my script is working as expected!
I was expecting to see in the system console, some ‘False’ results, but all I see is True. I would have thought limiting the distance to 1 would have prevented any true results given the locations, so I think somethings gone badly wrong!
Any help is greatly appreciated…
Thanks
trigHelp.blend (670.5 KB)
import bpy
import numpy as np
def getDirection(loc1, loc2):
dx = loc1[0] - loc2[0]
dy = loc1[1] - loc2[1]
dz = loc1[2] - loc2[2]
distance = np.sqrt([dx * dx, dy * dy, dz * dz])
return [np.cos(dx), np.cos(dy),np.cos(dz)]
selection_names = bpy.context.selected_objects
for i in selection_names:
for poly in i.data.polygons:
for obj in selection_names:
if(i.name != obj.name):
direction = getDirection(poly.normal, obj.location)
origin = np.add(i.location,poly.normal)
print(“poly Object=”, i.name, “poly index=”, poly.index, “location object=”, obj.name, “calculated orgin =”, origin, “location=”, obj.location, “calculated direction=”, direction)
test = bpy.context.scene.ray_cast(
view_layer = bpy.context.view_layer,
origin = origin,
direction = direction,
distance=1)
print(test)