I want to be able to emulate a user clicking on a point on the 3D view screen with the left mouse button from a particular angle.

The goal here is to snap the 3D cursor to the volume of an object from that projected point (something that blender does very nicely when used via the UI, but would probably be more than a bit tricky to do in other ways).

Is this possible? If not, which approach can get the same result?


1 Answer 1


You could try casting a ray from a specific angle, and see where on the mesh the ray intersects.

Check the mathutils library for the function intersect_ray_tri

mathutils.geometry.intersect_ray_tri(v1, v2, v3, ray, orig, clip=True)

Blender API intersect_ray_tri Function

Here is some code I found googling:


def CheckIntersections(mesh):
     intersects = 0
     obj = Blender.Object.GetSelected()
     origin = Blender.Mathutils.Vector(obj[0].getLocation("worldspace"))
     ray = Blender.Mathutils.Vector(0,0,20)
     print "Shooting ray from ",origin," towards ",ray
     for f in mesh.faces: # for each face
          v = f.verts # get vertices
          intersectionpoint = Blender.Mathutils.Intersect(v[0].co,v[1].co,v[2].co,ray,origin,1)
          if intersectionpoint != None:
               print "Ray intersects face at ", intersectionpoint
     intersects = intersects + 1

If you know the vertices of the object you want to position the 3D cursor on, you can select those verticies (1 or more) and snap cursor to selected (with more selected, it will go in the center (average) of the selected vertices).

  • $\begingroup$ Thanks! Although from an initial read of this function's API, it seems that it doesn't apply to an entire mesh object, but to a specific well defined triangle. Is there a similar function for intersections with a mesh or am I misunderstanding something? $\endgroup$
    – TLousky
    Commented Aug 6, 2015 at 14:52
  • 2
    $\begingroup$ Object.ray_cast()? In the next Blender release, there will also be BVHTree.ray_cast(). $\endgroup$
    – CodeManX
    Commented Aug 6, 2015 at 14:54
  • $\begingroup$ Yes honestly it is slightly more complex than I lead on, but I think it's a more robust method if you can get it to work. blender.stackexchange.com/questions/9073/… seems to have some code that may be useful. Its like you have to convert all faces to triangles, then loop through each one. $\endgroup$
    – beiller
    Commented Aug 6, 2015 at 14:56
  • $\begingroup$ Thanks beiller, Although it seems that function CoDEmanX mentioned might be a simpler solution. Thanks! $\endgroup$
    – TLousky
    Commented Aug 6, 2015 at 14:58
  • $\begingroup$ Of course, I didn't see that function! Looks nice good luck, $\endgroup$
    – beiller
    Commented Aug 6, 2015 at 15:37

You must log in to answer this question.

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