Mouse position in 3D

Hi,

I’m trying to get the mouse position in 3D space (Z=0.0) using the following code


        ray_vec = view3d_utils.region_2d_to_vector_3d(region, rv3d, coord)
        ray_loc = view3d_utils.region_2d_to_location_3d(region, rv3d, coord, ray_vec)

but it only works when I’m viewing the scene from top.

What I’m doing wrong?
Thanks!

Finally I’ve found how to do it.


from mathutils import Vector, geometry
from bpy_extras import view3d_utils
...
        region = context.region
        rv3d = context.region_data
        coord = event.mouse_region_x, event.mouse_region_y
        # get ray
        ray_org = view3d_utils.region_2d_to_origin_3d(region, rv3d, coord)
        ray_vec = view3d_utils.region_2d_to_vector_3d(region, rv3d, coord)
        # compute intersection with plane where Z = 0.0
        pos = 0.0
        v1 = Vector((0.0, 0.0, pos))
        v2 = Vector((1.0, 0.0, pos))
        v3 = Vector((0.0, 1.0, pos))
        pos = geometry.intersect_ray_tri(v1, v2, v3, ray_vec, ray_org, False)
        # pos will have the intersection point or None if there is no intersection