Skip to content

Caveat Importing Addons

Tyler Alden Gubala edited this page Aug 14, 2019 · 3 revisions

Overview

  • Addons must be compatible with the version of Python that you are using
  • Addons must be compatible with the same version of the bpy API

Example Code

To get started using addons please follow the code examples below.

Importing an Addon

Here we prescribe a method for importing Blender addons. Note that Addons will have to be present first, possibly downloaded via script in the case of continuous integration scripts.

#! /usr/bin/python
# -*- coding: utf-8 -*-
"""Example highlighting how to import an addon

It is typically called "Pythonic" to "ask for forgiveness, not permission"
so here we try to import the addon and if it doesn't work we install it and try again
"""
# STD LIB IMPORTS
import os
import pathlib

# Monkey-patch 3.4 and below

if sys.version_info < (3,5):

    def home_path() -> pathlib.Path:

        return pathlib.Path(os.path.expanduser("~"))

    pathlib.Path.home = home_path

import bpy # Makes `addon_utils` available to import
import addon_utils # Only available after `bpy` is imported

try:
    addon_utils.enable(module_name="BlendLuxCore") # Proper importing of addon
except ImportError:
    bpy.ops.wm.addon_install(filepath=os.path.join(pathlib.Path.home, 'Downloads/BlendLuxCore/BlendLuxCore-v2.0-win64-opencl.zip') # Attempt to install the addon, in this case BlendLuxCore
    addon_utils.enable(module_name="BlendLuxCore") # Proper importing of addon

# Usage of the addon...

Errors

As with any programming process there are errors that may potentially happen along the way

DLL Not Found

Traceback (most recent call last): File "C:\Users\TGubs\Code\Python\blender_test\venv\Scripts\2.79\scripts\modules\addon_utils.py", line 351, in enable mod = import(module_name) File "C:\Users\TGubs\AppData\Roaming\Blender Foundation\Blender\2.79\scripts\addons\BlendLuxCore_init_.py", line 13, in from .bin import pyluxcore ImportError: DLL load failed: The specified module could not be found.

This generally means that the addon was not compiled to the version of Python being used. Get a build of the addon that utilizes the same version of Python being used.

GUI Addons will fail

Because of the headless nature of the environment, the GUI is not initialized. Therefore, if the addon requires adding panels to the Blender UI, it will likely fail at that step.

3d Print Toolbox: Updating Panel locations has failed

This is usually fixed by importing only necessary functions or using the addon from a level of abstraction if necessary.

#! /usr/bin/python
# -*- coding: utf-8 -*-
"""Example highlighting how to import an function of an addon
"""
import bpy
import bmesh
from object_print3d_utils.mesh_helpers import bmesh_copy_from_object

# Create an empty mesh and the object.
mesh = bpy.data.meshes.new('Basic_Cube')
basic_cube = bpy.data.objects.new("Basic_Cube", mesh)

# Construct the bmesh cube and assign it to the blender mesh.
bm = bmesh.new()
bmesh.ops.create_cube(bm, size=1.0)
bm.to_mesh(mesh)
bm.free()

bmesh_copy_from_object(basic_cube).calc_volume()

Further Reading

See also

#14