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,
)
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__)
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) .
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.