Import Arrays and Combine in Master File

Rather than have 10,000 class names in an array in my __intit__.py, I want to have separate modules for each.

Here’s what I’m working with:

array_alpha.py

import bpy

array_alpha_classes = [
    Alpha_Panel,
    Alpha_Class_01,
    Alpha_Class_02,
    Alpha_Class_03,
]

All other class definitions are in separate files.

__init__.py

bl_info = {}

import os
import bpy

from .color_sets.colors_alpha import *
from .color_sets.colors_internal import *
from .panels.panel_alpha import *
from .panels.panel_internal import *

from .array_alpha import *

array_internal_classes = [
    Internal_Panel,
    Internal_Class_01,
    Internal_Class_02,
    Internal_Class_03,
]

classes = [
  *array_internal_classes,
  *array_alpha_classes
]

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

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

if __name__ == "__main__":
    __file__ = bpy.data.filepath
    register()

But when I I run it, the error I get is

Traceback (most recent call last):
  File "/Applications/Blender.app/Contents/Resources/3.2/scripts/modules/addon_utils.py", line 335, in enable
    mod = __import__(module_name)
  File "/XXX/Blender/3.2/scripts/addons/sample_addon/__init__.py", line 100, in <module>
    from .array_alpha import *
  File "/XXX/Blender/3.2/scripts/addons/sample_addon/array_alpha.py", line 4, in <module>
    Alpha_Panel,
NameError: name 'Alpha_Panel' is not defined

So the array items in array_alpha.py needs to be defined, but how?

Any suggestions?

The problem occurred because array_alpha_classes needs to be in the same file as the class definitions it references, or at least imported into the class definitions file in order to work.

For sake of documentation, here’s the solution:

alpha.py

import bpy

class Alpha_Colors(bpy.types.Operator):
    ...

# DEFINE PANEL AND DRAW IT
class Alpha_Panel(bpy.types.Panel):
    ...

array_alpha = [
    Alpha_Colors,
    Alpha_Panel,
]

__init__.py

bl_info = {}

import os
import bpy

# DEFINE INTERNAL OPERATORS
class Internal_Colors(bpy.types.Operator):
    ...

# DEFINE & DRAW INTERNAL PANEL
class Internal_Panel(bpy.types.Panel):
    ...

# IMPORT ALPHA CLASSES, PANEL, AND ARRAY
# DRAW ALPHA PANEL
from .alpha import *

# DEFINE INTERNAL ARRAY
array_internal = [
    Internal_Colors,
    Internal_Panel,
]

# CONCATENATE CLASSES
classes = [
  *array_internal,
  *array_alpha,
]

# REGISTER & UNREGISTER
def register():
    for cls in classes:
        bpy.utils.register_class(cls)

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

if __name__ == "__main__":
    __file__ = bpy.data.filepath
    register()

Your add-on can be expanded by adding beta.py, gamma.py, and so forth.

A Note About Panel Ordering

The import order will determine the display order of panels, so

from .alpha import *
from .beta import *
from .gamma import *

would display the panels in the order:

Internal_Panel
Alpha_Panel
Beta_Panel
Gamma_Panel

and so forth.