1
$\begingroup$

I built some functions into python module functions.py, but in it if I try to get the __name__ I get something like __name __.functions

How do you get the name of the addon a pythonic way?

I add some details:

Example module functions.py

def open_user_pref(context, addon_name):

    preferences = context.preferences.addons[addon_name].preferences
    bpy.ops.screen.userpref_show('INVOKE_DEFAULT')
    bpy.context.preferences.active_section = 'ADDONS'
    bpy.ops.preferences.addon_expand(module=addon_name)
    bpy.ops.preferences.addon_show(module=addon_name)

Example call from __init__.py

from . import functions

functions.open_user_pref(bpy.context, __name__)

But if I want to avoid the input name, I should find the exact name of the addon. So how can this be done?

def open_user_pref(contex):
    #addon_name = __name__.split(".")[0] ? This seems dangerous to me if this package becomes a subpackage

    preferences = context.preferences.addons[addon_name].preferences
    bpy.ops.screen.userpref_show('INVOKE_DEFAULT')
    bpy.context.preferences.active_section = 'ADDONS'
    bpy.ops.preferences.addon_expand(module=addon_name)
    bpy.ops.preferences.addon_show(module=addon_name)
$\endgroup$
2
  • 1
    $\begingroup$ Please add some details. An addon is imported as a module. The addons name (__name__) will be the same as folder or python file name (without py extension) in the addons folder. If functions.py is a submodule of your addon, then it is not itself an addon. If so and you are getting the package name of functions as eg myaddon.functions split on "." . $\endgroup$
    – batFINGER
    Commented Jun 12, 2021 at 5:51
  • $\begingroup$ Hi batFinger, I have added a trivial example. My concern (Because I'm impractical with this) is if the functions.py module became part of a subpackage $\endgroup$
    – Noob Cat
    Commented Jun 12, 2021 at 14:57

1 Answer 1

3
$\begingroup$

The inspect module.

This is somewhat a generic python question, tho As it happens I have an addon that automates a lot of settings to create and register addons based on a folder structure. Wanted to keep it as simple as possible, and used the following to automate getting the importing module (addon) name.

Pretty sure this can be used to suit your needs.

In root folder of "bat_tools" addon

bat_tools/__init__.py

from .types.app import BATApp

bl_info = {
    "name": "BAT Tools",
    "author": "batFINGER",
    "location": "Every Bloody Where",
    "description": "A Collection of BAT Tools",
    # "warning": "Still in Testing",
    "version": (0, 0, 1), "blender": (2, 80, 0),
    "support": "TESTING",
    "category": "Toolkit",
}

app = BATApp()

register, unregister =  app.register()

and a snippet from the class def in the submodule. When the object is instanced set the name and package from the module which imported it. (Code from stackoverflow.com search Get __name__ of calling function's module in Python)

bat_tools/types/app.py .

class BATApp:
    @property
    def prefs(self):
        from bpy import context
        pref = context.preferences.addons.get(self.name)
        if pref:
            return getattr(pref.preferences, self.name, None)
        return None


    def __init__(self, **kwargs):
        # get the details of calling module

        import inspect
        frm = inspect.stack()[1]
        mod = inspect.getmodule(frm[0])
        # print(mod.__name__, mod.__package__, mod.__file__)
        self.name = mod.__name__
        self.package = mod.__package__
        path = Path(mod.__file__)

BTW see no issue with splitting the package as commented, (and illustrated in question) foo.bar.foo since an addon is a base module, follows a naming convention which cannot be named "foo.bar" for example.

$\endgroup$
3
  • 3
    $\begingroup$ "BAT tools" made my day! $\endgroup$
    – brockmann
    Commented Jun 12, 2021 at 15:40
  • $\begingroup$ Really very interesting, I want to study this method of yours. But allow me a stupid question: Basically can I always get the name with __name __.split(".")[0]? or is it a bad idea? $\endgroup$
    – Noob Cat
    Commented Jun 12, 2021 at 15:50
  • 1
    $\begingroup$ If the base module is the same. Above (inspect) "should" work across modules. $\endgroup$
    – batFINGER
    Commented Jun 12, 2021 at 15:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .