Page MenuHome

Cannot access add-on user preferences from register if using --addons command line flag
Confirmed, NormalPublic

Description

System Information
Operating system: Linux-5.4.0-72-generic-x86_64-with-glibc2.31 64 Bits
Graphics card: GeForce GTX 970/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 450.102.04

Blender Version
Broken: version: 2.93.0 Beta, branch: master, commit date: 2021-04-19 07:18, hash: rB6dffdb02fa2a

Short description of error
An add-on cannot access its user preferences in the register() function if it is activated through the --addons command line flag, but has not been manually activated before.

Exact steps for others to reproduce the error

  • Install the following add-on stub:

bl_info = {
    "name": "Preference Register Test",
    "author": "Your Name Here",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "",
    "description": "",
    "category": "Test",
}

import bpy

def register():
    addon_prefs = bpy.context.preferences.addons[__name__].preferences
    print("Found preferences")

def unregister():
    pass
  • Run blender with this command: blender -b --addons test_register
    • The add-on fails with the following error:
Traceback (most recent call last):
  File "/home/damien/blender-git/build_linux/bin/2.93/scripts/modules/addon_utils.py", line 386, in enable
    mod.register()
  File "/home/damien/.config/blender/2.93/scripts/addons/test_register.py", line 14, in register
    addon_prefs = bpy.context.preferences.addons[__name__].preferences
KeyError: 'bpy_prop_collection[key]: key "test_register" not found'
  • Open Blender, activate the add-on from the User Preferences and save them
  • Run Blender in the background again (blender -b --addons test_register)
    • Now the add-on prints the expected Found preferences

Event Timeline

Campbell Barton (campbellbarton) changed the task status from Needs Triage to Confirmed.Tue, Apr 20, 4:36 AM
Campbell Barton (campbellbarton) moved this task from Backlog to Bugs (API) on the Python API board.