7
$\begingroup$

My addon is in /home/usr/.config/blender/2.68/scripts/addons/foldername, has an __init__.py and cannot somehow import parts, that exist in the same folder. It says, no module named mymodule but from text editor it runs and work properly

What could be wrong here?

$\endgroup$
  • $\begingroup$ it solved: import sys sys.path.append('/home/$USER/.config/blender/2.68/scripts/addons/$FOLDER') or installetion with zip from BUP $\endgroup$ – nikitron Oct 13 '13 at 14:26
  • $\begingroup$ Place an __init__.py file inside a folder to make it recognized as a Python module. $\endgroup$ – Adhi Oct 13 '13 at 14:46
  • $\begingroup$ @Adhi, it is in. The last thing i found, it says, that it whants not id-parameter in registration, but rna... I don't know what he needs exactly $\endgroup$ – nikitron Oct 14 '13 at 11:10
  • $\begingroup$ I forgot to say, we write custon nodes addon $\endgroup$ – nikitron Oct 14 '13 at 11:17
5
$\begingroup$

You can place your module into the folder blender_directory/2.68/scripts/modules/ (so Blender can found it automatically), while your addon can stay in the scripts/addons/foldername folder with its __init__.py (and not __index__.py, by the way).

Or, you can go to User Preferences, tab File, and specify a custom extra folder for scripts, in the scripts input.
Note that this custom folder must contain 3 sub-directories : addons, modules, and startup.

enter image description here

In this example, your addon will of course be placed in :
/home/user/scripts/blender/addons/yourAddon/,
and the module(s) you want to import will be into :
/home/user/scripts/blender/modules/.

This way, no need to use sys.path.append()!

$\endgroup$
  • $\begingroup$ thanks you much but index is html tradition. here all scripts init $\endgroup$ – nikitron Oct 13 '13 at 20:56
1
$\begingroup$

Mostly this is a Python question, but it can be helpful to see working examples. Just like topology isn't strictly a Blender specific topic yet we still cover it sometimes. This reimport / and local imports topic is not necessarily obvious in a Blender add-on context.

If you have a folder called 'SomeAddon' and it contains the following files.

SomeAddon
Ͱ __init__.py
Ͱ resources.py

And you want to import a few functions from resources.py into __init__.py, then you do

from .resources import (function1, function2, function3)

This will work once, but when you go to reload python F8 - as you will most likely do a lot during development - the changes you make to the functions (function1, function2, function3) will not be pulled in - globals() will still contain the cached versions of the old functions.

You can control what your add-on does during the reload event (triggered by F8). Most add-ons have some form of explicit reload of their modules. What tends to work is this (or something similar):

if 'resources' in globals():
    importlib.reload(resources)

from .resources import (function1, function2, function3)

The first time you enable the add-on, resources is hoisted into the globals for this module (your add-on). When you hit F8 resources is already present and to update it you need to use importlib's reload function.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.