newbie question

70 views
Skip to first unread message

Serena Xu

unread,
May 18, 2012, 10:43:33 AM5/18/12
to python_inside_maya
I would like to get the information of closest vertex on a cube to the
given point. I'm unsure of the type of argument I'm suppose to put as
the second argument of the getClosestPoint()
Please help!


def test():
cube = cmds.polyCube()

pointInfo = om.MPointOnMesh()
point = om.MPoint( 0, 8, 3)
cubeWS = om.MMeshIntersector()
result = cubeWS.getClosestPoint (point, pointInfo, 10)
position = [(result.x), (result.y), (result.z)]

return (position)

Kamil Hepner

unread,
May 18, 2012, 11:53:56 AM5/18/12
to python_in...@googlegroups.com
If you want to use maya python API, try this:

import maya.OpenMaya as om

om.MGlobal.clearSelectionList()

om.MGlobal.selectByName('pCube1')


sList = om.MSelectionList()

om.MGlobal.getActiveSelectionList(sList)

item = om.MDagPath()

sList.getDagPath(0, item)

item.extendToShape()


fnMesh = om.MFnMesh(item)

inPoint = om.MPoint(0, 2, 3)

outPoint = om.MPoint(0.0, 0.0, 0.0)

worldSpace = om.MSpace.kWorld


fnMesh.getClosestPoint(inPoint, outPoint, worldSpace)


print outPoint[2]



Or you can use simpler pymel function getClosestPoint(), from Mesh class, it's depend of what you want.


Cya!



2012/5/18 Serena Xu <toomuc...@gmail.com>

Serena Xu

unread,
May 18, 2012, 12:33:20 PM5/18/12
to python_inside_maya
Thanks for your reply! I still have a question about outPoint. How do
you get that initial value?

On May 18, 11:53 am, Kamil Hepner <hektor1...@gmail.com> wrote:
> If you want to use maya python API, try this:
>
> import maya.OpenMaya as om
>
> om.MGlobal.clearSelectionList()
>
> om.MGlobal.selectByName('pCube1')
>
> sList = om.MSelectionList()
>
> om.MGlobal.getActiveSelectionList(sList)
>
> item = om.MDagPath()
>
> sList.getDagPath(0, item)
>
> item.extendToShape()
>
> fnMesh = om.MFnMesh(item)
>
> inPoint = om.MPoint(0, 2, 3)
>
> outPoint = om.MPoint(0.0, 0.0, 0.0)
>
> worldSpace = om.MSpace.kWorld
>
> fnMesh.getClosestPoint(inPoint, outPoint, worldSpace)
>
> print outPoint[2]
>
> Or you can use simpler pymel function getClosestPoint(), from Mesh class,
> it's depend of what you want.
>
> Cya!
>
> 2012/5/18 Serena Xu <toomuchcof...@gmail.com>

Justin Israel

unread,
May 18, 2012, 12:48:48 PM5/18/12
to python_in...@googlegroups.com
He is just creating a new MPoint to be used when it is passed into getClosestPoint. The method will use that outPoint reference to store the result. Really he didn't have to initialize it with (0,0,0), because thats the default anyways:

print om.MPoint() == om.MPoint(0.0, 0.0, 0.0)
# True

If you are a python guy, the maya api can be a little confusing because its very C++ oriented. You often are asked to pass along a reference for the result to be stored, as opposed to expecting a return value as a result.

Serena Xu

unread,
May 18, 2012, 12:52:12 PM5/18/12
to python_inside_maya
oooh I see. Thank you! yea that part with passing a reference to store
the result is confusing.

Justin Israel

unread,
May 18, 2012, 1:12:10 PM5/18/12
to python_in...@googlegroups.com
The new API 2.0 is attempting to make it a more pythonic wrapper by actually returning values instead of making you pass references. It was also a bit cumbersome when you have to use the special helper functions to create python "pointers" that can be passed for return values on functions that explicitly need a type you don't normally have.

Serena Xu

unread,
May 19, 2012, 8:12:51 PM5/19/12
to python_inside_maya

hey, Justin Israel and Kamil Hepner, are either of you in NYC?....by
any chance?

Kamil Hepner

unread,
May 20, 2012, 3:25:41 AM5/20/12
to python_in...@googlegroups.com
unfortunately, no

2012/5/20, Serena Xu <toomuc...@gmail.com>:

Justin Israel

unread,
May 20, 2012, 11:49:26 AM5/20/12
to python_in...@googlegroups.com, python_inside_maya
Sorry, I'm in LA

Serena Xu

unread,
May 22, 2012, 3:11:58 PM5/22/12
to python_inside_maya
hehe i thought i'd take a shot. thanks though!

On May 20, 11:49 am, Justin Israel <justinisr...@gmail.com> wrote:
> Sorry, I'm in LA
>

Justin Israel

unread,
May 22, 2012, 5:07:47 PM5/22/12
to python_in...@googlegroups.com
Plane ticket?
Reply all
Reply to author
Forward
0 new messages