0
$\begingroup$

I want to store default export settings in addon preferences. Something like this:

class MyAddonPreferences(bpy.types.AddonPreferences):
    bl_idname = __name__
    my_saved_prop: BoolProperty()
    def draw(self, context):
        self.layout.prop(self, "my_saved_prop")

class MyExportOperator(bpy.types.Operator, ExportHelper):
    <...>
    prefs = bpy.context.preferences.addons[__name__].preferences 
    my_export_option: BoolProperty(default = prefs.my_saved_prop)
    # AttributeError: 'NoneType' object has no attribute 'my_saved_prop'

But I can't access the stored settings from the helper's attributes, only from its functions. I can populate the settings in invoke():

def invoke(self, context, event):
    prefs = bpy.context.preferences.addons[__name__].preferences
    self.my_export_option = prefs.my_saved_prop
    return ExportHelper.invoke(self, context, event)

...But that will mean any non-default settings from previous calls will be lost. Is there any way around this?

$\endgroup$

1 Answer 1

0
$\begingroup$

As it turns out, I need to register the preferences earlier for this to work:

class MyAddonPreferences(bpy.types.AddonPreferences):
    bl_idname = __name__
    my_saved_prop: BoolProperty()
    def draw(self, context):
        self.layout.prop(self, "my_saved_prop")

bpy.utils.register_class(MyAddonPreferences)

class MyExportOperator(bpy.types.Operator, ExportHelper):
    <...>
    prefs = bpy.context.preferences.addons[__name__].preferences 
    my_export_option: BoolProperty(default = prefs.my_saved_prop)

Simply registering before the operator in register() is not enough.

$\endgroup$

You must log in to answer this question.

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