If “bpy” in locals(): evals to True on first run

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.

• Is import bpy before or after the code shown above, secondly when importing use from . 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 can from make_Shaders.make_Shaders import xxxx for example. Also can I suggest all lower case for make_shaders and camel case for class names class MakeShaders – batFINGER Nov 16 '15 at 2:35
• Is my face red! The import bpy is before... it should be after. I will change to from . 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 can from 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. – RobinsSea Nov 16 '15 at 11:01
• Oh, and as to the why am I adding to the syspath? got that from Antonioya, and it seemed like a good idea that the time, particularly since I've been having such dramas with paths. Finally resolved the path problem in that other thread by using a csv to store the path to the images. This works in both Linux and Windows: the users put in the path themselves. Probably not ideal, of course, and it's going to be a nightmare to error-trap... perhaps a try - except in the execute(). – RobinsSea Nov 16 '15 at 11:13

1 Answer

Following batFINGER's sage advice, I've done the following. I think I get the from make_Shaders.make_Shaders import xxxx now. I haven't seen this syntax exactly anywhere, but it works an absolute treat. My actual statement is:

from make_shaders.make_shaders import buildShader
from make_shaders.figure_defs import matZones


'buildShader' and 'matZones' both being classes. This approach works perfectly, and is an excellent solution to managing multi-file modules. Incidentally, I took out the add to syspath stuff as well. Thank you, batFINGER. Wish you did a correspondence course... :)