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.
I've approached this in a few ways (two if which are listed below), and I'd appreciate opinions:
Approach1:
1) Create List1: nodes that have the required attribute via:
cmds.ls('*.customAttrName') ..not so fast (scene level search..) but somewhat convenient
2) Create List2: transforms of specific node type under the 'top_node' ..eg: all transforms of curves under the top_node
3) intersect the 2 lists using : list(set(List1) & set(List2))
top_node = 'M_worldMover_crv'
search_string = '*.gvAnim'
gvAnim_list = cmds.ls(search_string, r=1, o=1, l=1) curves_list = cmds.listRelatives(cmds.ls(top_node, dag=1, type='nurbsCurve',ni=1, o=1, r=1, l=1), p=1, f=1) final_list = list(set(gvAnim_list) & set(curves_list))
cmds.select(final_list)
Approach2:
I think this one is a bit faster but not greatly.
1) create List1: transforms of specific node type under the 'top_node' (eg all transforms of meshes).
2) Create List2: iterate through List1 to see if the node contains the custom attribute and append to the list.
top_node = 'M_worldMover_crv'
search_attribute = '.gvAnim'
curves_list = cmds.listRelatives(cmds.ls(top_node, dag=1, type='nurbsCurve',ni=1, o=1, r=1, l=1), p=1, f=1) final_list = [i for i in curves_list if cmds.ls(i + search_attribute )] cmds.select(final_list)
I guess my issue would be solved, mostly, if the command ls could take a string argument for attribute, while using the 'dag' flag, and a node to search under:
example:
cmds.ls(top_node, dag=1, type='mesh') # works to search under a hierarchy
cmds.ls('*.customAttr', o=1) # works to find all objects in scene with the specified attribute
but.. this obviously doesnt work, unless I'm missing something:
cmds.ls(top_node, '*.customAttr', dag=1, o=1) # but it's sort of what I am trying to do..
Is the API faster? If so is there someone that could point me in the right direction please?
Thanks for any help.
Gerard.