0
$\begingroup$

I have a multi-module addon that has ModuleA as AddonPrefs and ModuleB as operators.

I have the following EnumProperty inside my ModuleA AddonPreferences. It takes in a function that makes a list based on a .json file. Everything works for adding new items inside that json.

def update_registered_projects(self, context):
    import json
    projects_list = []

    for mod in addon_utils.modules():
        if mod.bl_info['name'] == __package__:
            addon_path = Path(mod.__file__)

    projects_file_path = Path(addon_path.parent / 'registered_projects.json')

    if projects_file_path.is_file():
        with open('registered_projects.json', 'r') as projects:
            projects_json = json.load(projects)
            for i, project in enumerate(projects_json):
                projects_list.append((project, project, '', i))

    return projects_list

class BitCakeToolsPreferences(AddonPreferences):
    bl_idname = __package__

    registered_projects: EnumProperty(items=update_registered_projects,
                                      name='',
                                      description='Register projects here before starting.',
                                      )

In ModuleB I have an Operator that basically reopens the .json file, .pop() the Key contaning the project I want to unregister from my enum and dumps the dict back into that file again.

I'm getting the following error on my Panel's Draw() function once my enum_unregister function runs:

This only happens when I delete the LAST entry in the Enum.

WARN (bpy.rna): C:\Users\blender\git\blender-v293\blender.git\source\blender\python\intern\bpy_rna.c:1505 pyrna_enum_to_py: current value '1' matches no enum in 'BitCakeToolsPreferences', '(null)', 'registered_projects'
Python: Traceback (most recent call last):
  File "C:\Users\NZXT-DESKTOP\AppData\Roaming\Blender Foundation\Blender\2.93\scripts\addons\BitTools\menu_side.py", line 69, in draw
    current_engine = projects_json[addonPrefs.registered_projects]['engine']
KeyError: ''

This error is happening on the last line of the following code:

class BITCAKE_PT_send_to_engine(Panel):
    bl_idname = "BITCAKE_PT_send_to_engine"
    bl_label = "BitCake Exporter"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = "BitTools"

    def draw(self, context):
        scene = context.scene
        panel_prefs = scene.menu_props

        addonPrefs = context.preferences.addons[__package__].preferences

        pcoll = preview_collections["main"]
        unity_logo = pcoll["unity"]
        unreal_logo = pcoll["unreal"]
        cocos_logo = pcoll["cocos"]

        registered_projects_path = get_registered_projects_path()

        current_engine = None
        if registered_projects_path.is_file():
            projects_json = json.load(registered_projects_path.open())
            current_engine = projects_json[addonPrefs.registered_projects]['engine']

I've tried Googling everything and searching this place for an answer, without any success. Hopefully you guys could shed some light, here.

Thanks a lot for taking your time to help me out :)

$\endgroup$
2
  • $\begingroup$ You should probably gracefully handle when the enum isn't in the JSON anyway, but the most obvious thing is you aren't holding a reference to the strings your dynamic enum uses: see the warning in EnumProperty's docs and this Q. Try fixing that first. $\endgroup$
    – scurest
    Dec 4, 2021 at 3:26
  • $\begingroup$ I spent so much time figuring out why this bug was happening that I didn't realize I could've just pointed MyEnum = 'AnotherEnumStringID' and just change it to another valid entry before Draw() was called. So I did that. I was trying to forcefully rebuild my Enum list before Draw() was called and the solution was way simpler than I thought, I had even read that same Question you sent... But anyways, problem handled, sorry for the dumb question :/ $\endgroup$
    – elamhut
    Dec 5, 2021 at 23:40

0

You must log in to answer this question.

Browse other questions tagged .