Jump to content
sant0s81

[Solved] External python script in HDA modules

Recommended Posts

Posted (edited)

Hello,

I once saw a tutorial that explained, how to import another python script inside a HDA - but I dont find it anymore.

On the screenshot I try to explain what I mean.

I am not sure anymore if it was import xyz from xyz or something like that.

Could someone give me a hint? :)

Thanks alot,
sant0s

import_external_file.thumb.jpg.1ecc46f207e92ae46fa094ef50976547.jpg

 

 

Edited by sant0s81

Share this post


Link to post
Share on other sites

To import a specific script in Python you need to append the folder where it is to the python path (the path contains all the paths to the different modules it loads). You can do it using the sys module :

import sys

sys.path.append("/path/to/the/folder/where/my/script/is/myscript.py")

import myscript

And if you want to import just a specific function from your file and not all the file :

import sys

sys.path.append("/path/to/the/folder/where/my/script/is/myscript.py")

from myscript import myfunction 

 

Cheers,

  • Like 2

Share this post


Link to post
Share on other sites

If I understand correctly this question: How import into the PythonModule a Custom Script stored in HDA ?

Like your screenshot, to import all functions stored into "import_that_script" inside the PythonModule.

There is an answer in docs: https://www.sidefx.com/docs/houdini/hom/hou/HDAModule.html

Quote

 

If you find that a digital asset has too much Python code to store in one module, it’s possible to create submodules. For example, if you want to create a submodule named bar, put its source code in a new digital asset section (say, "bar_PythonModule"). Then, from the Python Module section, you can write the following:


import toolutils
bar = toolutils.createModuleFromSection("bar", kwargs["type"], "bar_PythonModule")


 

So in your case in the PythonModule section:

import toolutils
myscript = toolutils.createModuleFromSection("myscript", kwargs["type"], "import_that_script")

Then, you can execute a function as imported module do: myscript.myfunction()

  • Like 1

Share this post


Link to post
Share on other sites

@fsimerey

Great, thats actually what I was trying - but than stored the script in a file and used the technique mentioned above.

But yes, thats what I was thinking.

Thx! :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×