8
$\begingroup$

I can create a checkbox with bpy.props.BoolProperty. I can set a default for it. True or False. Let's say we start with False. And my user wants the value to be True. Permanently. The problem is, when i restart Blender, then the bool is reset to the default value. And this also when i save the user preferences or the startup.blend

Is there a way to save the current state of the BoolProperty so that the user doesn't have to adjust it all the time?

Examplecode:

import bpy

class LayoutDemoPanel(bpy.types.Panel):
    bl_label = "Checkbox"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'

    def draw(self, context):
        layout = self.layout

        wm = context.window_manager # Our bool is in the windows_manager
        layout.prop(wm, "checkbox_bool") # Our checkbox

def register():
    bpy.utils.register_class(LayoutDemoPanel)
    #Our bool
    bpy.types.WindowManager.checkbox_bool = bpy.props.BoolProperty(
    name="A checkbox", description="Do this or that", default = False)

def unregister():
    del bpy.types.WindowManager.checkbox_bool # Unregister our flag when unregister.
    bpy.utils.unregister_class(LayoutDemoPanel)


if __name__ == "__main__":
    register()
$\endgroup$

1 Answer 1

10
$\begingroup$

The window-manager is an exception when it comes to data-blocks, having some characteristics that make it work differently then meshes, scenes... etc.

  • Can't be added or removed.
  • Its a singleton (there is only ever one).
  • Its not saved in the blend file (this is why properties aren't set on reload).

This can be handy, for example - this is used for the add-on and key-map search text fields.

For persistent data, its normally best to use...

$\endgroup$
0

You must log in to answer this question.

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