Add-on preferences panel

When developing add-ons it is often necessary to give an ability to set a number of parameters that affect the whole add-on work to the user. For example, the user can specify a directory for saving/loading files, set some default variables or switch between add-on modes. Of course, the interface for setting such parameters can be placed in the add-on panel, but it is better to place it in a separate add-on preferences panel, which is located in the “Preferences” window under the add-on installation panel.

The main advantage of the add-on preferences is that they don’t reset when Blender restarts. The user does not need to configure the add-on preferences each time, it’s enough to set the necessary parameters once, personalizing the add-on for convenient work.

Let’s create an add-on and define a parameter, placing it in the add-on preferences panel.

At first create the simplest add-on, which adds a default cube to a scene by clicking a button in the N-panel of the 3D Viewport window.

Add-on code:

This “test” add-on works for Blender 2.8.

In this add-on we defined an operator class named “addCubeSample”, whith the identifier “mesh.add_cube_sample”.

In the “execute” method we call the system operator “mesh.primitive_cube_add”, which creates a default cube.

Creating the “addCubeSamplePanel” class we defined the “TEST” panel in the “3D Viewport” window for the user UI and placed the button for calling our operator “mesh.add_cube_sample” on it.

In the “register” and “unregister” functions we link the add-on to the Blender API.

We can install the add-on and execute it – by clicking the “Add Cube” button. A default cube will be added to the scene.

To add preferences to our add-on, we need to create a new class inheriting it from “bpy.types.AddonPreferences”:

Our add-on is placed in a single file (module), so we must set the “bl_idname” identifier to the name of the add-on module “__name__”.

If you create preferences for a multi-file add-on, you need to specify the add-on package name instead of the module name:

Let’s define a parameter that will affect the execution of our add-on — the Bevel modifier will be automatically added to the created cube if the user specifies this in the add-on preferences.

Parameters are created in the same way as for operator classes, using the types described in bpy.props: StringProperty, IntProperty, BoolProperty, etc.

Define a RadioButton switcher with two choices: “Add Bevel” – add a modifier to the cube, and “No Bevel” – don’t do that.

“add_bevel” is the parameter name, the default value – not to add a modifier.

To show this parameter on the add-on preferences panel, we need to use the “draw” function. Drawing the user interface for the add-on preferences panel is done in the same way as for common UI panels.

We added a “label” text field with the parameter description and the “add_bevel” parameter looks like a value switcher in this code.

The add-on preferences panel class must also be registered in the “register” function and unregistered in the “unregister” function.

To access the parameter from the add-on preferences in the operator class, we need to get it by specifying the add-on name and the parameter name. The following code returns the current value of the “add_bevel” property of our add-on:

Add some code to the operator class “addCubeSample” for adding a Bevel modifier according to the “add_bevel” parameter value:

The final add-on code:

Install the add-on. Now an additional “Preferences” panel appears under the add-on panel.

If the user sets the switcher to the “Add Bevel” position and saves preferences by clicking the “Save Preferences” button, the default cube created with our add-on will be added to a scene with the “Bevel” modifier by default.

5 7 votes
Article Rating
Subscribe
Notify of
guest

4 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Anonymous
Anonymous
2 years ago

This doesn’t seem to work anymore, whenever I try to add a cube I get the following error message:

The blender version I’m using is 3.2, but neither 3.3 or even 2.81 worked, is there a fix to this?

Toapy & Friends
Toapy & Friends
1 year ago
Reply to  Nikita

I had to change  context.preferences.addons[‘test’] and change ‘test wo quotes ‘to my file name that I saved and then installed into blender… to get it to work. so ‘test’ became ‘Addon preferences’. from the filename Addon preferences.py