3
$\begingroup$

I have a couple of preference properties (string, boolean, float) in my add-on and they all have a default value in their defining statement.

When I install an update to my add-on, the previously modified settings are not saved and the default values are used instead.

Is there a way to save the settings before the update and restore them afterwards? Or do I have to get rid of the default values from the properties? (That would be not so good.)

I was looking at the application handlers but I could not find a handler for add-on updates.

Does anyone have a best practice method for dealing with add-on preferences? Thanks in advance.

Update: here is how my add-on settings are defined:

class PREF_PT_Preferences(bpy.types.AddonPreferences):
    bl_idname = __package__

    pref_library_path: bpy.props.StringProperty(name="pref_library_path",subtype="DIR_PATH", description = '...', default=Library_Location)
    pref_local_lib_path: bpy.props.StringProperty(name="pref_local_lib_path",subtype="DIR_PATH",description = '...', default="")
    pref_stage_height: bpy.props.FloatProperty(name ='pref_stage_height', description = '...', default=(240), soft_min=0, soft_max=1000)
$\endgroup$
7
  • $\begingroup$ Really important question. I would like to know the answer as well. Maybe you could let the readers know, how you are updating the addon? $\endgroup$
    – Leander
    Commented Jan 4 at 11:52
  • 1
    $\begingroup$ Sorry, I forget to mention that: I let the users install the add on, explicitly asking them not to remove the older version before the installation. The add on has the same name and installs over the old version. The update works fine, just the user modified preference settings are lost after the update. $\endgroup$
    – Steve
    Commented Jan 4 at 12:10
  • $\begingroup$ I have an idea for a ridiculous hack. In the register portion of your addon, check for a custom attribute, that persists (like on the window_manager). If it doesn't exist create it and make it store the addon preferences. If it exists, copy those values to the addon preferences. $\endgroup$
    – Leander
    Commented Jan 4 at 12:14
  • $\begingroup$ Interesting idea. Thanks. I still hope that there is an "official" way of dealing with this. To be honest, I was lacking the time to check other add-ons from professional programmers how they do it. It's not so easy to follow others code (I am a beginner developer) and it would be less "painful" if someone who knows this well, would explain it properly. $\endgroup$
    – Steve
    Commented Jan 4 at 12:37
  • $\begingroup$ So, I check my addons and this is the default behaviour. The settings are not overriden. Are you defining your preferences in a class, that inherits from bpy.types.AddonPreferences with :-properties? $\endgroup$
    – Leander
    Commented Jan 4 at 13:24

1 Answer 1

0
$\begingroup$

I faced the same problem working on addon of mine, and the solution was to save them in a .env.

  1. Download dotenv using pip install python-dotenv

  2. Create a .env file in you addon main directory and add the default preferences to it.

  3. Read the .env file using dotenv, like this:

     from dotenv import load_dotenv, find_dotenv
    
     dotenv_file = find_dotenv()
     load_dotenv(dotenv_file)
    
  4. You can now retrieve any value from the .env using:

     from dotenv import dotenv_values, find_dotenv
     dotenv_file = find_dotenv()
     config = dotenv_values(dotenv_file)
     return config["YOUR_PREFERENCE_KEY"]
    
  5. If you want to override the setting you can use the set_key function like this:

     from dotenv import set_key, find_dotenv
    
     dotenv_file = find_dotenv()
     set_key(dotenv_file, "YOUR_PREFERENCE_KEY", "YOUR_PREFERENCE_VALUE")
    

Hope this helps. Just make sure the user's .env doesn't get replaced with the stock one when updating.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .