Error using hapi for a headless Houdini

   2202   4   1
User Avatar
Member
10 posts
Joined: Jan. 2023
Offline
Hi, I'm trying to connect to a headless Houdini in a Windows terminal to edit a hdalc or hiplc file using hapi. I'd like to create a new node inside the file for starters and I encounter an 'Invalid id' error (below).

I'm using hython.exe as my shell (I couldn't import hou/hapi/anything-Houdini after adding 'C:\Program Files\Side Effects Software\Houdini 19.5.569\houdini\python3.7libs' to my Windows environment path, I get "import _hou: ImportError: DLL load failed: The specified module could not be found" error). I've been following this Getting Started tutorial in https://www.sidefx.com/docs/houdini/hapi/ [www.sidefx.com]

Error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
hapi.InvalidArgumentError: Invalid argument given: Invalid id.

Can anyone explain what goes in this 'Sop/MyType' part?:
# Assuming we have a loaded an asset which defines a SOP type `MyType`
node_id = hapi.createNode(session, -1, 'Sop/MyType')

Here's my code:

import hapi
options=hapi.ThriftServerOptions() 
process_id = hapi.startThriftSocketServer(options, 9090, log_file="C:\\Users\\m.zaras\\Desktop\\headless_houdini\\log_file.txt")  
session=hapi.createThriftSocketSession('localhost',9090)
cook_options=hapi.CookOptions()
hapi.initialize(session,cook_options)
hda_bytes=open('C:\\Users\\m.zaras\\Desktop\\headless_houdini\\mydigitalasset.hdalc','rb').read()lib_id=hapi.loadAssetLibraryFromMemory(session,hda_bytes,len(hda_bytes),True)

[b]node_id=hapi.createNode(session,-1,'Sop/MyType')[/b]
node_info=hapi.getNodeInfo(session,node_id)



Is using hapi the easiest/only way to work on a HOudini file without the UI/headless?
User Avatar
Member
636 posts
Joined: June 2006
Offline
your lib path is wrong python3.7 doesn't exist in Houdini 19.5.
C:\Program Files\Side Effects Software\Houdini 19.5.569\houdini\python3.9libs

when you use hython you can import directly hou, in a python shell you need to import the lib.
hython is just houdini python shell with no UI it's the simplest way but not headless.

i dont have a hapi example but for hython i have a snippet.

Import:
try:
    import hou
except ImportError:
    print 'There is a Problem in the File: server.py please edit the path to houdini installation'
    sys.path.append("C:\\Program Files\\Side Effects Software\\Houdini 19.5.569\\houdini\\python3.9libs")
    import hou

Load a Hip File:
 hip_file = "test.hipnc"
 hou.hipFile.load(hip_file)
after that you can access the nodes like you would be inside Houdini in the Python Shell.

i haven't found a standalone lib or pip command of hapi.py outside the python libs folder but i think if you want to have a headless setup you need only the 3 files (hapi.exp, hapi.lib, hapi.pyd) in the houdini/python3.9libs folder in your custom python project.
User Avatar
Member
10 posts
Joined: Jan. 2023
Offline
Hi @mandrake0, thanks for your reply.

your lib path is wrong python3.7 doesn't exist in Houdini 19.5.
C:\Program Files\Side Effects Software\Houdini 19.5.569\houdini\python3.9libs
I have Houdini 19.5.569 and I only have python3.7libs in this directory

I followed your example and I get this error when I run it in powershell with: python "path_to_script.py" :

Traceback (most recent call last):
  File "C:\Users\<myusername>\Desktop\script_to_run_in_hython.py", line 8, in <module>
    import hou
  File "C:\Program Files\Side Effects Software\Houdini 19.5.569\houdini\python3.7libs\hou.py", line 28, in <module>
    import _hou
ImportError: DLL load failed while importing _hou: the specified module could not be found.


I ended up running hython.exe instead of powershell since it's got hou already integrated. But I'd love to learn how to manage to install hou with python not on hython.
User Avatar
Member
636 posts
Joined: June 2006
Offline
Sorry i haven't seen that there exist a Python 3.7 Build.

Found a Article about it:
https://www.sidefx.com/docs/houdini/hom/commandline.html [www.sidefx.com]

This should work with a normal Python Runtime.

import os, sys

# import Python Libs and add Path for the Dll Files 
sys.path.append("C:\\Program Files\\Side Effects Software\\Houdini 19.5.569\\houdini\\python3.7libs")
os.add_dll_directory("C:\\Program Files\\Side Effects Software\\Houdini 19.5.569\\bin")

import hou

# Testing, just change the file path 
hou.hipFile.load("c:\\temp\\test.hiplc")
n = hou.node('/obj/box')
# Scale Value is set to 0.123
print(n.parm("scale").eval())

Attachments:
powershell.png (43.2 KB)
test.hiplc (132.1 KB)

User Avatar
Member
10 posts
Joined: Jan. 2023
Offline
Thanks mandrake0, it worked! Turns out os.add_dll_directory is only available in Python 3.8 and above, so I reinstalled my Houdini from version py 37 to default (with python 3.9), installed python 3.9.1 and entered python shell with py -3.9 to avoid conflicts with Houdini python version.

Dancing banana gif tax [tenor.com]
  • Quick Links