Find the closest point on mesh

1,832 views
Skip to first unread message

illunara

unread,
Dec 4, 2014, 7:10:36 AM12/4/14
to python_in...@googlegroups.com
Hi everybody
Maya already had closestPointOnMesh node to do it, but i don't know how it working from inside. Any chance  to read the source of this node?

Anyway, i try to write one using openMaya, but its not working either, here is the code. Please tell me where it goes wrong. Thanks

https://gist.github.com/illunara/4a456d1407f9f60a1089

Eduardo Grana

unread,
Dec 4, 2014, 11:29:30 AM12/4/14
to python_in...@googlegroups.com
Hello Illunara,

Haven't done this for a while, but i remember using
from MFnMesh the getClosestPoint function instead,
not sure if this is what you are after...

MStatus getClosestPoint(const MPoint & toThisPoint,
MPoint & theClosestPoint,
MSpace::Space space = MSpace::kObject,
int * closestPolygon = NULL 
)const

I cant find the code where i used it,
I'll keep looking...
Cheers,
Eduardo


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/56641ac5-2e10-41b1-940b-5b8edc16614c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Tuan Nguyen

unread,
Dec 4, 2014, 12:45:12 PM12/4/14
to python_in...@googlegroups.com
Hi Graña
I tried, and fail too. The result is on mesh's surface, but clearly not the closest point :(

Eduardo Grana

unread,
Dec 4, 2014, 1:31:21 PM12/4/14
to python_in...@googlegroups.com
Hello,
I've made this simple test,


Hope its usefull, otherwise let me know.
Be aware that is with python api 2.0, and you cant mix it with regular
python api. I used it to avoid the pointers that are a little bit messy...
Cheers,
Eduardo


On Thu, Dec 4, 2014 at 2:45 PM, Tuan Nguyen <cb.il...@gmail.com> wrote:
Hi Graña
I tried, and fail too. The result is on mesh's surface, but clearly not the closest point :(

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Tuan Nguyen

unread,
Dec 4, 2014, 2:04:42 PM12/4/14
to python_in...@googlegroups.com
First of all, it works, thanks a lots, and for telling me about python api 2 too :D

Just a little confuse about MSpace, i used MSpace::kObject, but lately,it got multiply with mesh's matrix, so they should be identical, right?

Chad Vernon

unread,
Dec 4, 2014, 3:35:23 PM12/4/14
to python_in...@googlegroups.com
If you are calculating many closest points, you should use MMeshIntersector as it will be a lot faster.

Tuan Nguyen

unread,
Dec 4, 2014, 3:56:20 PM12/4/14
to python_in...@googlegroups.com
Hi Chad
I attach the code using MMeshIntersector in the first post, but the result gone wrong :(
can you take a quick look at it please?

Eduardo Grana

unread,
Dec 4, 2014, 4:30:38 PM12/4/14
to python_in...@googlegroups.com
Yes, it should be the same, although I don't know how MMeshIntersector handles spaces.


On Thu, Dec 4, 2014 at 4:04 PM, Tuan Nguyen <cb.il...@gmail.com> wrote:
First of all, it works, thanks a lots, and for telling me about python api 2 too :D

Just a little confuse about MSpace, i used MSpace::kObject, but lately,it got multiply with mesh's matrix, so they should be identical, right?

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Chad Vernon

unread,
Dec 4, 2014, 6:40:23 PM12/4/14
to python_in...@googlegroups.com
You're passing in the wrong matrix.  You don't want the inverse you just want the inclusive matrix :
 
mat = meshPath.inclusiveMatrix()
resultPoint = om.MPoint()
polyIntersect = om.MMeshIntersector()
polyIntersect.create(shape, mat)
ptON = om.MPointOnMesh()
polyIntersect.getClosestPoint(pt, ptON);
resultPoint = om.MPoint(ptON.getPoint().x, ptON.getPoint().y, ptON.getPoint().z)

Tuan Nguyen

unread,
Dec 4, 2014, 11:42:18 PM12/4/14
to python_in...@googlegroups.com
Thank Chad, it works beautifully :D

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/EznnAtM0Hgw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/68f88735-8cb6-44db-8655-f93d7aff80a1%40googlegroups.com.

igo rov

unread,
Jul 30, 2018, 5:52:29 PM7/30/18
to Python Programming for Autodesk Maya
Hi , I know its a old post...but how to you get the world position of the point which is MFloatPoint, 3d Point in objects space?


Am Freitag, 5. Dezember 2014 05:42:18 UTC+1 schrieb illunara:
Thank Chad, it works beautifully :D
On Fri, Dec 5, 2014 at 6:40 AM, Chad Vernon <chadv...@gmail.com> wrote:
You're passing in the wrong matrix.  You don't want the inverse you just want the inclusive matrix :
 
mat = meshPath.inclusiveMatrix()
resultPoint = om.MPoint()
polyIntersect = om.MMeshIntersector()
polyIntersect.create(shape, mat)
ptON = om.MPointOnMesh()
polyIntersect.getClosestPoint(pt, ptON);
resultPoint = om.MPoint(ptON.getPoint().x, ptON.getPoint().y, ptON.getPoint().z)


On Thursday, December 4, 2014 12:56:20 PM UTC-8, illunara wrote:
Hi Chad
I attach the code using MMeshIntersector in the first post, but the result gone wrong :(
can you take a quick look at it please?

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/EznnAtM0Hgw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_maya+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages