Here’s a relatively easy newbie version that doesn’t need the API, in case that’s the stumbling block:
import maya.cmds as cmds
def get_verts_below(obj, comparison):
results = []
# get all the vertices in the object
vertlist = cmds.polyListComponentConversion(obj, tv=True)
# this get all of their world-space positions as a flat list:
raw_positions = cmds.xform(vertlist, q=True, translation=True, worldSpace=True)
# grab the y components, which which will be index 1 of every three (ie, 012012012...)
y_positions = [raw_positions[i] for i in range(len(raw_positions)) if i % 3 == 1]
# grab all the ones that are below your threshold. `enumerate` counts indices for you:
for index, ypos in enumerate(y_positions):
if ypos <= comparison:
results.append(f"{obj}.vtx[{index}]")
#return the list
return results
get_verts_below("pSphere1", 0)
# ['pSphere1.vtx[0]', 'pSphere1.vtx[1]', 'pSphere1.vtx[2]', 'pSphere1.vtx[3]', 'pSphere1.vtx[4]', 'pSphere1.vtx[5]', 'pSphere1.vtx[6]', 'pSphere1.vtx[7]', 'pSphere1.vtx[8]', 'pSphere1.vtx[9]', 'pSphere1.vtx[10]', 'pSphere1.vtx[11]', 'pSphere1.vtx[12]', 'pSphere1.vtx[13]', 'pSphere1.vtx[14]', 'pSphere1.vtx[15]', 'pSphere1.vtx[16]', 'pSphere1.vtx[17]', 'pSphere1.vtx[18]', 'pSphere1.vtx[19]', 'pSphere1.vtx[20]', 'pSphere1.vtx[21]', 'pSphere1.vtx[22]', 'pSphere1.vtx[23]', 'pSphere1.vtx[24]', 'pSphere1.vtx[25]', 'pSphere1.vtx[26]', 'pSphere1.vtx[27]', 'pSphere1.vtx[28]', 'pSphere1.vtx[29]', 'pSphere1.vtx[30]', 'pSphere1.vtx[31]', 'pSphere1.vtx[32]', 'pSphere1.vtx[33]', 'pSphere1.vtx[34]', 'pSphere1.vtx[35]', 'pSphere1.vtx[36]', 'pSphere1.vtx[37]', 'pSphere1.vtx[38]', 'pSphere1.vtx[39]', 'pSphere1.vtx[40]', 'pSphere1.vtx[41]', 'pSphere1.vtx[42]', 'pSphere1.vtx[43]', 'pSphere1.vtx[44]', 'pSphere1.vtx[45]', 'pSphere1.vtx[46]', 'pSphere1.vtx[47]', 'pSphere1.vtx[48]', 'pSphere1.vtx[49]', 'pSphere1.vtx[50]', 'pSphere1.vtx[51]', 'pSphere1.vtx[52]', 'pSphere1.vtx[53]', 'pSphere1.vtx[54]', 'pSphere1.vtx[55]', 'pSphere1.vtx[56]', 'pSphere1.vtx[57]', 'pSphere1.vtx[58]', 'pSphere1.vtx[59]', 'pSphere1.vtx[60]', 'pSphere1.vtx[61]', 'pSphere1.vtx[62]', 'pSphere1.vtx[63]', 'pSphere1.vtx[64]', 'pSphere1.vtx[65]', 'pSphere1.vtx[66]', 'pSphere1.vtx[67]', 'pSphere1.vtx[68]', 'pSphere1.vtx[69]', 'pSphere1.vtx[70]', 'pSphere1.vtx[71]', 'pSphere1.vtx[72]', 'pSphere1.vtx[73]', 'pSphere1.vtx[74]', 'pSphere1.vtx[75]', 'pSphere1.vtx[76]', 'pSphere1.vtx[77]', 'pSphere1.vtx[78]', 'pSphere1.vtx[79]', 'pSphere1.vtx[80]', 'pSphere1.vtx[81]', 'pSphere1.vtx[82]', 'pSphere1.vtx[83]', 'pSphere1.vtx[84]', 'pSphere1.vtx[85]', 'pSphere1.vtx[86]', 'pSphere1.vtx[87]', 'pSphere1.vtx[88]', 'pSphere1.vtx[89]', 'pSphere1.vtx[90]', 'pSphere1.vtx[91]', 'pSphere1.vtx[92]', 'pSphere1.vtx[93]', 'pSphere1.vtx[94]', 'pSphere1.vtx[95]', 'pSphere1.vtx[96]', 'pSphere1.vtx[97]', 'pSphere1.vtx[98]', 'pSphere1.vtx[99]', 'pSphere1.vtx[100]', 'pSphere1.vtx[101]', 'pSphere1.vtx[102]', 'pSphere1.vtx[103]', 'pSphere1.vtx[104]', 'pSphere1.vtx[105]', 'pSphere1.vtx[106]', 'pSphere1.vtx[107]', 'pSphere1.vtx[108]', 'pSphere1.vtx[109]', 'pSphere1.vtx[110]', 'pSphere1.vtx[111]', 'pSphere1.vtx[112]', 'pSphere1.vtx[113]', 'pSphere1.vtx[114]', 'pSphere1.vtx[115]', 'pSphere1.vtx[116]', 'pSphere1.vtx[117]', 'pSphere1.vtx[118]', 'pSphere1.vtx[119]', 'pSphere1.vtx[120]', 'pSphere1.vtx[121]', 'pSphere1.vtx[122]', 'pSphere1.vtx[123]', 'pSphere1.vtx[124]', 'pSphere1.vtx[125]', 'pSphere1.vtx[126]', 'pSphere1.vtx[127]', 'pSphere1.vtx[128]', 'pSphere1.vtx[129]', 'pSphere1.vtx[130]', 'pSphere1.vtx[131]', 'pSphere1.vtx[132]', 'pSphere1.vtx[133]', 'pSphere1.vtx[134]', 'pSphere1.vtx[135]', 'pSphere1.vtx[136]', 'pSphere1.vtx[137]', 'pSphere1.vtx[138]', 'pSphere1.vtx[139]', 'pSphere1.vtx[140]', 'pSphere1.vtx[141]', 'pSphere1.vtx[142]', 'pSphere1.vtx[143]', 'pSphere1.vtx[144]', 'pSphere1.vtx[145]', 'pSphere1.vtx[146]', 'pSphere1.vtx[147]', 'pSphere1.vtx[148]', 'pSphere1.vtx[149]', 'pSphere1.vtx[150]', 'pSphere1.vtx[151]', 'pSphere1.vtx[152]', 'pSphere1.vtx[153]', 'pSphere1.vtx[154]', 'pSphere1.vtx[155]', 'pSphere1.vtx[156]', 'pSphere1.vtx[157]', 'pSphere1.vtx[158]', 'pSphere1.vtx[159]', 'pSphere1.vtx[160]', 'pSphere1.vtx[161]', 'pSphere1.vtx[162]', 'pSphere1.vtx[163]', 'pSphere1.vtx[164]', 'pSphere1.vtx[165]', 'pSphere1.vtx[166]', 'pSphere1.vtx[167]', 'pSphere1.vtx[168]', 'pSphere1.vtx[169]', 'pSphere1.vtx[170]', 'pSphere1.vtx[171]', 'pSphere1.vtx[172]', 'pSphere1.vtx[173]', 'pSphere1.vtx[174]', 'pSphere1.vtx[175]', 'pSphere1.vtx[176]', 'pSphere1.vtx[177]', 'pSphere1.vtx[178]', 'pSphere1.vtx[179]', 'pSphere1.vtx[180]', 'pSphere1.vtx[181]', 'pSphere1.vtx[182]', 'pSphere1.vtx[183]', 'pSphere1.vtx[184]', 'pSphere1.vtx[185]', 'pSphere1.vtx[186]', 'pSphere1.vtx[187]', 'pSphere1.vtx[188]', 'pSphere1.vtx[189]', 'pSphere1.vtx[190]', 'pSphere1.vtx[191]', 'pSphere1.vtx[192]', 'pSphere1.vtx[193]', 'pSphere1.vtx[194]', 'pSphere1.vtx[195]', 'pSphere1.vtx[196]', 'pSphere1.vtx[197]', 'pSphere1.vtx[198]', 'pSphere1.vtx[199]', 'pSphere1.vtx[380]']
This does the same thing and uses the same logic, but it’s a bit faster, especially on big meshes. The difference is that we don’t create the list of results in memory and append to it, instead we use yield
to return the results one at a time, and we also don’t make the intermediate list of y values:
def get_verts_below_iterator(obj, comparison):
# results = [] --- no need to declare a result list
# get all the vertices in the object
vertlist = cmds.polyListComponentConversion(obj, tv=True)
# this get all of their world-space positions as a flat list:
raw_positions = cmds.xform(vertlist, q=True, translation=True, worldSpace=True)
# using parens instead of square brackes turns this into a generator
y_positions = (raw_positions[i] for i in range(len(raw_positions)) if i % 3 == 1)
# grab all the ones that are below your threshold. `enumerate` counts indices for you:
for index, ypos in enumerate(y_positions):
if ypos <= comparison:
yield (f"{obj}.vtx[{index}]")
The big difference here is that get_verts_below_iterator()
will produce a stream of vertices that are below the threshold instead of a list. You could still make them into a list if you are doing something where you need them all at once --but if you don’t, you save time by not moving around big chunks of memory.