Save python variables within .blend?

I was wonder if there is a way I can do a save state to save variables in my python script into blender’s .blend files.

this doesn’t save it in the blend file, but in a config file, but still, it might work,
you might want to type ‘help(B.Registry.SetKey/GetKey)’ then run it also:

 import Blender as B

dict = {"1":0}
B.Registry.SetKey("TestEntry", dict, True)


import Blender as B

dict = B.Registry.GetKey("TestEntry", True)
print dict

this isn’t much but it might be what you want. Note that the data being stored is a dictionary

I think blender saves ID properties with the files (not 100% sure on this though). You might try assigning the values to a dummy object and then retrieving them later.

If not, you might just have to keep up with them in a file unfortunately.

Oh, yeah, you could always have the script write the variables to a text object (as in a text editor object, not a 3D text like in the viewport) and then reinterpret them later, probably using eval/exec. That would probably be a slightly hacky way of doing it, but it would work.

Thanks, I’ll guess I’ll serialize python objects and save them into a text-editor object in the blend files.

Just to have this here as well - I recently used some ID properties and found it’s pretty easy to do. Might be worth a try:

Additional info in the API doc:
http://www.blender.org/documentation/245PythonDoc/IDProp.IDGroup-class.html

Quick usage overview:


###################
# storing properties in the scene (this works as well in objects, armatures, etc...; "properties" is of the type "IDGroup")

sc = Blender.Scene.GetCurrent()
props = sc.properties

if ("your_stored_value" in props):
    value = props["your_stored_value"]
    props["your_stored_value"] = 123

# new value
props["your_stored_value2"] = 123
props["your_stored_value2"] = 0.123
props["your_stored_value2"] = "testtext"
props["your_stored_value2"] = [0, 0, 1.0, 0]

###################
# getting a subgroup in properties
#
# Example:
# properties (IDGroup)
#  * subgroupname (IDGroup)
#    -> var1
#    -> var2
#  * otherstuff
#

subprops = sc.properties["subgroupname"]
if ("var1" in subprops):
    value = subprops["var1"]
    subprops["var1"] = 123

I don’t want to show off, but I have made a framework for this. It can automatically convert user-supplied GUI parameters to a Python class, and stores the Python class as a string in the .blend file.
If this is what you like to do, you can contact me (the latest version is not on the web yet, and lacking in documentation).