I have the following situation. I have a class (let's call it GeometryCalculator) with many functions which I would like to reuse between a few addons. How can I do it in a way that the GeometryCalculator is a part of each addon. If a user has 5 addons installed and then he uninstalls any of them the rest of addons can still use this common class. Should all addons have a copy of the GeometryCalculator class in their folder? Any ideas how to do it are welcome. Are there any guidelines how this should be tackled? Thanks in advance.
1 Answer
Inside your scripts folder you have addons
and maybe addons_contrib
and a modules
folder.
The modules folder can added if it's not there and used to store files that you want to access from various add-ons. In this case I have a file which stores functions, but it could store classes (anything a .py file is used for).
Imagine the modules folder has a folder inside called 'some_name',
and some_name contains a python file called functions,
so the path is scripts/modules/some_name/functions.py
. To import that module in an addon you write
from some_name import functions
Potentially this makes installing your add-on a little bit more involved for the average novice user, unless you write code that places the files in the modules folder automatically
-
$\begingroup$ Ok Thanks for the answer Zeffii. I need to look into it and test it a bit. Just a small follow up question: In case there are a few addons using the functions.py module from module folder - which one of them should actually delete the functions.py during addon removal? $\endgroup$– komi3DJul 9, 2015 at 22:06
-
$\begingroup$ A fair question! A python file can tell you its current location on disk using
__file__
, meaning you can search relative to that for the modules folder, but the tricky issue is that Blender may use several places to store addons. This is probably worthy of a new stand-alone question, it may be more involved than I want to answer in a comment, hope you understand (that way it can be found by other people asking the same question too) $\endgroup$– zeffiiJul 10, 2015 at 5:06 -
$\begingroup$ Ok thanks Zeffii. I just want to add a small comment. I use windows and the addons are usually installed in place like: C:/Users/<username>/AppData/Roaming/Blender Foundation/Blender/2.74/scripts/addons/. So it seems that if an addon is using an import like "from some_name import functions" you can basically create a folder "some_name" inside the above path for addons and add the modules there as well. the addon should be able to load that. However with this approach the folder "some_name" is left on the disk when uninstalling the addon... $\endgroup$– komi3DJul 22, 2015 at 20:15
-
$\begingroup$ well, it really depends how the user installs the addon, personally I stick everything into a separate scripts folder (can be set in user preferences - File - Scripts ), not in AppData or the linux/osx equivalent. So I think your best bet is to do cleanup relative to
__file__
$\endgroup$– zeffiiJul 22, 2015 at 21:21 -
$\begingroup$ but you can also list all the possible places that blender will be looking for addons/addons_contrib/module and see if any of those contain your module.
bpy.utils.script_paths()
$\endgroup$– zeffiiJul 22, 2015 at 21:24