Printing Node as code in Houdini

   2806   3   1
User Avatar
Member
5 posts
Joined: Feb. 2023
Offline
Hello, I recently stumbled upon a super old forum post describing a way to print a specific node as code in Houdini using the python shell editor.

the line was as follows:

node = hou.node('/obj/geo1/resample1')
print node.asCode()

(I am trying to see the inner workings of the resample node)

However when this is ran it just returns a syntax error:



I am quite new to python and I am unsure on what the issue is / if this even works anymore.

Any help or alternative solutions would be amazing.

Thanks!

Attachments:
Screenshot 2023-10-05 145040.png (10.7 KB)

User Avatar
Member
64 posts
Joined: April 2022
Offline
in python 3.9 you must use print(value) instead of print value
alexeyvanzhula.gumroad.com
User Avatar
Member
313 posts
Joined: Oct. 2016
Offline
Hi,

I’ve been doing this for years.

However, what you see is not the inner workings. It is just the python definitions for what node it is and parms.

That said, it is very good to know these things if you want to use Python.

However, SideFX has added additional tools when you drag and drop to the shelf. I suggest you 100% to just use that.

Cheers!




Don't use this, because you will get trouble. Use the shelf because it solves those things.
Maybe the code gives some error, but if so, just fix them and it should work. It should then be network_snippets_path maybe.
As I wrote, it just saves the node parms and whatever you can place there. Other data is basically forgotten.
from pathlib import Path


path_user_home = str(Path.home())
network_snippets_path = path_user_home+"/"



def getNodeType(node_path):
    node = hou.node(node_path)
    type = str(node.type().name())
    return type


def saveNodesAsPy():
    for node in hou.selectedNodes():
        if (node == None):
            hou.ui.displayMessage("Please select a node first.")
        else:
            node_path = str(node.path())
            node_name = str(node.name())
            node_type = getNodeType(node_path)
            title = 'Save node as Python - '+node_path
            default_value = node_type+"_"+node_name
            pattern="*.py"
            path_and_filename = hou.ui.selectFile(start_directory=network_snippets_path,pattern=pattern,title=title,default_value=default_value)
            if (path_and_filename == ""):
                hou.ui.displayMessage("Filename is empty. Please try again.")
            else:
                if not path_and_filename.endswith(".py"):
                    path_and_filename += ".py"
                node_file = open(path_and_filename, "w")
                node_file.write(node.asCode(False, True))
                node_file.close()
                
saveNodesAsPy()




from pathlib import Path

path_user_home = str(Path.home())
network_snippets_path = path_user_home+"/"


def getNodeType(node_path):
    node = hou.node(node_path)
    type = str(node.type().name())
    return type


def loadPyNode():
    '''Import node from py'''
    path_and_filename = hou.ui.selectFile(start_directory=network_snippets_path,pattern="*.py",title="Load node as Python")
    if not (path_and_filename == ""):
        exec(open(path_and_filename).read())

loadPyNode()
Edited by SWest - Oct. 5, 2023 16:20:01
Interested in character concepts, modeling, rigging, and animation. Related tool dev with Py and VEX.
User Avatar
Member
5 posts
Joined: Feb. 2023
Offline
Thanks so much for such an in depth response!

Sorry it took me so long to see this reply.

Much appreciated!
  • Quick Links