Data persistance?

Hi all, very quick question -
I’m storing data in my add-on, but i need it to persist across blend saving. In addition, i need to use a list, and not just a string …
How do i save this to an object and the scene, and have the data save?

Can you use RNA/ID properties to do it? They’re preserved when you save a blend file.
http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Cookbook/Code_snippets/Properties
This first link talks about RNA and ID properties in general
http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Cookbook/Code_snippets/Interface
This second link talks about in relation to the UI. Specifically, check out the “Panel Properties” and “Using Scene Properties to Store Information” section

Not really :confused:
The problem is that when i create logic, it stores a list / dic on the scene to tell it what has been added. Therefore, there isn’t really another way :frowning:

I typically use objects for persistent data. So come up with a unique name and create an object that will hold your data. In my Bleeble AddOn I look for any objects in the scene that have my “prefix” bleeble_ as part of their name. When the object is found, I know it has my custom properties and I fetch the persistent data at that time.

Here is an example using the default cube:


import bpy

ob = bpy.data.objects["Cube"]

my_list = []
my_list.append(["item1","value1"])

# By simply accessing a previously unused custom property name it is created.
ob["my_list"] = my_list

# NOTE: You should not reference a custom property before it is created.
print (ob["my_list"])

Because it is an object in the scene it will be saved along with everything else.

However, you cannot store a dict on these objects, as you have shown. Likewise, you cannot store anything but a float / int in the list :frowning:
I tried using JSON, but the interpreter is saying strings don’t support item assignment, showing that json isn’t working :frowning:

It would probably be a little slow, but you could convert your dict to a str and save that. Then when you load the file back up, convert the str back into a dict. Wouldn’t that work?

Isn’t there a way to copy the data from your dict and store it, in some clever way, in the list? Then when it comes fetching time just recreate the dict from the persistent list…just brainstorming here I don’t know what kinds of data types you are expecting to make persistent.

What if you just stored the name, type and parameters of the logic brick in a list.
Create a scene_on_load handler and put some code in there.
The load code could grab the list from the persistent object.
Examine the scene for the existence of a logic brick named like our list entry.
If the brick does not exist, it could create it. If the brick already exists just populate its parameters from the persistent data.

I’ve managed to use JSON.
I was going to use the str()
it turns out that when you enable and disable an addon it doesn’t reload the source :L
Big mistake :wink: