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()
Interested in character concepts, modeling, rigging, and animation. Related tool dev with Py and VEX.