How are you selecting your objects to iterate over? And how long is ‘too long’?
If I create 10,000 transforms with the attribute “.myattr” and give it a numeric value, then select by the wildcard ls("*.myattr", o=True) and return only those whose myattr attribute == 10, it takes .3 seconds. Is that too long, or is yours much longer? Are you using a more complex wildcard to select the objects?
That being said, you can get improved performance using the API:
import time
import maya.api.OpenMaya as om2
import maya.cmds as cmds
def setup():
grp = cmds.createNode('transform', n='topgrp')
nodes = []
for i in range(5000):
t = cmds.createNode('transform')
cmds.addAttr(t, ln='myattr', dv=i%200)
nodes.append(t)
cmds.parent(nodes, grp)
def test():
## Using maya.cmds
#
start = time.time()
res = []
for i in cmds.ls('*.myattr', o=True):
if cmds.getAttr('%s.myattr' % i) == 10:
res.append(i)
end = time.time()
print 'maya.cmds: %s' % (end - start)
## Using API 2.0
#
start = time.time()
search = "*.myattr"
sel = om2.MGlobal.getSelectionListByName(search)
depFn = om2.MFnDependencyNode()
res = []
for i in xrange(sel.length()):
mObj = sel.getDependNode(i)
depNode = depFn.setObject(mObj)
if depNode.findPlug("myattr", True).asInt() == 10:
res.append(depNode.name())
end = time.time()
print 'api: %s' % (end - start)
# maya.cmds: 0.15700006485
# api: 0.0460000038147
If you’re not on 2012 or later (or maybe 2011?) you’ll have to use the 1.0 API, which will be slightly different. I don’t know how comfortable you are with it but if you need some help piecing that together I’m happy to help.
If your attribute is not an integer attribute, you’ll need to change the call to asInt() to one that suits your needs. Check the MPlug doc page for info on other available methods. I don’t know how to link to maya api docs (frames yay) but there are a number of methods (asInt, asBool, asFloat, …)