How to load different instances of HOU module?

   1862   4   2
User Avatar
Member
21 posts
Joined: 4月 2012
Offline
Hi there,

I'm importing the hou module into a custom python script and I would like to have multiple scenes opened at the same time. This is how it is implemented:

import sys
from importlib import reload
sys.path.append(HOUDINI_PATH)
try:
    import hou
except ImportError:
    print('houdini.py: There was a problem loading the hou module')

box_hip = hou.hipFile
box_scene = box_hip.load(path/to/box_scene.hipnc)
print(box_hip.name()) # ==> box_scene.hipnc CORRECT

reload(hou)
sphere_hip = hou.hipFile
sphere_scene = sphere_hip.load(path/to/sphere_scene.hipnc)
print(sphere_hip.name()) # ==> sphere_scene.hipnc CORRECT

# Let's print the name of box_hip again
print(box_hip.name()) # ==> sphere_scene.hipnc WRONG

As you can see, even though I've reloaded the hou module, it's still using the same hou instance for both hip files and therefore always returning the data of the latest loaded scene.

Is there a way to have different instances of the hou module so I can load and work on multiple different hip files in the same script? I've also tried to assign hou to different variables with no luck.


Any help would be highly appreciated, thanks in advance!
Edited by leota - 2020年7月13日 06:14:35
User Avatar
Member
30 posts
Joined: 5月 2017
Offline
Weird. What would happen if you reloaded hou again after assigning the sphere. Would it clear the assignments from before?

How are you using this script? Is there any way you could leverage PDG to have multiple script instances running and sort of work around this issue in a way? What's your particular use-case for having multiple hips in the same script?

Otherwise, did you think about the workaround of launching a second instance of the script in another instance automatically? Not sure how to apply it, depends on the use-case.
- Martin Egger
Student at Filmakademie BW
& Freelance FX TD
User Avatar
Member
21 posts
Joined: 4月 2012
Offline
What would happen if you reloaded hou again after assigning the sphere. Would it clear the assignments from before?
It would still print out data from the sphere_scene, so it doesn't clear anything but instead keeps in memory the latest loaded file.

How are you using this script?
I have extracted the problem from my bigger solution, so at the moment is a simple script which I run this way:
hython myscript.py

What's your particular use-case for having multiple hips in the same script?
The particular use-case is a REST API web server that returns data about a specific scene file based on a given HTTP request.
For example:
GET /api/projects/sphere ==> will return info about sphere_scene
GET /api/projects/box ==> will return info about box_scene
The problem is that I'm trying to reduce the response time, so reloading the scene file for every request just adds a lot of latency.
I work-around that I've thought is to merge all the scenes into a single project file, but doesn't sound ideal since it would be hard to maintain it with a large number of files.

Is there any way you could leverage PDG to have multiple script instances running and sort of work around this issue in a way?
I've never used PDG before, I was just reading something about to figure out how it could solve my issue. It would be great if you could provide an hint/example on how you would solve that with PDG.

Thanks a lot!
User Avatar
Member
30 posts
Joined: 5月 2017
Offline
Phew, I must say that is a bit beyond my reach, but you're probably best served researching PDG a bit more. I'm still learning it myself, but the help is quite a good starting point. Again it's quite like starting from scratch in Houdini, so best start with some videos of what people built like here (Concerning attribute handling): https://www.youtube.com/watch?v=L51DUKjrkIo [www.youtube.com]
They do have functionality for “python servers” (or “command chains”), which are mystical beings beyond my current understanding that could be used for such things from what I grasp, haha.

And here's the examples that are always great to learn:
http://127.0.0.1:48626/examples/nodes/top/index.html [127.0.0.1]
Or in the web-view (unable to load into Houdini, so start the first instance with clicking on the help button anywhere in Houdini once)
https://www.sidefx.com/docs/houdini/examples/nodes/top/index.html [www.sidefx.com]
There is one example on the python command chain. But the resources on this stuff are rather limited, it might require a lot of fiddling around. But maybe it's the way to go!
- Martin Egger
Student at Filmakademie BW
& Freelance FX TD
User Avatar
Member
21 posts
Joined: 4月 2012
Offline
Thanks for your help anyway! The resources you linked are definitely helpful, I'll have a deeper read about TOPs.
  • Quick Links