Installation

Add the blender_addon_utils respository to your addon as a submodule:

$ git submodule add https://github.com/30350n/blender_addon_utils my_addon/blender_addon_utils/

How it works

You can declare required dependencies for your addon via add_dependencies().

When you generate your registration code via register_modules_factory(), the function will automatically check if all previously declared dependencies are available.
If not, the resulting registration functions, won’t register your modules, but the end-user will instead be presented with a panel like this, in the addon preferences:
_images/addon_prefs.png
The panel first offers the option to install pip and then the required dependencies.
After dependency installation is complete, your addon will be automatically reloaded.
By default, dependencies will be installed to a site-packages directory inside the root directory of the package that’s calling the add_dependencies() function.
So if you call add_dependencies() in my_addon/__init__.py, dependencies will be installed to my_addon/site-packages (make sure your vcs ignores this folder).

Usage

To use the add_dependencies() and register_modules_factory() functionality, all your addon code will have to be arranged into submodules, which each submodule having its own register() and unregister() functions.

Your __init__.py should only contain minimal setup code, like this:

bl_info = {
    ...
}

from .blender_addon_utils import add_dependencies, register_modules_factory

deps = {
    "pip_package_name": "module_name",
}
add_dependencies(deps)

modules = ["my_submodule"]
register, unregister = register_modules_factory(modules)

Example my_submodule.py file:

import bpy

...

classes = (
    ...
)

def register():
    for cls in classes:
        bpy.utils.register_class(cls)

def unregister():
    for cls in reversed(classes):
        bpy.utils.unregister_class(cls)