简体   繁体   中英

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 (eg 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM