Tom is right. Using a set is an easy way to prevent the returned collection 
from containing duplicates (which can get out of hand in recursive tree-walking 
searches if your script branches and re-joins several times). It also prevents 
you from having to de-dupe your resulting list at the end or do membership 
testing inside your search function.

However, to call myself out on some short-sightedness, there’s no reason to 
create a new set every time; set.update makes things a little clearer (and less 
verbose).

def recursiveFindReads(node=None, group=nuke.root(), reads=None):
    if reads is None:
        reads = set()
    if node is None:
        node = nuke.selectedNode() # Let this error on purpose if no node 
selected
    for n in node.dependencies(nuke.INPUTS | nuke.HIDDEN_INPUTS):
        cls = n.Class()
        if cls == 'Read':
            reads.add(n)
        elif cls == 'Group':
            reads.update(recursiveFindReads(node=nuke.allNodes('Output', 
group=n)[0], group=n, reads=reads))
        else:
            reads.update(recursiveFindReads(node=n, group=group, reads=reads))
    return reads


-Nathan



From: Tom Ward 
Sent: Friday, April 27, 2012 4:39 AM
To: Nuke Python discussion 
Subject: Re: [Nuke-python] Re: selecting a node from its name

>From what I can see it's used to  create a new set of unique items (correct me 
>if I'm wrong!)

for example:

listA = set(["a", "b", "c"]) 
listB = set(["b", "c", "d"])

listC = listA.union(listB)

print listC



prints:


# Result:

set(['a', 'c', 'b', 'd'])



see http://docs.python.org/library/stdtypes.html#set



Tom



On 27/04/2012 12:08, Howard Jones wrote: 
  Out of interest ...

  what does 'union' do in this script


  Howard



----------------------------------------------------------------------------
    From: Nathan Rusch mailto:[email protected]
    To: [email protected] 
    Sent: Friday, 27 April 2012, 3:42
    Subject: Re: [Nuke-python] Re: selecting a node from its name


    Same function with one line cleaned up (sorry, it’s been a long day...)

    def recursiveFindReads(node=None, group=nuke.root(), reads=None):
        if reads is None:
            reads = set()
        if node is None:
            node = nuke.selectedNode() # Let this error on purpose if no node 
selected
        for n in node.dependencies(nuke.INPUTS | nuke.HIDDEN_INPUTS):
            cls = n.Class()
            if cls == 'Read':
                reads.add(n)
            elif cls == 'Group':
                reads = 
reads.union(recursiveFindReads(node=nuke.allNodes('Output', group=n)[0], 
group=n, reads=reads))
            else:
                reads = reads.union(recursiveFindReads(node=n, group=group, 
reads=reads))
        return reads


    If you really DO need to select them, just run this:

    nukescripts.clear_selection_recursive()
    [n.setSelected(True) for n in recursiveFindReads()]


    -Nathan



    From: invisfx 
    Sent: Thursday, April 26, 2012 7:35 PM
    To: [email protected] 
    Subject: [Nuke-python] Re: selecting a node from its name

    what I meant by loop is 
    depNodes = selectedNode.dependencies()
    only selects the immediate dependencies, not all the way up the graph. So I 
have to re select those nodes and do it again.
    or am I wrong?

----------------------------------------------------------------------------
    _______________________________________________
    Nuke-python mailing list
    [email protected], http://forums.thefoundry.co.uk/
    http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python


    _______________________________________________
    Nuke-python mailing list
    [email protected], http://forums.thefoundry.co.uk/
    http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python




   

_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python


--------------------------------------------------------------------------------
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python

Reply via email to