Programmatically Create SOP Import Node using Selected Node

   603   2   1
User Avatar
Member
129 posts
Joined: 9月 2021
Offline
I'm trying to make a shelf tool that allows a user to select a node in a SOP network and then automatically creates a SOP import node in the stage context (/stage). The issue I've had with doing this programmatically is switching to the stage context to create this node. Any ideas on how to do this?

This is kind of the outline of the script:

import hou

def create_sop_import_node():
    selected_node = hou.selectedNodes()[0] if hou.selectedNodes() else None

    # Create a new SOP Import node
    sop_import_node = sop_network.createNode("sopimport") # The issue here is that a sopimport node can only be created in the /stage context, and this shelf tool is called in the SOP context.

    # Proceed to set the sop path to be the selected node...

Any help would be appreciated!

Thanks!
Anson
User Avatar
Member
477 posts
Joined: 8月 2014
Offline
You don't need to switch to stage context. You can just create an operator there using createNode()method.

def create_sop_import_lop():
    """Creates SOP Import node from the first node in selection."""
    selection = hou.selectedNodes()
    if len(selection) == 0:
        hou.ui.setStatusMessage(
            'Nothing was selected',
            severity=hou.severityType.Warning
        )
        return
    selected_node = selection[0]
    lop_node = hou.node('/stage').createNode('sopimport')
    lop_node.parm('soppath').set(selected_node.path())


create_sop_import_lop()

If you wish to use relative paths instead of absolute in the soppathparameter, look up hou.Node.relativePathTo()method.
Edited by ajz3d - 2023年9月1日 15:54:07
User Avatar
Member
129 posts
Joined: 9月 2021
Offline
ajz3d
You don't need to switch to stage context. You can just create an operator there using createNode()method.

def create_sop_import_lop():
    """Creates SOP Import node from the first node in selection."""
    selection = hou.selectedNodes()
    if len(selection) == 0:
        hou.ui.setStatusMessage(
            'Nothing was selected',
            severity=hou.severityType.Warning
        )
        return
    selected_node = selection[0]
    lop_node = hou.node('/stage').createNode('sopimport')
    lop_node.parm('soppath').set(selected_node.path())


create_sop_import_lop()

If wish to use relative paths instead of absolute in the soppathparameter, look up hou.Node.relativePathTo()method.

Oh wow, this works great! I appreciate it.
  • Quick Links