Export and import node sets (help wanted)

   356   1   0
User Avatar
Member
311 posts
Joined: Oct. 2016
Offline
Hi,

Here's a prototype of something that works on Linux (with dependencies met). It need some updates to work on all platforms and versions, but I'll leave that to the Houdini community. Installation instructions are in the top of the code.

Shelf tool 1:

from pathlib import Path


# set some variables
path_user_home = str(Path.home())
network_snippets_path = path_user_home+'' # set the suffix '' as desired


def loadPyNode():
    '''Import node from py. See saveNodesAsPy. To install: For example, add this code to a shelf and add a hotkey (for example: Ctrl+Shift+V). Then run it from the Network Pane. Remember to follow the EULA of the software (SWest).'''

    kwargs = {'node': '', 'parm': '', 'script_multiparm_index': '-1', 'script_value0': '0', 'script_value': '0', 'parm_name': '', 'script_multiparm_nesting': '0', 'script_parm': ''}
    path_and_filename = hou.ui.selectFile(start_directory=network_snippets_path,pattern="*.py *.png *.jpg",title="Load node as Python")
    if path_and_filename.endswith(".png"):
        path_and_filename = path_and_filename.replace(".png",".py")    
    if path_and_filename.endswith(".jpg"):
        path_and_filename = path_and_filename.replace(".jpg",".py")    
    if not (path_and_filename == ""):
        exec(open(path_and_filename).read())
      
loadPyNode()

Shelf tool 2:

import os
from pathlib import Path


# set some variables
path_user_home = str(Path.home())
network_snippets_path = path_user_home+'' # set the suffix '' as desired


def saveNodesAsPy():
    '''Save a single or a set of nodes as Python code including parameter settings to storage medium. 
    The code is generalized so it can be imported to any compatible node parents. Use regular folders to organize nodes. This function support the creation of an optional thumbnail file that is also supported by the equivalent load function. This tool extends the existing functionality of the shelf. To install: For example, add this code to a shelf and add a hotkey (for example: Ctrl+Shift+C). Then run it from the Network Editor. Remember to follow the EULA of the software (SWest).'''

    import toolutils

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

    
    # a selection should look similar to this:
    # /obj/char_workflow/char_rig /obj/char_workflow/OUT /obj/char_workflow/null1
    selection = hou.selectedNodes()
    nodepath_or_list = []
    for node in selection:
        nodepath_or_list.append(node.path())

    # print(f'selection: {selection}')
    # print(f'nodepath_or_list: {nodepath_or_list}')

    tool_script_for_node = toolutils.generateToolScriptForNode(nodepath_or_list)            
    node_path = str(selection[0].path())
    node_name = str(selection[0].name())
    node_type = getNodeType(node_path)
    title = 'Save node as Python - '+node_path
    default_value = node_type+"_"+node_name
    pattern="*.py *.png *.jpg"
    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(tool_script_for_node)
        node_file.close()
        # show message: press ok to capture a screenshot        
        user_choice = hou.ui.displayMessage("Press Ok to take a screen shot.", buttons=('Ok', 'Cancel'))
        # user_choice 0 (Ok); user_choice 1 (Cancel)
        if user_choice == 0:
            # set filename
            path_and_filename = path_and_filename.replace(".py",".png")
            os.system("xfce4-screenshooter -r -s '"+path_and_filename+"'") # use same path
            # resize the image
            from PIL import Image
            basewidth = 64
            img = Image.open(path_and_filename)
            wpercent = (basewidth/float(img.size[0]))
            hsize = int((float(img.size[1])*float(wpercent)))
            img = img.resize((basewidth,hsize), Image.Resampling.LANCZOS)
            # save thumbnail image
            img.save(path_and_filename)

            
saveNodesAsPy()
Edited by SWest - April 18, 2024 14:51:58

Attachments:
output.gif (1.3 MB)

Interested in character concepts, modeling, rigging, and animation. Related tool dev with Py and VEX.
User Avatar
Member
193 posts
Joined: Dec. 2016
Offline
Did you know you can save a subnet to a gallery ?
  • Quick Links