A good investment is a set of functions which extract the index numbers from a Maya-style selection list:
test
import re
brackets = re.compile("(\[)(.*)(\])")
def extract_indices (maya_selection):
comps = (brackets.search(c) for c in maya_selection)
non_empty_string_comps = (cmp[2] for cmp in comps if cmp)
for eachcomp in non_empty_string_comps:
s, _, e = eachcomp.partition(":")
if (s and e):
for item in range(int(s), int(e) + 1):
yield item
else:
yield(int(s))
def generate_components(object, comp, indices):
for i in indices:
yield object + "." + comp + "[" + str(i) + "]"
indices = extract_indices(cmds.ls(sl=True))
# [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41]
recap = generate_components("pCylinder", "v", indices)
# ['pCylinder.v[0]', 'pCylinder.v[1]', 'pCylinder.v[2]', 'pCylinder.v[3]', 'pCylinder.v[4]', 'pCylinder.v[5]', 'pCylinder.v[6]', 'pCylinder.v[7]', 'pCylinder.v[8]', 'pCylinder.v[9]', 'pCylinder.v[10]', 'pCylinder.v[11]', 'pCylinder.v[12]', 'pCylinder.v[13]', 'pCylinder.v[14]', 'pCylinder.v[15]', 'pCylinder.v[16]', 'pCylinder.v[17]', 'pCylinder.v[18]', 'pCylinder.v[19]', 'pCylinder.v[40]', 'pCylinder.v[2]', 'pCylinder.v[3]', 'pCylinder.v[4]', 'pCylinder.v[7]', 'pCylinder.v[8]', 'pCylinder.v[10]', 'pCylinder.v[11]']
these two offer bidirectional conversion between Maya component strings (as you can see, what kind of component doesn’t matter) and integer indices. You can always use the builtin for inversion selections, but using index lists is good for things like intersecting two different selection, seeing if one component is in an list, or other set-like operations.