2
$\begingroup$

I have a multi-file/multi-folder add-on, with preferences properties created in a prefs.py.

import bpy

class AddonPreferences(bpy.types.AddonPreferences):
    bl_idname = __package__

    my_property : bpy.props.BoolProperty(
        name = "my_bool",
    )

I can easily access this bool in my __init__.py with:

bpy.context.preferences.addons[__package__].preferences.my_property

But I can't manage to access it in panel.py placed in a UI folder. What is the correct way to call that property in a multi-file add-on?

$\endgroup$
5
  • $\begingroup$ I would guess those properties should be declared in a class, which is registered on blender startup? $\endgroup$
    – Rafal Zera
    Mar 16 '20 at 1:46
  • $\begingroup$ Yes I declare them in the init.py file: bpy.utils.register_class (prefs.AddonPreferences) But if I try to call them in my other files I get a KeyError: KeyError: 'bpy_prop_collection[key]: key "myaddon.ui.panel" not found' $\endgroup$
    – chafouin
    Mar 16 '20 at 2:01
  • $\begingroup$ @chafouin What is the value of __package__ in prefs.py? $\endgroup$
    – batFINGER
    Mar 16 '20 at 9:23
  • $\begingroup$ @batFINGER Is it something I have to define manually? $\endgroup$
    – chafouin
    Mar 17 '20 at 1:03
  • $\begingroup$ @batFINGER Printing it from the prefs.py returns the name of my top folder, which is the name of my addon (in that case, photographer) $\endgroup$
    – chafouin
    Mar 17 '20 at 3:51
1
$\begingroup$

My workaround was to have all the python files in a single folder and avoid subfolders. That way __package__ was returning the proper add-on name. This is not a real solution though.

$\endgroup$
1
$\begingroup$

I recently came across the same issue while developing an addon. I got a workaround from looking at this code of BlendLuxcore: use a mixture of basename and dirname from os.path to get to the folder name of your addon.

$\endgroup$
0
$\begingroup$

After observing how __init__.py packages everything together this simple trick worked. Please do let me know if some other correction is needed.

My folder structure:
my_addon/__init__.py
my_addon/somefunc.py
my_addon/anotherfunc.py
my_addon/somefolder/otherfunc.py

The folder name (my_addon) should have pythonic name (no period, slash etc).

Now in __init__.py I put this simple import:

bl_info = {
    "name": "My addon",
    "version": (3, 0),
    "blender": (2, 90, 3),
    "category": "Sequencer",
    }

# this (from . import) go up one folder 
#and will import the modules from that folder. 
#As a result will recognize as a package.

from . import somefunc, anotherfunc
from .somefolder import otherfunc



modules = (somefunc, anotherfunc, otherfunc)


def register():
    for m in modules:
        m.register()


def unregister():
    for m in modules:
        m.unregister()


if __name__ == "__main__":
    register()
$\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.