1
$\begingroup$

I have a bool property. I can save the state of the bool property in the startup file. And it will load in this state when you start Blender.

The problem is when i load a new blend file then this bool gets reset to its default status. Or to the state in which it gots saved to the blend file.

Both is not wanted behaviour. I want to keep the state in which it was saved in the startup file when i load a new file. It is a setting of my addon. It is a bit cumbersome to turn the checkbox on (or off) every time you load a new blend file.

Is there a way to prevent to load the status of the bool with loading a blend file? But just with the startup file?

Examplescript to test the problem. Run it in the scripting layout, turn on the checkbox in the properties sidebar and load a new blend file to see the problem:

    import bpy

class MyData(bpy.types.PropertyGroup):
    checkbox_bool = bpy.props.BoolProperty(name="A checkbox", description="Do this or that", default = False) # Our prop

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

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

        scene = context.scene # Our data is in the current scene
        layout.prop(scene.my_addon_data, "checkbox_bool") # Our checkbox

        # Example useage of the bool. 
        if scene.my_addon_data.checkbox_bool:
            layout.label(text="checkbox is on")

        else:
            col = layout.column(align=True)
            col.label(text="checkbox is off")


def register():
    bpy.utils.register_class(CheckboxpropPanel)
    #Our data block
    bpy.utils.register_class(MyData) # Our data block
    bpy.types.Scene.my_addon_data = bpy.props.PointerProperty(type=MyData) # Bind reference of type of our data block to type Scene objects

def unregister():
    del bpy.types.Scene.my_addon_data # Unregister our data block when unregister.
    bpy.utils.unregister_class(MyData) # Our data block
    bpy.utils.unregister_class(CheckboxpropPanel)

if __name__ == "__main__":
    register()

enter image description here

enter image description here

$\endgroup$
5
  • $\begingroup$ See docs.blender.org/api/blender_python_api_2_71_release/…, this is probably what you want. $\endgroup$ Feb 21 '17 at 18:09
  • $\begingroup$ Theoretically yes. But the checkbox is in the tool shelf. Not in the User Preferences. Is there a way to work with addonpreferences in the tool shelf too? $\endgroup$
    – Tiles
    Feb 21 '17 at 18:11
  • 1
    $\begingroup$ Imho this looks like some global addon setting to me, then it should be in the Preferences and not in the ToolShelf. But if you really want you can access such properties from wherever you like and also you can display them in a Panel wherever you want. It's just a bit hack-ish. $\endgroup$ Feb 21 '17 at 18:16
  • $\begingroup$ Yes. But the point of having it accessible is well, to have it accessible. It's both, a global setting when you save the startup file, and something to work with when you need it. That's why i don't want my users to dig in hidden settings. I want and need it at ui top level - So thanks. Let's have a look if i can get this to work :) $\endgroup$
    – Tiles
    Feb 21 '17 at 18:50
  • $\begingroup$ I am lost. I have no idea how to use AddonPreferences with my prop here. Does somebody know a good example? $\endgroup$
    – Tiles
    Feb 22 '17 at 17:07
2
$\begingroup$

There is an AddonPreferences example here. It even has a bool property.

Access this property and display it anywhere like this (for example Panel's draw method):

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

    user_preferences = context.user_preferences
    addon_prefs = user_preferences.addons[__name__].preferences

    layout.prop(addon_prefs, "boolean")

    if addon_prefs.boolean:
        layout.label(text="checkbox is on")
    else:
        layout.label(text="checkbox is off")
$\endgroup$
6
  • $\begingroup$ Thanks Jerryno, I tried this example, but it doesn't show anywhere. How do i get the example to work? $\endgroup$
    – Tiles
    Feb 23 '17 at 9:56
  • $\begingroup$ Now i feel stupid. It's an addon. So i have to install it, and the stuff will show up in the User Preferences. I did run it as a script in the text editor ^^ - Problem solved i would say. Many thanks again :) $\endgroup$
    – Tiles
    Feb 23 '17 at 10:37
  • $\begingroup$ I was too fast at the accept button ^^. I cannot access the bool externally. I either end in "KeyError", or in " name not defined". I use the code as it is above. But it seems that something is missing .How exactly does the addon_prefs = user_preferences.addons[name].preferences line have to look like? Because at the moment it seems to lack the name of the addon. The addon with the bool is called ExampleAddonPreferences. It's still the example from the api. $\endgroup$
    – Tiles
    Feb 23 '17 at 17:49
  • $\begingroup$ Have it! - addon_prefs = user_preferences.addons["ExampleAddonPreferences"].preferences $\endgroup$
    – Tiles
    Feb 23 '17 at 17:59
  • 1
    $\begingroup$ @Tiles Thanks for pointing this out, yes, there should be parentheses. Corrected. $\endgroup$ Feb 23 '17 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.