Jump to content
Spyrogif

Add a Sop to every geo node in a project or subnet

Recommended Posts

How could i add a sop to every geometry node inside a subnet with a complex hierarchy (multiple subnet inside subnet inside subnet etc...talking about non repeating pattern )

Merging everything  inside a single geometry to cook my modification is not an option here as i really need to keep my hierarchy paths intact for multiple software workflow and rendering purpose.

Scripting  looks like the way to go but i m not comfortable enough with it. Any idea how to tackle this specific problem ? Hscript ? Python ?

In others words :

///

For each geometry node inside a subnet.

Add a specific Sop inside  geometry nodes.

Connect this new sop to the last existing sop .

Set the display flag to this new node.

Joy.

//

Thanks for your help.

 

 

Share this post


Link to post
Share on other sites

Hi Spyro,
you'll want to use Python for any node network-related manipulation.

The relevant functions are

node.children() # Return a list of the children                        https://www.sidefx.com/docs/houdini/hom/hou/Node.html
node.createNode() # Create a node                                      https://www.sidefx.com/docs/houdini/hom/hou/Node.html
node.setFirstInput() # Connect input to specified node                 https://www.sidefx.com/docs/houdini/hom/hou/SopNode.html
node.setDisplayFlag() # Set the display flag                           https://www.sidefx.com/docs/houdini/hom/hou/SopNode.html
node.setRenderFlag() # Set the render flag                             https://www.sidefx.com/docs/houdini/hom/hou/SopNode.html
node.moveToGoodPosition() # Move the node under its connected parent   https://www.sidefx.com/docs/houdini/hom/hou/SopNode.html

The rest would be Python logic to decide what to do.

subnet = hou.node("/obj/subnet2") # Get a reference to the subnet you want to do stuff to
children = subnet.children() # Get all the children of the subnet
for child in children: # Loop over each child
    # Make sure it's a geometry node, since we can have other stuff such as nulls and whatnot
    nodeType = child.type().name()
    if nodeType == "geo":
        # Get Display node
        displayChild = child.displayNode()
        
        # Create Node, parent, and some housekeeping
        newNode = child.createNode("null")
        newNode.setFirstInput(displayChild)
        newNode.setDisplayFlag(True)
        newNode.setRenderFlag(True)
        newNode.moveToGoodPosition()

This parents the new node to the one that had its display flag set to it.
Maybe that's not what you want

# Instead of Get Display Node, here's two more options

# Get last children
# uses the Tree View order (for more info, head to the documentation)
children_1 = child.children()
lastChild = children_1[len(children_1)-1]

# Get last created node
# uses the CreationTime metadata
# for all children, get the creationTime, and get the youngest one
time = children_1[0].creationTime() # initialize time to the first node
youngestChild = children_1[0] # initialize youngestChild to the first node
for child_1 in children_1:
    time1 = child_1.creationTime()
    
    if time1 > time: # if the current node is younger
        time = time1
        youngestChild = child_1

# And with that, you'd replace setFirstInput(displayChild) with either lastChild or youngestChild

Anyways, it all depends on what exactly you want, I needed to do some guessing since we lack an example scene.

Here's a scene with an example
addSopToChildren.hipnc

P.S. I used a weird method to get the script to run when I press a "button".
There are much better ways to do it. Here's one → https://projectjulien.space/houdini-tutorials/2018/7/4/create-a-python-snippet-container-on-null-or-any-node

Edited by Alain2131
  • Thanks 1

Share this post


Link to post
Share on other sites

Fantastic job Alain you nailed it thanks for your help.

Just a little thing who's off for my context.

Using your example :

If you create more geo node inside the targeted subnet inside other subnet inside others subnet etc....it doesn t work. Only the first level childrens are taking into account.

Any idea how to tweak the first lines to get all the geo node at any level in the tree hierarchy (with subnets who have differents children depth)   ?

Still a fantastic and super clear piece of code right now thanks again.

Edited by Spyrogif

Share this post


Link to post
Share on other sites

Was simple as f*** i was just lazy.

i ve just replace 

children() tuple of hou.Node

Return a list of nodes that are children of this node.
 
by
 

allSubChildren(top_down=True, recurse_in_locked_nodes=True) → tuple of hou.Node

Recursively return all sub children of this node. For example, hou.node("/").allSubChildren() will return all the nodes in the hip file.

 

Working like a charm in the test scene. Have to test it in a massive scene now.

 Thanks again .

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×