View Full Version : Python: mouse click to select closest vertex
06-28-2012, 09:28 PM
I need to be able to select a mesh, and have the closest vertex under the cursor's xyz values displayed .
I'm looking along the lines of using a dragger context, but I'm finding it hard to understand how that click would be converted to a 3d coordinate and matched with the desired vertex.
06-28-2012, 10:04 PM
hmm, it's kinda simple in theory but in practice it's rather tedious. The main element you need for this is a rayCast or rayIntersect function. Maya has one that you can access via the API or via python, and there is also a pretty decent plug-in wrapper for this for MEL. There's no point me going through all this yet again so just do a search on this forum for rayIntersect and you'll find out all you need on that score.
Once you have that, you use draggerContext to get TWO points each click (by changing the projection space) then use those two points to construct a ray vector that you use to hit the mesh. If the ray intersect doesn't give you the nearest vertex for free (iirc it does) you can instead feed the hit point into closestPointOnMesh to find the nearest vertex.
06-28-2012, 10:16 PM
actually, I've just re-read your original post and I'm getting a completely different picture from before.
do you mean you want to move the cursor around over a selected mesh and see an xyz readout change as your cursor nears different vertices..? That would be a lot more complicated than what I previously suggested, where I just thought you wanted to click on a mesh and just get the nearest vertex to the click (but thinking about it, why not just use the bog-standard selection tool/preselection highlighting and a script job! :D )
06-29-2012, 01:18 PM
Thank you for your advice.
It seems as though i need something different than what I originally anticipated. Basically i need a locator to be able to be dragged along the surfaces of a mesh, rather than have it snap to the nearest vertex.
I must say I have only been working with Maya/C++/python for a very short time so the whole task seems a bit of a mystery!
06-29-2012, 01:23 PM
in that case then, yes, the draggerContext/rayIntersect solution I detailed previously should work fine.
However, if you simply geometry-constrain a locator to a mesh (or even just make the mesh "live") this is the behaviour you already get..! (or are you wanting to drag it off one or more meshes too?)
06-29-2012, 02:09 PM
Thanks! That works really well.
I just have one more question. Is there a way though using this geometry constrain tool to extract the normal of the polygon the locator is currently constrained to, or even just get the current polygon?
06-29-2012, 02:36 PM
the rayIntersect method would obviously give you both of those for free, but you should be able to get them using the closestPointOnMesh node if you're using constraints instead.
06-29-2012, 03:17 PM
There is of also a normal constraint, to get the locator to rotate to the normal ; current polygon is still not known there but I assumed you would want to know that to find out the normal.
06-29-2012, 03:53 PM
Thanks for your help. Everything works perfectly!
06-29-2012, 03:53 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2015, Jelsoft Enterprises Ltd.