store PropertyGroup in an external file i tried to use python pickle to dump a PropertyGroup to a file and try to restore it, but pickle can't dump the class and gives an error:

_pickle.PicklingError: Can't pickle &lt;class '__main__.SI_type'&gt;: attribute lookup
 __main__.SI_type failed


is there a way to dump a PropertyGroup to a file, or it's values then reconstruct the object on load, maybe?



example with the problem:

import bpy
from bpy.props import *

print("-"*60)

class MyPropertyGroup(bpy.types.PropertyGroup):
    name = StringProperty()
    index = IntProperty()
    value = FloatProperty()

bpy.utils.register_class(MyPropertyGroup)

class Mytype(bpy.types.PropertyGroup):
    lista = CollectionProperty(type=MyPropertyGroup)
    listaIndex = IntProperty(default=0)

bpy.utils.register_class(Mytype)
bpy.types.Scene.Mytype = PointerProperty(type = Mytype)

mytypes = bpy.context.scene.Mytype

item = mytypes.lista.add()
item.name = 'foo'
item.index = 42
item.value = 3.1416

dict_test = bpy.context.scene.Mytype["lista"].to_dict()
#AttributeError: 'list' object has no attribute 'to_dict'

import pickle

bytes_test = pickle.dumps(dict_test)

del mytypes["lista"]

dict_test = pickle.loads(bytes_test)

mytypes["lista"] = dict_test

print(
    "name: %s"%mytypes["lista"].test.name,
    "index: %d"%mytypes["lista"].test.index,
    "value: %f"%mytypes["lista"].test.value,
    sep="\n"
    )
Try to pickle the custom property which saves the data. You can retrieve the dictionary from it with the .to_dict() method.

import bpy

print("-"*79)

class MyPropertyGroup(bpy.types.PropertyGroup):
    name = bpy.props.StringProperty()
    index = bpy.props.IntProperty()
    value = bpy.props.FloatProperty()


bpy.utils.register_class(MyPropertyGroup)
bpy.types.Object.test = \
    bpy.props.PointerProperty(type=MyPropertyGroup)

obj = bpy.context.object
#assign some data to test!
obj.test.name = "foo"
obj.test.index = 42
obj.test.value = 3.1416

#if you haven't assigned any data to the attributes of obj.test
#obj["test"] may not exist
#the expression statement
obj.test
#will create obj["test"] with default values then
dict_test = obj["test"].to_dict()

import pickle

bytes_test = pickle.dumps(dict_test)

del obj["test"]
#no data - returns default values
print(
    'del obj["test"]',
    "name: %s"%obj.test.name,
    "index: %d"%obj.test.index,
    "value: %f"%obj.test.value,
    "\n",
    sep="\n"
    )

dict_test = pickle.loads(bytes_test)
obj["test"] = dict_test
print(
    'obj["test"] = dict_test',
    "name: %s"%obj.test.name,
    "index: %d"%obj.test.index,
    "value: %f"%obj.test.value,
    sep="\n"
    )