Search code examples
c++pluginsmeshmaya

Get a mesh node from a transform node in Maya C++ API


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.


Solution

  • 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