Accessing the module of another add-on?

I’m interested in allowing one of my add-ons to call into functionality of one of my other add-ons. I poked around in the AddonManager for a while, but could only find functions to retrieve metadata of another add-on, not to actually access any of its code. I am assuming that the module of an imported add-on is accessible through some other module I can get to within the context of an executing add-on, but am not sure where.

If there’s another way to expose a function in one add-on so that I can call it, I would be OK with modifying the dependency add-on to make this possible too, since I control both of them.

1 Like

Let’s say you want to access my_addon.src.lib. Use this utility function:

from typing import Optional
from types import ModuleType
from importlib import import_module

def find_addon_by_name(addon_name: str) -> Optional[ModuleType]:
    for name in mw.addonManager.allAddons():
        if mw.addonManager.addonName(name) == addon_name:
            try:
                return import_module(name)
            except ModuleNotFoundError:
                pass

    return None

Then you can use this function like so:

if mylib := find_addon_by_name('My add-on'):
    lib = mylib.src.lib
9 Likes

Thanks, this works perfectly!