If I had a complex object, and it's axises aligned. How can I get the x length or coordinates given a z coordinate?

For example, how can I get the x length of object's slice , given z=-1, y= -2?

For now, I use a thin plane to bool intersect the object, and get the bound box of the intersection result. But I think there should be a more efficient way to do this.

enter image description here


1 Answer 1


As @X Y suggested, this can be done with ray_cast. Coordinate x = 0 along with your y and z coordinates can be used to set the origin of the ray casts. You can then shoot one ray in the -x direction and another in the +x direction to get the intersection points p1 and p2, which are stored in index 1 of the two cast results. I also added a two tiny spheres to verify the intersections.

import bpy
from mathutils import Vector

someObj = bpy.data.objects['SomeObject']

y = -2
z = -1

origin = Vector((0, y, z))
direction = Vector((1, 0, 0))

# ray_cast parameters: (origin, direction, distance, depsgraph) --- excluding depsgraph
cast_result1 = someObj.ray_cast(origin, -direction, distance = 10)   
# -x intersection point: 
p1 = cast_result1[1]

cast_result2 = someObj.ray_cast(origin, direction, distance = 10)    
# +x intersection point:
p2 = cast_result2[1]

# distance between intersection points:
distance = p2.x - p1.x

bpy.ops.mesh.primitive_uv_sphere_add(radius = 0.1, location = p1)
bpy.ops.mesh.primitive_uv_sphere_add(radius = 0.1, location = p2)

enter image description here


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .