Search code examples
pythonlistindexingmayavertices

Maya, Python, How do i get the name of an object based on vertex selection?


I got the code working like this until i realized that the vertex index changes for geometry with over 100 vertices...

I assumed i could just split the string and everything would be dandy

import maya.cmds as mc
selPoints = mc.ls(sl = True) # list of names of selected vertices
objName = (str(selPoints[0]))[:-9]
print selPoints
print objName

Heres what it returned:

[u'pCylinder25.vtx[4]', u'pCylinder25.vtx[24]']
pCylinder

I'm trying to hack off the portion with '.vtx[integer]'

I may be going about this completely wrong, and there may be a dead simple way to do this.

Thanks


Solution

  • Wouldn't it be awesome if it were easy to get the object from Maya? From experience, I know it can be frustrating since MEL/maya.cmds doesn't use an object-oriented approach.

    Anyhow, you should refer to the documentation often for more info on the variety of string methods you can use. Really comes in handy all the time!

    To answer your question, you can use .split or .find, whichever you prefer.

    print selPoints[0].split('.vtx')[0]
    print selPoints[0][0:selPoints[0].find('.vtx')]
    

    The split method returns a list of strings created by the delimiter string '.vtx'. Then, taking the first element from that list will always be the object name.

    The find method returns the index of the substring '.vtx', so the second example simply uses slicing syntax to return the correct string.