Using multiple python files as modules

   1671   2   1
User Avatar
Member
4 posts
Joined: July 2017
Offline
So I'm working on a plugin composed of numerous python files and am struggling to get grip on the module system which is in place. I've been looking at this [www.sidefx.com] page to try and figure it out with limited success. For example I've got 2 extra files alongside the python module file which look like this:

PythonModule:
import toolutils
foo = toolutils.createModuleFromSection("foo", kwags['type'], "foo.py")

foo.py:
import hou
import toolutils

nodeType = hou.nodeType(hou.objNodeTypeCategory(), "nodeName")
bar = toolutils.createModuleFromSection("bar", nodeType, "bar.py")

bar.py
import hou

foo = hou.pwd().hdaModule().foo

However, running them I end up with an AttributeError from bar.py: ‘module’ object has no attribute ‘foo’

Why is this? does createModule only work inside the PythonModule file? is the access method in bar.py correct? I'm just a bit puzzled with the whole thing…
User Avatar
Member
1906 posts
Joined: Nov. 2006
Online
Personally I would really avoid doing this kind of thing. If you have a lot of code better organized in different modules I'd recommend just using proper python modules on disk somewhere. If they really need to be encapsulated within the asset then I'd probably bite the bullet and just put all the code in the PythonModule, no matter how unruly it may be.

Most likely the original poster's issue is that there's basically circular importing going on. The main PythonModule is "importing" foo which is attempting to "import" bar which then tries to access foo from the main module which is still in a loading operation. In their case it would have probably been better for bar to access foo by also creating it from source internally since these aren't real modules and it would avoid what's probably the issue. Unless of course there are global variable things involved in foo then you're out of luck.
Graham Thompson, Technical Artist @ Rockstar Games
User Avatar
Member
311 posts
Joined: Oct. 2016
Offline
This is for external files on disk, not an HDA Python module.

For a long time this just works. It should be no issue adding more modules.

See last post and code at bottom.

Your text to link here... [www.sidefx.com]

I use one shelf button to import or refresh. Other shelf to call function.
Edited by SWest - Nov. 3, 2022 03:53:46
Interested in character concepts, modeling, rigging, and animation. Related tool dev with Py and VEX.
  • Quick Links