3

I want to get a shape/mesh object under a transform node active in Maya. If I select and object (e.g. a poly sphere) in Maya, when calling getActiveSelectionList method, it returns a transform node, not a shape/mesh one.

I'm getting crazy reading the API classes (MDagPath, MSelectionList, MFnDependencyNode) and methods which would achieve that but I can't find a way to do it.

So, I want to get the info (vertex coordinates) of a selected/active poly object in Maya GUI through C++ API.

jgonzac
  • 175
  • 2
  • 9

1 Answers1

6

You want to get an MDagPath leading to the transform and then use .extendToShape or .extendToShapeDirectlyBelow() to get the shape node. Then you need to get an MFnMesh from the shape and use that to get to the vertices.

Here's the python version, which is all I have handy. Apart from syntax it will work the same way in C++ :

# make a selectionList object, populate ite
sel_list = MSelectionList()
MGlobal.getActiveSelectionList(sel_list)

# make a dagPath, fill it using the first selected item
d = MDagPath()
sel_list.getDagPath(0,d)

print d.fullPathName()
# '|pCube1" <- this is the transform
d.extendToShape()
print d.fullPathName()
#  "|pCube1|pCubeShape1" < - now it points at the shape

# get the dependency node as an MFnMesh:
mesh = MFnMesh(d.node())

# now you can call MFnMesh methods to work on the object:
print mesh.numVertices()
# 8
theodox
  • 12,028
  • 3
  • 23
  • 36
  • Thank you, theodox. It's exactly what I wanted. If I use the method `mesh.getPoints(mypoints, MSpace::kWorld)` the retrieved positions that are supposed to be in world space, are (0 0 0 0) for every vertex in a single polyplane. If I use default MSpace, I get the local positions correctly. Do you have any idea why this is happening? – jgonzac May 25 '15 at 09:52
  • 1
    It could happen if you've got a degenerate transform (scaled to zero on the two axes defining the plane, for example). You're doing C++ - what does the MStatus return say? – theodox May 26 '15 at 01:37
  • Thanks for the advice. It returned `Error: (kInvalidParameter): Must have a DAG path to do world space transforms`. So I had to use `MFnMesh fnmesh(dagpath)` instead of the `meshnode` in order to get the global coordinates when using `fnmesh.getPoints(...)` – jgonzac May 27 '15 at 09:38