Jump to content

HDA - Import Python Module from extra files


faxingberlin

Recommended Posts

Hello, I want to embed my python scripts in HDA.

So, I add test.pyc file to Extra Files Tab. And I want to import this PythonModule in Scripts Tab.

I try to this via hou.readFile("opdef:Sop/hdaname?test.pyc"), but this cannot read pyc binary file.

Is there any way to import python module in extra file?

Link to comment
Share on other sites

On 2019-07-09 at 7:04 PM, faxingberlin said:

Hello, I want to embed my python scripts in HDA.

Just copy paste your code into a PythonModule, why do you need complicate things with embedding a pyc file?

Using pyc files has some benefits, but only in certain rare circumstances. If your intent is to hide the source code...well.. is it really worth it? pyc files can be easily disassembled. If you want to confuse users so they couldn't figure out your algorithm you could use a python code obfuscator like this one: https://liftoff.github.io/pyminifier/

  • Thanks 1
Link to comment
Share on other sites

there are some ways to use external files.

one way is to load Your script into the hda in the Scripts tab. and then You can access it  with

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

you can then access the methods by

pwd().hdaModule().bar....

if I remember right, stalkerx777, explained this to me some time ago, thanks again :)

this works really well

Another way is to use the python2.7libs folder in a houdini path folder or in You houdini home directory

every python script stored there can be imported as a module

Link to comment
Share on other sites

8 hours ago, Stalkerx777 said:

Just copy paste your code into a PythonModule, why do you need complicate things with embedding a pyc file?

Using pyc files has some benefits, but only in certain rare circumstances. If your intent is to hide the source code...well.. is it really worth it? pyc files can be easily disassembled. If you want to confuse users so they couldn't figure out your algorithm you could use a python code obfuscator like this one: https://liftoff.github.io/pyminifier/

Thanks. I see the link that you attached. This is awesome too!

At this time, I want to know how to execute binary files( pyc is just a example ) using hou.readFile method not to hide them.

I know hou.readFile method read file as string. I cannot import and execute them from HDA extra files not only py and pyc.

So, I think just execute py code from string and pyc binary code from string.

I found execute pyc binary by using imp, marshal module. It works perfectly.

https://mail.python.org/pipermail/chicago/2007-August/002498.html

I just wanted to know if there was a way to do this.

I agree with the best way is just copy code to python module in HDA.

Edited by faxingberlin
Link to comment
Share on other sites

1 hour ago, sanostol said:

there are some ways to use external files.

one way is to load Your script into the hda in the Scripts tab. and then You can access it  with


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

you can then access the methods by


pwd().hdaModule().bar....

if I remember right, stalkerx777, explained this to me some time ago, thanks again :)

this works really well

Another way is to use the python2.7libs folder in a houdini path folder or in You houdini home directory

every python script stored there can be imported as a module

 

This is really good to import python module from external files. But extra files in HDA can load only using hou.readFile method.

Thank you.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...