Convert "mel.eval('select -r mesh.vtx[10]')" to Python

Convert "mel.eval('select -r mesh.vtx[10]')" to Python

Anonymous
Not applicable
807 Views
2 Replies
Message 1 of 3

Convert "mel.eval('select -r mesh.vtx[10]')" to Python

Anonymous
Not applicable

Sorry  this is a basic question.

I'm trying to figure out how to select mesh vertices by vertex id in Python.

I have it working with the mel code mel.eval('select -r mesh.vtx[10]')

but I need to be able to use a variable number in place of 10, so I need to be able to do this in Python. Also, how do you return the vertex number of a selected mesh vertex? Thanks.

0 Likes
808 Views
2 Replies
Replies (2)
Message 2 of 3

stephenkmann
Collaborator
Collaborator

I'm not sure how much you want to learn here. but you may just want to find a good intro to Python in maya tutorial.

 

in the meantime. the  cmds module of Python is very similar to all the mel commands.

 

import maya.cmds as cmds

cmds.select("mesh.vtx[10]", replace = True)

 

if you wanted to select a range of verts you could use a simple loop

cmds.select(clear = True)

for i in range(0,10):

         cmds.select("mesh.vtx[{}]".format(i), add = True)

 

 hth

-=s

 

 

 

 

 

 

 

 

 

 

 

 

 

Message 3 of 3

muir.j
Contributor
Contributor
import maya.cmds as cmds
asset = 'mesh'
num = 100
cmds.select(asset + '.vtx['+str(num)+']')
foo = cmds.ls(sl=True)
foo = foo[0]
left = foo.index('[')
right = foo.index(']')
output = int(foo[left+1:right])
print output