Just to get geeky and not sure if it's any faster but here's another 
version. ;)

import maya.cmds as mc
import itertools

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return itertools.izip_longest(fillvalue=fillvalue, *args)

# search under node for nodes with attr that are of type in allowed_types
root_node = 'rootNode'
search_attr = 'testAttr'
allowed_types = frozenset(['mesh'])
valid_nodes = [node for node, type in grouper(mc.ls('*.{0}'.format(
search_attr), long=True, recursive=True, showType=True, o=True), 2) if type 
in allowed_types and node.startswith(root_node)]
mc.select(valid_nodes, r=1)

Note that this version assumes root_node is actually a root node (so has no 
parent). If you want to allow it to have a parent then you wouldn't do 
node.startswith() but something like:

import maya.cmds as mc
import itertools

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return itertools.izip_longest(fillvalue=fillvalue, *args)

# search under node for nodes with attr that are of type in allowed_types
root_node = 'rootNode'
search_attr = 'testAttr'
allowed_types = frozenset(['mesh'])

root_node_as_parent = '{0}|'.format(root_node)
valid_nodes = [node for node, type in grouper(mc.ls('*.{0}'.format(
search_attr), long=True, recursive=True, showType=True, o=True), 2) if type 
in allowed_types and root_node_as_parent in node]
mc.select(valid_nodes, r=1)

Sometimes the fastest thing is limiting the amount of commands you run that 
actually rely on querying something from Maya so this could be fast. Could 
you provide a heavyweight test scene example? ;)
Both these versions rely on a single maya command query.
Let me know what kind of speeds you get from this. :D

Cheers and /geekout,
Roy


On Thursday, March 12, 2015 at 4:01:51 AM UTC+1, GerardVOK wrote:
>
> Hey there Chris. Thanks for the code I'll take a gander. Fuel catchup was 
> great. Haven't seen any of the old team in quite some time.
> Cheers.
> G.
>
> On Thu, Mar 12, 2015 at 12:45 PM, Chris Gardner <chrisg....@gmail.com 
> <javascript:>> wrote:
>
>> hey gerard,
>>
>> nice to see you at the pub the other day :)
>>
>> here's some API code for looking for custom attrs called "tags" and then 
>> looking for a string inside that. it's a bunch faster than maya cmds. adapt 
>> as necessary. it's filtering on transform nodes currently, but you could 
>> change that for your node type you want to target.
>>
>> import time
>> import maya.api.OpenMaya as om2
>>
>>
>> def findByTag(inTag):
>>     coll = []
>>     
>>     sel = om2.MGlobal.getSelectionListByName('*')
>>     depFn = om2.MFnDependencyNode()
>>     
>>     xformType = om2.MTypeId(0x5846524d)
>>     
>>     for i in xrange(sel.length()):
>>         mObj = sel.getDependNode(i)
>>         depNode = depFn.setObject(mObj)
>>         if depNode.typeId == xformType:
>>             if depNode.hasAttribute('tags'):
>>                 plug = depNode.findPlug("tags", False)
>>                 result = plug.asString()
>>                 
>>                 if result:
>>                     tags = result.split(',')
>>                     if inTag in tags:
>>                         coll.append(depNode.name())
>>     return coll
>>     
>>
>> ts = time.time()
>> coll = findByTag('blah')
>> te = time.time()
>>
>> print coll
>> #cmds.select(coll)
>>
>> print 'api2 %2.4f sec' % (te - ts)
>> print len(coll)
>>
>> cheers,
>> chrisg
>>
>>
>> On 12 March 2015 at 12:38, Gerard v <gera...@gmail.com <javascript:>> 
>> wrote:
>>
>> Hi all.
>>>
>>> I am trying to determine the most efficient way to search for nodes 
>>> (transforms) of specified types (eg transform of a curve) under a hierarchy 
>>> (lets call it 'top_node') that have a specific custom (string) attribute.
>>> When potentially dealing with a large number of nodes speed becomes a 
>>> factor. 
>>>
>>>
>>>  -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Python Programming for Autodesk Maya" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to python_inside_maya+unsubscr...@googlegroups.com <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/python_inside_maya/CAPoKtNfn1sCAivOYEw5NQ2kFpWGzdQVYZcfP8FXBrm53_mm%3DOQ%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/python_inside_maya/CAPoKtNfn1sCAivOYEw5NQ2kFpWGzdQVYZcfP8FXBrm53_mm%3DOQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/0ffe7f97-9e92-41dd-9149-41c3609223b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to