lately I have been testing solutions to build a python tool that allows me to quickly save node networks to a python file and being able to choose presets on disk and execute them, similar to dragging node constellations into the Shelf.
So far I it worked pretty good by using the hou.Node.asCode() method, but this quickly breaks down when using Attribute Vops As far as I understand, by dragging nodes into the shelf, it generates some hscript code (Shown in the picture below).
My question: Is there a way to generate this hscript code with python, so kind of what dragging nodes into the shelf does, but with code myself, so I can use it to save snippets to disk myself?
Yes, exactly! Thanks Does that also keep intact node connections like dragging into the shelf does?? (If I run over multiple nodes) Or can I do something like this
I found a way for myself in case anyone is interested. Of course I need to work on creating an UI for this, custom file naming and loading etc. I'm saving a dictionary at the start of the text file for safe renaming like this:
importhounodes=hou.selectedNodes()names=[]forninnodes:names.append(n.name())curP=nodes[0].parent()# Create duplicatesnew_nodes=hou.copyNodesTo(nodes,curP)definition=[]# Make dictionary and rename nodes to temporary namesfornum,newinenumerate(new_nodes):new_def={}old_name=names[num]new.setName("CPPS_NODE{}".format(num))new_def["temp_name"]="CPPS_NODE{}".format(num)new_def["name"]=old_namedefinition.append(new_def)definition_str=str(definition)print(definition_str)# Create multiuple node string for hscript commandnode_string=""fornodeinnew_nodes:path=node.path()node_string+=" "+pathhs=hou.hscript("opscript -b -r -s{}".format(node_string))# Delete temp nodesfordinnew_nodes:d.destroy()# Add dictionary to top linefinal_output=definition_str+"\n\n"forhinhs:final_output+=hfinal_output+=" "file=open("C:/Users/Joshi/Desktop/Preset-Save/test.txt","w")file.write(final_output)file.close()
Preset Loading:
importsysimporttoolutilsimporthouimportjsonh_extra_args=''pane=toolutils.activePane(kwargs)ifnotisinstance(pane,hou.NetworkEditor):pane=hou.ui.paneTabOfType(hou.paneTabType.NetworkEditor)ifpaneisNone:hou.ui.displayMessage('Cannot create node: cannot find any network pane')sys.exit(0)pane_node=pane.pwd()child_type=pane_node.childTypeCategory().nodeTypes()if'attribvop'notinchild_type:hou.ui.displayMessage('Cannot create node: incompatible pane network type')sys.exit(0)pane_node.setSelected(False,True)h_path=pane_node.path()h_preamble='set arg1 = "'+h_path+'"\n'h_cmd=""withopen("C:/Users/Joshi/Desktop/Preset-Save/test.txt","r")asfile:temp=file.read()# Filter first line to be json dictionarydict_str=""fornum,linenumerate(temp.splitlines()):ifnum==0:dict_str=l.replace("'",'"')else:if"opcf "inl:l="opcf $arg1"h_cmd+=l+"\n"d=json.loads(dict_str)hou.hscript(h_preamble+h_extra_args+h_cmd)# Rename nodes to fit name from dictionaryforitemind:temp_name=item["temp_name"]name=item["name"]node=hou.node("{}/{}".format(pane_node.path(),temp_name))node.setName(name,True)node.setSelected(True)