Enum default from Addon preferences


I’d like to be able to pick a default value for an enumProperty from the addon preferences.

For this I have created a second enumProperty in the Addon preferences (named enumProp_pref here), identical to the other enum, and I am trying to fetch it the addon, but I can’t get it to work. Is it even possible?

For the addon Preferences:

    class AddonPreferences(bpy.types.AddonPreferences):
        bl_idname = __name__
        enumprop_pref = bpy.props.EnumProperty(
        name = "enumprop_pref",
        items = [('ONE','One', ''),('TWO','Two','')],
        default = 'ONE'

And then in my addon properties:

class PanelSettings(bpy.types.PropertyGroup):
        enumprop = bpy.props.EnumProperty(
        name = "enumprop",
        items = [('ONE','One', ''),('TWO','Two','')],
        default = bpy.context.user_preferences.addons[__name__].preferences.enumprop_pref,

Yes, it is possible.
Make sure you register the addon preferences before you reference it.
Post the error message.

I am getting this error:
AttributeError: ‘NoneType’ object has no attribute ‘enumprop_pref’

I can print bpy.context.user_preferences.addons[__name__].preferences.enumprop_pref in the def __init__ without any problem, but using a function called from the addon properties doesn’t work.

My AddonPreferences class is registered properly by bpy.utils.register_module(__name__)

1 Like

Here’s a working example:

Is your addon a single- or multi-file addon?

bl_info = {   
 "name": "addon name",   
 "author": "author",   
 "version": (1, 0),   
 "blender": (2, 7, 9),   
 "location": "",   
 "description": "",
 "warning": "",   
 "wiki_url": "",   
 "tracker_url": "",
 "category": "OBJECT"}

import bpy

# Addon Prefs
class AddonPreferences (bpy.types.AddonPreferences):
    bl_idname = __name__
    enumprop_pref = bpy.props.EnumProperty(
        name = "enumprop_pref",
        items = [('ONE','One', ''),('TWO','Two','')],
        default = 'ONE'

# Register Addon Prefs
bpy.utils.register_class (AddonPreferences)

# Rest of the Addon
class PanelSettings (bpy.types.PropertyGroup):
    enumprop = bpy.props.EnumProperty(
        name = "enumprop",
        items = [('ONE','One', ''),('TWO','Two','')],
        default = bpy.context.user_preferences.addons[__name__].preferences.enumprop_pref

# Register Addon
def register():
    bpy.utils.register_class (PanelSettings)

def unregister ():
    bpy.utils.unregister_class (PanelSettings)
if __name__ == "__main__":   


  1. Define addon preferences
  2. Register addon preferences
  3. Define rest of the addon
  4. Register rest of the addon
1 Like

My addon is a single file at the moment.
You were right, I was not registering the Addon Prefs before defining the rest of the addon. It works now.

However, I noticed that I have to close and reopen Blender for it to apply the Addon preferences. Saving the User preferences isn’t enough. It works fine for other addons I’m using though (comparing to Speedflow, which is a multi-file addon with the AddonPreferences registered in the init.py) .

Thanks a lot for your help!

Changing properties in AddonPreferences won’t override the value of the properties, it only overrides their default value. When a new blend file is created all properties will be set to their default value but in an existing blend file the value of properties will be unaffected by changes in AddonPreferences, only the default values change. This is the desired behaviour.

I understand what you mean, of course it would be a nightmare if that would override settings that had been already set up.

In my case, since these are default values for new camera properties, I was hoping that all new cameras would inherit from the addon preferences at creation. This is only the case when restarting blender, not even when creating a new scene.

I guess I can live with that. Thanks again for your help.

Then read the props from addon prefs when you create a new camera. In that case, you shouldn’t even create those props outside the addon prefs.

That’s a very good idea :slight_smile: I’ll do that, thanks!