Hi! How can I store some addon settings so I can get it even if the project file changes, or I close and reopen Blender?
For example, I’ll make a Library Gallery, but I don’t want to setup the Gallery Path every time I open a new file.
Thanks!
Hi! How can I store some addon settings so I can get it even if the project file changes, or I close and reopen Blender?
For example, I’ll make a Library Gallery, but I don’t want to setup the Gallery Path every time I open a new file.
Thanks!
Try addon preferences:
http://www.blender.org/documentation/blender_python_api_2_68_release/bpy.types.AddonPreferences.html
I guess you need to enable your addon and save user settings to keep them permanently.
You can just create your own file and read that file when the AddOn loads. If the file does not exist just populate with defaults.
But there is an API mechansim for storing ‘Preferences’ with an AddOn.
writing to an own config file can be problematic on windows if it’s a folder on the system drives that requires elevated rights - in that case, writing will fail unless the user runs blender as administrator. If you use the %appdata% path of blender, it should work though.
@ CodeManX: Thanks for the link to the preferences. I knew they were added a few versions back but never took the time to include them in my AddOns. I added a simple Boolean to my AddOn and linked it to my global SHOW_MESSAGES flag for debugging. This global flag resides in a module named util that my AddOn imports. Now I can control if debug messages get displayed in the console directly from the User Preferences panel.
Not too hard to implement and the preferences properties seem to support the update feature as well.
import bpy
from bpy.types import Operator, AddonPreferences
def updateReGroupPreferences(self,context):
util.SHOW_MESSAGES = self.show_debug
class ReGroupPreferences(AddonPreferences):
# This must match the addon name, use '__package__'
# when defining this in a submodule of a python package.
bl_idname = __name__
show_debug = bpy.props.BoolProperty(name="Show RE:Group Debug Messages In The Console", description="When enabled debug messages will appear in the console window.", default=False, options={'ANIMATABLE'}, subtype='NONE', update=updateReGroupPreferences)
def draw(self, context):
self.layout.prop(self, "show_debug")
def register():
properties.register()
ui.register()
events.register()
operators.register()
bpy.utils.register_class(ReGroupPreferences)
# Make sure the intial preferences value gets set.
user_preferences = bpy.context.user_preferences
addon_prefs = user_preferences.addons["regroup"].preferences
util.SHOW_MESSAGES = addon_prefs.show_debug # Transfer preference data to module variable.
NOTE: I added my init logic to the end of register def that gets called when the main AddOn loads. This way I can read the stored preferences data and blast it out over top my global in an imported module.