Add single object raycast method

This commit is contained in:
pullusb
2024-02-06 15:57:35 +01:00
parent 5590753550
commit b8180ea84f
5 changed files with 141 additions and 62 deletions
+50 -8
View File
@@ -68,15 +68,8 @@ def search_square(point, factor=0.05, cam=None):
return matrix_transform(plane, mat @ mat_scale)
def ray_cast_point(point, origin, depsgraph):
ray = (point - origin)#.normalized()
hit, hit_location, normal, face_index, object_hit, matrix = bpy.context.scene.ray_cast(depsgraph, origin, ray)
if not hit:
return None, None, None, None
def get_tri_from_face(hit_location, face_index, object_hit, depsgraph):
eval_ob = object_hit.evaluated_get(depsgraph)
face = eval_ob.data.polygons[face_index]
vertices = [eval_ob.data.vertices[i] for i in face.vertices]
face_co = matrix_transform([v.co for v in vertices], eval_ob.matrix_world)
@@ -88,8 +81,37 @@ def ray_cast_point(point, origin, depsgraph):
if intersect_point_tri(hit_location, *tri):
break
return tri, tri_indices
def ray_cast_point(point, origin, depsgraph):
ray = (point - origin)
hit, hit_location, normal, face_index, object_hit, matrix = bpy.context.scene.ray_cast(depsgraph, origin, ray)
if not hit:
return None, None, None, None
tri, tri_indices = get_tri_from_face(hit_location, face_index, object_hit, depsgraph)
return object_hit, np.array(hit_location), tri, tri_indices
def obj_ray_cast(obj, point, origin, depsgraph):
"""Wrapper for ray casting that moves the ray into object space"""
# get the ray relative to the object
matrix_inv = obj.matrix_world.inverted()
ray_origin_obj = matrix_inv @ origin # matrix_transform(origin, matrix_inv)
ray_target_obj = matrix_inv @ point # matrix_transform(point, matrix_inv)
ray_direction_obj = ray_target_obj - ray_origin_obj
# cast the ray
success, location, normal, face_index = obj.ray_cast(ray_origin_obj, ray_direction_obj, depsgraph=depsgraph)
if not success:
return None, None, None, None
# Get hit location world_space
hit_location = obj.matrix_world @ location
tri, tri_indices = get_tri_from_face(hit_location, face_index, obj, depsgraph)
return obj, np.array(hit_location), tri, tri_indices
def empty_at(name='Empty', pos=(0,0,0), collection=None, type='PLAIN_AXES', size=1, show_name=False):
'''
@@ -425,6 +447,26 @@ def following_keys(forward=True, all_keys=False) -> list:# -> list[int] | list |
return [int(new)]
def index_list_from_bools(bool_list) -> list:
'''Receive a list of boolean
Return a list of sublists of indices where there is a continuity of True.
e.g., [True, True, False, True] will return [[0,1][3]]
'''
result = []
current_sublist = []
for i, value in enumerate(bool_list):
if value:
current_sublist.append(i)
elif current_sublist:
result.append(current_sublist)
current_sublist = []
if current_sublist:
result.append(current_sublist)
return result
## -- animation
def is_animated(obj):