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:
importhoudefcreate_sop_import_node():selected_node=hou.selectedNodes()[0]ifhou.selectedNodes()elseNone# Create a new SOP Import nodesop_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...
You don't need to switch to stage context. You can just create an operator there using createNode()method.
defcreate_sop_import_lop():"""Creates SOP Import node from the first node in selection."""selection=hou.selectedNodes()iflen(selection)==0:hou.ui.setStatusMessage('Nothing was selected',severity=hou.severityType.Warning)returnselected_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.
ajz3d You don't need to switch to stage context. You can just create an operator there using createNode()method.
defcreate_sop_import_lop():"""Creates SOP Import node from the first node in selection."""selection=hou.selectedNodes()iflen(selection)==0:hou.ui.setStatusMessage('Nothing was selected',severity=hou.severityType.Warning)returnselected_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.