How to create referance to self in the python script node

   4216   7   0
User Avatar
Member
24 posts
Joined: July 2017
Offline
Hey guys!
I've been fooling around with TOPS over the last week and in doing so I've encountered an issue with the python script node. Due note that this is (almost) 100% due to my lack of experience with python as a coding language.

I am having an issue creating a reference to the current python script node running. When making a python node in SOP I am sued to being able to run hou.node('.') or hou.pwd() to get a reference to the current python script node and from there being able to get to the parent node or any other node in the node tree and their parameters. However if I run hou.node('.') or hou.pwd() all I get in TOPS is a simple ‘/’ when I print it instead of the expected path to the node.

I've noticed that in the python script node there is a built in variable ‘self’ but I have not been able to get that to give me any useful results or use it with any of the hou.Node.TOPNode specific commands in the documentation.

Is there something really obvious I am missing? I noticed that there was multiple python nodes, would a different one be better? All I am looking to do is execute a few lines of python after a cache is done running.

Thanks for the read!
User Avatar
Staff
585 posts
Joined: May 2014
Offline
Which version of Houdini are you using?

In a Python Script node, you should be able to run hou.node('.'), assuming your Python Script is configured to “Evaluate In Process”. If it's not, the script is run in a separate child process and won't have access to anything in the .hip file.

The self variable is a reference to the underlying PDG node. It gives you a reference to one of these: https://www.sidefx.com/docs/houdini/tops/pdg/Node.html. [www.sidefx.com] The work_item variable gives you access to the work item that is evaluating: https://www.sidefx.com/docs/houdini/tops/pdg/WorkItem.html [www.sidefx.com]

Each TOP node owns a single PDG node, and it's the PDG nodes that are actually doing most of the work when the graph is evaluating. For example, parameters should be evaluated through the PDG node since it configures a context that ensures the correct local state is available:

value = self['myparm'].evaluateInt(work_item)
work_item.setIntAttrib('example_data', value)
User Avatar
Member
24 posts
Joined: July 2017
Offline
Hi tpetrick, thanks for the reply! And thanks for the tips on how I could use the self and work-item variables!

I'm currently running Houdini 17.5 (been working with a few sub-versions but the screenshots below are from 17.5.327).

So first of all a thing I forgot to add to the original question, is it normal to have to import the hou module into the TOPS python script node? Because if I don't I get the error “name hou is not defined”, whilst in SOP, OBJ and the python shell this is never needed, it automatically has the hou module available.

Over to the issue at hand, I do indeed have the ‘Evaluate in Process’checkbox checked. However when I try to print the result of storing a hou.node('.') call in a variable, all I get shown in the console is a simple ‘/’ as if it did not find anything.

See the images attached.
Edited by underscoreus - March 16, 2020 22:25:28

Attachments:
TOPS_Issue_01.jpg (223.2 KB)
TOPS_Issue_02.jpg (199.6 KB)

User Avatar
Staff
585 posts
Joined: May 2014
Offline
Yes, you need to import hou manually. This is partly because the Python Script can be configured to run its work items out of process using Python, possibly even on the farm with a machine that doesn't have Houdini installed. Also, the script code is running inside of PDG's evaluation context which also doesn't import hou.

It looks like the change to make hou.pwd and hou.node work properly from the script was only made in 18.0, and not backported to 17.5. I can look into backporting those changes, but in the mean time you should be able to use the following to get a reference to the TOP node. When create from TOPS, a PDG node will have it's parent TOP node id set as its customId:

import hou
node = hou.nodeBySessionId(self.customId)
print(node)
User Avatar
Member
24 posts
Joined: July 2017
Offline
Ah! Thank you so much tpetrick!

Works like a charm!
User Avatar
Staff
585 posts
Joined: May 2014
Offline
I looked into this further and the fix so that hou.pwd() work is actually available in 17.5.350 and newer.
User Avatar
Member
2 posts
Joined: Sept. 2023
Offline
Hey, just found this as I have similar issues. I have no issues running the python node "generate" or in "in-process" however "out-of-process" and "services" are not working for me.
The error I'm getting is either:
"NameError: name 'self' is not defined" which is by running the code provided by @tpetric:
import hou
node = hou.nodeBySessionId(self.customId)
print(node)

This code is also not giving me the result I was expecting:

import hou
node = hou.pwd().parent()
print(node)

print:
Running Houdini 20.0.547 with PID 13732
None


Which explains why hou.parm("name_of_ui_element").eval()
is not working.


Any suggestions to why this is not working?

Thanks
User Avatar
Staff
585 posts
Joined: May 2014
Offline
When running out of process or using services, the script runs in a separate process that doesn't have access to the original .hip file. There are no nodes or scene at all -- just the script code and the work item data.
  • Quick Links