Iterate downstream (outputs and dependents)

I made a script, which recursively iterates down the network, over all the selected nodes' outputs and dependents. I use it to find all the Filecache SOP nodes (just not to overlook something). It can be also used to generate TOP chain.

import wf_network_utils
reload(wf_network_utils)

# -------------------
# iteration
def fc_iterate_downstream (node,go_below_fc) :
    global dependents
    global filecaches
    candidates = list(set(  node.outputs() + node.dependents()  ))

    for candidate in candidates :
        if candidate not in dependents :
            dependents.append(candidate)

            # candidate is filecache
            if candidate.type().name() == "filecache" :
                filecaches.append(candidate)

                # go below this filecache
                if go_below_fc == True :
                    fc_iterate_downstream(candidate,go_below_fc)

            # candidate is not a filecache
            else:
                fc_iterate_downstream(candidate,go_below_fc)

        # If candidate's parent is a dopnet, check also dopnet,
        # because Dop I/O is dependent on Dop Object,
        # which is often independent.
        parent = candidate.parent()
        if parent.type().name() == "dopnet" :
            if parent not in dependents :
                dependents.append(parent)
                fc_iterate_downstream(parent,go_below_fc)
# -------------------

# init lists
dependents  = []
filecaches  = []
node        = hou.selectedNodes()[0]
go_below_fc = True

# iterate
fc_iterate_downstream(node, go_below_fc)

# print list and copy to clipboard
wf_network_utils.fc_list_print(filecaches)

 

def fc_list_print (filecaches) :
    import wf_network_ui
    reload(wf_network_ui)

    # remove duplicates
    filecaches = list(dict.fromkeys(filecaches))

    # create search pattern
    print '\n-------    found:  -------'
    pattern       = ''
    pattern_count = 0

    for fc in filecaches :
        # set session Id
        session_id = fc.sessionId()
        wf_network_ui.parm_create (fc, "integer", "session_id", "session_id")
        wf_network_ui.parm_update (fc, "integer", "session_id", hidden="True")
        fc.parm("session_id").set(session_id)

        # create search pattern
        if pattern_count > 0 :
            pattern   += ' | '
        pattern       += "session_id" + '=' + str(session_id) + ''
        pattern_count += 1
        print 'FILECACHE: ' + str(fc.path()) + ''

    hou.ui.copyTextToClipboard(pattern)
    print '--------------------------'