I have an add-on that sets a material color, and as it grows to include more color sets, I’m making it modular.
Moving the color classes to external files was easy, but moving the individual sub-panels is breaking them.
The sub-panels load a color thumbnail image for each button, but when I move them to an external file, the link to the image breaks.and the error message I get is
Traceback (most recent call last): File "/XXX/Blender/3.2/scripts/addons/blender-mc/panel_f58.py", line 23, in draw scol.label(text="", icon_value=c_icons["f58a_raven_black"].icon_id) NameError: name 'c_icons' is not defined
The files are on GitHub, but here are the relevant code snippets:
__init__.py, I open with
import os import bpy import bpy.utils.previews
The parent panel is defined, and then where I used to have the sub-panel class, I import an external file
from .panel_f58 import *
and at the end, the image loader is included in register/unregister:
def register(): global c_icons c_icons = bpy.utils.previews.new() addon_path = os.path.dirname(__file__) icons_dir = os.path.join(addon_path, "icons") for entry in os.listdir(icons_dir): c_icons.load(os.path.splitext(entry), os.path.join(icons_dir, entry), "IMAGE") def unregister(): global c_icons bpy.utils.previews.remove(c_icons)
panel_f58.py file opens with
and the relevant code for each button is
def draw(self, context): global c_icons layout = self.layout srow = layout.row() scol = srow.column(align=True) scol.scale_y = 1.25 scol.label(text="", icon_value=c_icons["f58a_raven_black"].icon_id) ... scol = srow.column(align=True) scol.scale_y = 1.25 scol.scale_x = 3.0 scol.operator("color.f58a_raven_black", text="A Raven Black") ...
and that’s about it.
I’ve tried copying the
__init__.py imports and register/unregister into my sub-file, but nada.
It’s probably something very basic to do with
global c_icons connecting/pointing to the wrong location or
os.path being incorrectly defined or something, but I’m pretty much hurr-durr at this point.