# Get position of the first solid crossing limits line of a camera

I am working on a Python script for a scene of a movie project where no Depth of Field is used by the active camera (no dof_distance neither dof_object).

However there is a character crossing the limits line of the camera and I need the character position or at least, the location of the first point which cross the line (red point on my illustration). Could someone help me find a solution ?

I give you a simple illustration of my problem : • So you want the origin of the object, or the surface coordinates / nearest vertex that a ray casted from the center of the camera would hit? I'm asking because there could be other objects closer to the camera and also within the view frame, just not at the center, so the ray would not pick them up. – CoDEmanX Jun 26 '14 at 9:16
• Yes I understand. I need the location of the object's origin. – Grégoire Jun 26 '14 at 12:14
• Just get your object's location, that should be it. If you need the 2D distance in cam space, use the code I posted to your other question. – CoDEmanX Jun 26 '14 at 17:45

Ignoring other object's which might be located between the mesh and the ray origin you can use the object's ray_cast method:

If there are multiple mesh objects, you might simply iterate over the objects, picking the one yielding the closest intersection point.

import bpy
from mathutils import Vector

cam = bpy.data.objects['Camera']
group = bpy.data.groups['Group']

min = (cam.data.clip_end, None, None)
for obj in group.objects:
v0 = obj.matrix_world.inverted() * \
cam.matrix_world.translation

v1 = obj.matrix_world.inverted() * \
cam.matrix_world * \
Vector((0.0, 0.0, -cam.data.clip_end))

success, location, normal, index = obj.ray_cast(v0, v1)
if index != -1:
mag = (obj.matrix_world * (location - v0)).magnitude
if mag < min:
min = (mag, obj, location)

#set the cursor to the intersection point
if min:
bpy.context.scene.cursor_location = min.matrix_world * min


Without using python you can use drivers.

The driver has a Distance type built in already. You just input two objects and it will tell you the distance between them. On the empty (or any object in the scene) add a custom property and increase the max value so it can store a distance above 1.00. Then add the driver to the custom property; change the var type to Distance. Change the type to Sum Values. If you need it in a script then use bpy.data.objects["objectname"]["propertyname"]