Python: mouse click to select closest vertex

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 06 June 2012   #1
Python: mouse click to select closest vertex

Hi,

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.

Thanks
__________________
if(youAreHappyAndYouKnowIt == true)
{
clapYourHands();
}
 
Old 06 June 2012   #2
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.

Last edited by NaughtyNathan : 06 June 2012 at 10:06 PM.
 
Old 06 June 2012   #3
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! )
 
Old 06 June 2012   #4
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!
__________________
if(youAreHappyAndYouKnowIt == true)
{
clapYourHands();
}
 
Old 06 June 2012   #5
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?)
 
Old 06 June 2012   #6
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?
__________________
if(youAreHappyAndYouKnowIt == true)
{
clapYourHands();
}
 
Old 06 June 2012   #7
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.
 
Old 06 June 2012   #8
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.
 
Old 06 June 2012   #9
Thanks for your help. Everything works perfectly!
__________________
if(youAreHappyAndYouKnowIt == true)
{
clapYourHands();
}
 
Old 06 June 2012   #10
Thread automatically closed

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.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 03:14 PM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.