Get the mesh name from the selected vertex

1.9k views Asked by At

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.

2

There are 2 answers

0
theodox On BEST ANSWER

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']
0
DrWeeny On

for one item :

sel = cmds.ls(sl=True)[0].split('.')[0]

for a list of items :

sel = [i.split('.')[0] for i in cmds.ls(sl=True)]
sel = list(set(sel)) # Use this to remove multiple instance of an object