Learn to run Houdini's Python interface (hou module) in external Python scripts

0. Target

Learn to use hou in external Python scripts (not inside the Houdini editor) .

Mainly refer to the [Accessing hou from a Regular Python Shell] section in the official Houdini document Command-Line Scripting . I record the main points below:

1. Add Houdini's dll to the search path

In order to use the hou module, the Houdini dll must be linked against.
To do this, you can add the "bin folder of the Houdini installation path" to the environment variable Path:
insert image description here

Official documents also have os.add_dll_directorya way to add paths to scripts. Unfortunately, the python version I am using is not yet supported (can be judged by the return value of hasattr(os, “add_dll_directory”)).

2. Add the path of the python file of the hou module

According to the installed version of Houdini and the version of Python used, this path is in $HFS/houdini/pythonX.Ylibs
For example, here is: C:/Program Files/Side Effects Software/Houdini 19.5.303/houdini/python3.7libs
insert image description here

Therefore, import houbefore, you need to use sys.path.appendthis path to join.

At import houthe time, Python will find the Houdini library and create a blank Session.

3. Test

The test script is as follows:

import sys
sys.path.append("C:/Program Files/Side Effects Software/Houdini 19.5.303/houdini/python3.7libs")
import hou

#测试使用hou模块:
obj = hou.node("/obj")
obj.createNode("geo", "foo")
hou.hipFile.save("D:/Temp/MyTest0708.hip")

Then run it on the command line, e.g.

C:/python-3.7.9-embed-amd64/python.exe D:/Temp/Test0708.py

This will save a D:/Temp/MyTest0708.hip file.

Summarize

  • Houdini's dll needs to be added to the search path, which can be solved by adding the environment variable Path.
  • import houBefore, you needed to make sure the path to the module could be found, otherwise use sys.path.appendjoin.
  • At import houthe time, Python will find the Houdini library and create a blank Session.

Other problem records

In the test script of the official document, there is also getdlopenflagsrelated logic.
Specifically, when starting with:

    # Importing hou will load Houdini's libraries and initialize Houdini.
    # This will cause Houdini to load any HDK extensions written in C++.
    # These extensions need to link against Houdini's libraries,
    # so the symbols from Houdini's libraries must be visible to other
    # libraries that Houdini loads.  To make the symbols visible, we add the
    # RTLD_GLOBAL dlopen flag.
    if hasattr(sys, "setdlopenflags"):
        old_dlopen_flags = sys.getdlopenflags()
        sys.setdlopenflags(old_dlopen_flags | os.RTLD_GLOBAL)

When it ends:

    # Reset dlopen flags back to their original value.
    if hasattr(sys, "setdlopenflags"):
        sys.setdlopenflags(old_dlopen_flags)

I haven't done this operation here, so I don't know if it will affect it in the future.

Guess you like

Origin blog.csdn.net/u013412391/article/details/131609298