Node dependencies in Python

   Views 3634   Replies 5   Subscribers 2
User Avatar
Member
66 posts
Joined: 3月 2017
Offline
Hey guys,

I need to find a way to retrieve the nodes that are referenced by the entire hierarchy used to cook a given node.
With node.references, node.inputAncestors, etc. I think I can get something, but Houdini has a visual representation of this already :


And this is exactly what I need. I want to know which nodes at the same level of the node selected are needed for the selected node.
Is there a function I don't know of that already does this ?

Thanks a lot,

Julien

Attachments:
Screenshot 2020-08-06 at 09.34.53.png (49.2 KB)

VFX Supervisor @ MPC London
User Avatar
Member
16 posts
Joined: 3月 2015
Offline
Probably the hou.Node.dependents() function will do the trick.

Attachments:
Annotation 2020-08-06 162313.png (29.2 KB)

User Avatar
Member
66 posts
Joined: 3月 2017
Offline
I don't think that's what the dependents function does.
This function returns a list of nodes referencing the given node.
VFX Supervisor @ MPC London
User Avatar
Member
274 posts
Joined: 7月 2013
Offline
https://bitbucket.org/jcdeblok/jdb_houdinitoolkit/src/master/python_panels/ [bitbucket.org]

Check my ‘visibility manager’ panel. It has some buttons to select the whole tree a node belongs to, or all below/above a node etc. There are a few python function in the source that are easy to rip out.
More code, less clicks.
User Avatar
Member
66 posts
Joined: 3月 2017
Offline
Thanks Jonathan, I will have a look.
In the meantime, I think I managed to get what I needed by writing a recursive function that inspects the references of a given list of nodes. The function find the references of each node, then look at their ancestors for new references, etc.
I don't think it's necessarily future proof, and I'll keep testing it further, but for now I think I got what I needed.

Please let me know if you guys feel like there is something wrong with what I'm doing.

### RETURNS REFERENCES FROM A GIVEN NODE ###
### Ignores the references inside a hda, as they are considered self contained ###
def getReferences(node):

    references = []

    if isNodeHDA(node):
        references = list(node.references(include_children=False))

        ### Removes children if referenced in the hda ###
        for reference in references:
            if isNodeChildOf(reference,node):
                references.remove(reference)
    else:
        references = node.references(include_children=True)

    references = list(set(references))

    ### Removes the node if it references itself ###
    if node in references:
        references.remove(node)

    return references

### RETURNS ALL DEPENDENCIES FOR A GIVEN NODE
def getAllDependencies(nodes, level=0, maxdepth=10):

    dependencies = []

    if level == maxdepth:
        return dependencies

    for node in nodes:
        references = getReferences(node)

        ## for each reference, get ancestors
        for reference in references:
            ancestors = reference.inputAncestors(include_ref_inputs=True,only_used_inputs=False)
            newDependencies = getAllDependencies(ancestors,level+1,maxdepth)
            newDependencies.append(reference)

            for dependency in newDependencies:
                if dependency not in dependencies:
                    dependencies.append(dependency)

    result = []
    for dependency in dependencies:
        if dependency not in nodes:
            result.append(dependency)

    return result
Edited by julien-b - 2020年8月6日 18:06:10
VFX Supervisor @ MPC London
User Avatar
Member
33 posts
Joined: 2月 2015
Offline
Hey julien-b,

I have stumbled across the same problem today and managed to solve it with a hscript command within python

https://www.sidefx.com/docs/houdini/commands/opdepend.html [www.sidefx.com]

so


hou.hscript("opdepend -d -e " + node.path())


should give you a list of dependency nodes.

cheers
Edited by Pirminus - 2024年9月26日 02:08:48

Attachments:
dependency.JPG (96.2 KB)

  • Quick Links