I've put together this multi-module script, which lives in a make_Shaders folder, which is also the name of the module that makes the shaders: make_Shaders.py. I'm using this code in the __init__.py to load (import) the modules:
# 'borrowed' from Antonioya - elegant solution!
path = sys.path
flag = False
for item in path:
if "make_Shaders" in item:
flag = True
if flag is False:
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'make_Shaders'))
print("make_Shaders folder added to pythonpath")
if "bpy" in locals():
import imp
imp.reload(figure_Defs)
imp.reload(make_Shaders)
print("make_Shaders: Reloaded multifiles")
else:
import figure_Defs
import make_Shaders
print("make_Shaders: Imported multifiles")
When I zip and install this in a brand-new .blend (just opened Blender), I get the following error when I try to tick the 'Activate' tickmark:
addon_utils.disable: make_Shaders not disabled.
Modules Installed from '/home/robyn/Documents/Blender/Projects/AllScripts/make_Shaders.zip' into '/home/robyn/.config/blender/2.76/scripts/addons' (make_Shaders)
addon_utils.disable: code_autocomplete not disabled.
make_Shaders folder added to pythonpath
Traceback (most recent call last):
File "/home/robyn/blender276/2.76/scripts/modules/addon_utils.py", line 324, in enable
mod = __import__(module_name)
File "/home/robyn/.config/blender/2.76/scripts/addons/make_Shaders/__init__.py", line 61, in <module>
imp.reload(figure_Defs)
NameError: name 'figure_Defs' is not defined
addon_utils.disable: make_Shaders not disabled.
Line 61 is the last statement in this segment:
if "bpy" in locals():
import imp
imp.reload(figure_Defs)
It seems "bpy" is in locals() straight away, when first starting Blender, which this says wouldn't happen:
This code works as follows.
If __init__.py() is run for the first time, i.e. on starting Blender with the addon enabled in your default.blend, "bpy" in locals() is False. The other files in the package are imported, and Imported multifiles is printed in the terminal. Thank you for considering my little dilemma.
Edited to Add
I double-checked case and spelling of the modules: both correct.
import bpy
before or after the code shown above, secondly when importing usefrom . import make_Shaders
and lastly why do you need to add to the sys.path ? The addon registration will do this for you. Once it is enabled you canfrom make_Shaders.make_Shaders import xxxx
for example. Also can I suggest all lower case for make_shaders and camel case for class namesclass MakeShaders
$\endgroup$import bpy
is before... it should be after. I will change tofrom . import make_shaders
(changing case to lower) as you suggest. The only bit I still don't get is this: "Once it is enabled you canfrom make_Shaders.make_Shaders import xxxx
" - I thought I was limited as to where I can issue an 'import' statement, as in, not in a function. $\endgroup$try - except
in the execute(). $\endgroup$