How to access the asset node instance from PythonModule?

   2806   5   0
User Avatar
Member
105 posts
Joined: 11月 2007
Offline
What is the correct way to access the current instance of the asset from within the asset's PythonModule?

I have tried hou.pwd() and hou.node(“.”) but these both return the root “/”.
kwargs just returns the asset type so that's no use.

The only reliable way I can find to get this reference into the PythonModule is to pass it through from anything that calls it but this is extremely annoying and leads to a lot of duplicate code. I feel that I must be doing it wrong.
User Avatar
Member
201 posts
Joined: 7月 2015
Offline
You can use the kwargs dictionary.

If you want the node it is
kwargs["node"]

If call a function from a parameter and need access to the parameter object you can do this IIRC
kwargs["parm"]

Depending on where you call your code from the kwargs dict gives you lot's of additional help.
Manuel Köster - Senior Technical Artist @Remedy Entertainment

https://www.remedygames.com/ [www.remedygames.com]
http://shadesoforange.de/ [shadesoforange.de]
https://twitter.com/ShadesOfOrange_ [twitter.com]
User Avatar
Member
105 posts
Joined: 11月 2007
Offline
Thanks Manuel.

Unfortunately, the
kwargs["node"]
key is only available in the “Event” callbacks (OnLoaded, OnCreated etc.), not in the PythonModule itself. The kwargs dictionary for the PythonModule only contains a type key which is the problem. The only way I can see to get the node instance from there is to pass it in.
User Avatar
Member
201 posts
Joined: 7月 2015
Offline
From where are you trying to call the PythonModule?

Of the top of my head I don't know a usecase where you want to access the node object, but don't call the PythonModule from a place where you don't have access to kwargs. It's also available on parameter callback scripts, action button scripts as well as menu scripts.
Manuel Köster - Senior Technical Artist @Remedy Entertainment

https://www.remedygames.com/ [www.remedygames.com]
http://shadesoforange.de/ [shadesoforange.de]
https://twitter.com/ShadesOfOrange_ [twitter.com]
User Avatar
Member
1908 posts
Joined: 11月 2006
Offline
You should always pass in a node to any code in the PythonModule which requires a node. The PythonModule is nothing special and really has no concept of Houdini or the node it belongs to. At most it is aware of the node type that it belongs to (kwargs in the main namespace).

The fact that you can sometimes call hou.pwd() within it an get the instance of the node you expect is essentially up to luck with the context the code is calling from. Essentially you should treat any code in the PythonModule just like an external module that has nothing to do with a node.
Graham Thompson, Technical Artist @ Rockstar Games
User Avatar
Member
105 posts
Joined: 11月 2007
Offline
Ok. Thanks guys. This is clearer now. I've re-written my asset code accordingly. I've also started using the hou.session to hold modules that are a bit more complex which is working well.
  • Quick Links