Search code examples
pythonpolygonmeshmayavertex

Get the mesh name from the selected vertex


I have a script where the user selects a vertex, and should store the name of the mesh that vertex belongs to.

However I'm unaware of a way to elegantly get that information. It's printed out nice, e.g. pPipe1.vtx[242]. But getting just the pPipe1 transform node name doesn't seem super straightforward. Would rather not resort to cutting off characters in the string. That seems bad practice.


Solution

  • The ugly way is the easy way - if you string split the vertex entries on period you'll get the transform parents. However you can also pass vertex entries to cmds.ls(o=True) which strips off attribute and component names and returns only objects:

    cmds.select("pCube1.vtx[*]")
    sel = cmds.ls(sl=True)
    print sel
    # [u'pCube1.vtx[0:7]']
    obj = cmds.ls(*sel, o=True)
    print obj
    # [u'pCubeShape1']