1
$\begingroup$

I am working on my first add-on, though I am pretty experienced with Python having written multiple apps using Django. My project structure looks like this:

lib/
  application/
  blender/
    models/
    operators/
      test_local_simulation_operator.py
      ...
    ui/
      panels/
        test_simulation_panel.py
        ...
  domain/
  ...
__init__.py
...

My init file contains the following code after the bl_info.

from bpy.utils import register_classes_factory

from lib.blender.operators.test_local_simulation_operator import (
    TestLocalSimulationOperator,
)
from lib.blender.ui.panels.test_simulation_panel import TestSimulationPanel

classes = (TestLocalSimulationOperator, TestSimulationPanel)

register, unregister = register_classes_factory(classes)

I am using VS Code on Windows with the Blender Development extension. When I launch the add-on using the Blender: Start command, I get the following import error:

Traceback (most recent call last):
  File "c:\Users\[user]\Blender Foundation\Blender 2.91\2.91\scripts\modules\addon_utils.py", line 351, in enable
    mod = __import__(module_name)
  File "C:\Users\[user]\AppData\Roaming\Blender Foundation\Blender\2.91\scripts\addons\[project_folder]\__init__.py", line 22, in <module>
    from lib.blender.operators.test_local_simulation_operator import (
ModuleNotFoundError: No module named 'lib.blender'

I have tried adding an init file to all of my sub-modules to no avail. The only way I can get it to work is by adding this import style to each init file chained throughout my project.

# lib/__init__.py
from . import blender, ...

This seems like a lot of unnecessary overhead not required in conventional Python projects, so my conclusion is that I am doing something wrong here.

What should I do to get my modules to import properly?

$\endgroup$
3
  • $\begingroup$ See this. Everyone just writes from . import though. You can always check how the default addons do things. $\endgroup$
    – scurest
    Commented Feb 23, 2021 at 1:17
  • $\begingroup$ (Not using VS) but suggest looking at answers here blender.stackexchange.com/questions/158775/… and similarly here blender.stackexchange.com/questions/183773/… Rather than some horrible long import chain to get a blender class to register, can propagate the register down the hierarchy by calling the register method of immediate child modules. $\endgroup$
    – batFINGER
    Commented Feb 23, 2021 at 6:49
  • $\begingroup$ Thank you for the comments! I was able to solve it by diving down the rabbit hole provided by @scurest. I created a function to register my add-on path with Blender's paths, moved my class imports to a getter function, and called these from my own register/unregister functions instead of using Blender's factory utility. $\endgroup$ Commented Feb 24, 2021 at 0:55

0

You must log in to answer this question.

Browse other questions tagged .