2

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?

TLousky
  • 16,043
  • 1
  • 40
  • 72

1 Answers1

3

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:

http://blenderartists.org/forum/archive/index.php/t-154521.html

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).

beiller
  • 2,170
  • 12
  • 11
  • 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? – TLousky Aug 06 '15 at 14:52
  • 2
    Object.ray_cast()? In the next Blender release, there will also be BVHTree.ray_cast(). – CodeManX Aug 06 '15 at 14:54
  • 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. http://blender.stackexchange.com/questions/9073/how-to-check-if-two-meshes-intersect-in-python seems to have some code that may be useful. Its like you have to convert all faces to triangles, then loop through each one. – beiller Aug 06 '15 at 14:56
  • Thanks beiller, Although it seems that function CoDEmanX mentioned might be a simpler solution. Thanks! – TLousky Aug 06 '15 at 14:58
  • Of course, I didn't see that function! Looks nice good luck, – beiller Aug 06 '15 at 15:37
  • @beiller Do you know how I can do this for clicking on nodes? If so, could you please take a look at my question here and see if you can offer a solution? I think this should be the only way I can activate a node. – Amir Mar 10 '18 at 19:06
  • @Amir, I've seen your question, will try to find time to answer this week – TLousky Mar 10 '18 at 19:31