HDA python global variables

   3335   5   3
User Avatar
Member
277 posts
Joined: July 2006
Offline
Hi!

I'm trying to stroe some informations in HDA, so when user for examplepaint, information from position of his mouse will be stored, so later I could use it.

Is it possible to create in HDA (in hdaModule?) global variables, that I could use always from this HDA? (like array of floats)

thank you
User Avatar
Member
1904 posts
Joined: Nov. 2006
Offline
Currently there's no real great way of doing this. Whenever I want to store data associated with a particular node instance I use one or more hidden string parameters. I thought I talked about it in another post on the forums but I can't seem to find it.

To store persistent data with a node I make use of pickling my data using cPickle. This basically takes a data structure (list, tuple, dictionary, list of lists, etc) and turns it into a string representation that can be read and used to recreate the original data structure.

If I had a list of something I wanted to store for another cook or whatever I'd do this.
import cPickle
mylist =
node = hou.node(“some node I want to attach data too”)
data_parm = node.parm(“hidden_data”) # hidden string parameter

# to store mylist
data_string = cPickle.dumps(mylist)
data_parm.set(data_string)
# to extract
data_string = data_parm.evalAsString()
data = cPickle.loads(data_string)

As long as your data type supports picking you can do this. It's pretty simple and very useful. Since the data is kept as a parameter value it gets saved properly with the scene and if you copy/paste the node it comes along for the ride.
Graham Thompson, Technical Artist @ Rockstar Games
User Avatar
Member
277 posts
Joined: July 2006
Offline
graham Thank you very very very much!
That is briliant!
User Avatar
Member
31 posts
Joined: Sept. 2019
Offline
Wuf, old topic! Maybe there's a nice and Pythonic way of solving this 12 years later, but I just used this technique with one slight change. I didn't want to install a new Python library so I used "ast" instead. To "unpack" the string I just used "ast.literal_eval(x)".
Houdini Technical Artist @ Sharkmob
User Avatar
Member
1904 posts
Joined: Nov. 2006
Offline
Good times. I think this original post preceeded the ability to stash data on hou.Node instances. There's definitely different ways you could do this now:

As a direct replacement for using a parameter, you can now more easily take your binary data string and use the Data Blocks added in recent versions. This basically allows you attach the binary string directly to the node instance that will persist sessions and would also be brought along if you copy the node.
>>> some_data = range(10)
>>> data_str = pickle.dumps(some_data)
>>> hou.node('/obj/subnet1').setDataBlock("my_range", data_str, "")
>>> some_data == pickle.loads(hou.node('/obj/subnet1').dataBlock("my_range"))
True

If you just need your data available in the current session and don't need it to persist you can of course now just take your Python object and assign it directly as cached user data on the hou.Node instance. You could also stash the above string as standard user data as well.

Another alternative would be to use the Data parameter type and encode your data in geometry and then attach it to a node that way. If your data is a simple key/value dict you could also use the Key/Value parameter type and be able to visualize/edit it in the interface.
Graham Thompson, Technical Artist @ Rockstar Games
User Avatar
Member
31 posts
Joined: Sept. 2019
Offline
Love it! Thank you for sharing. Really nice with a non hacky way.
Houdini Technical Artist @ Sharkmob
  • Quick Links