Creating Blender Add-ons variables with values saved in blend-files

All user-defined classes (panels, operators), registered in Blender API, exists only during Blender runs. After program close they are deleted from memory. Therefore, if some variables are defined in user classes, all of them will be reset after Blender restart.

However, sometimes it is necessary to use in Blender add-on variables with values that not be lost in the process of program restarting. To retain variables values, it needs to wrap them into special class – property, and attach to any object whose properties are stored in * .blend file.

Lets create a demo property-class SampleVariables, filled with a set of variables.

To convert user-defined class into complete property, it needs to inherit from bpy.types.PropertyGroup. For example define in our class two variables variable1 (numeric) and variable2 (boolean), which values must be preserved after Blender exit. Take the type of these two variables from bpy.props, so we can bring them on panel and easily change their values.

Like any user-defined class, to append our property class to the Blender API we must register it via the register_class and unregister at the end with unregister_class:

Also, lets create a panel SamplePanel, place it on the T-bar in Properties region, and createt layout whit our two variables in it:

And, most importantly, property class must be appended as a property to one of the objects with structure stored in *.blend file. This may be any object that logically related these variables. In our example, variables are abstract and not tied to any specific objects, so make our class the whole scene property.

The binding must be done at the time of add-on registration:

and deleted with add-on unregistration:

We tied our property class SampleVars as the sample_vars scene property. Therefore, in our panel class layout we specified bpy.context.scene.sample_vars as the object to interact with property variables.

Full code of demo add-on that creates two variables, binds them as properties to the scene and places them on the T-bar to change their values:

Variables with stored to *.blend file values
Variables with stored to *.blend file values

Variables placed on the panel will keep the set values after Blender restarting.

5 2 votes
Article Rating
Notify of

0 Comment
Inline Feedbacks
View all comments