Saving blender data with file?

Hi,
As the title states I would like to save some of blender’s data with the .blend file that is open. I know that blender’s properties are saved with the file, but I can’t find the “correct” way to save a material object, or a scene obj etc. In the StringProperty I can only save a string, a string which also could be evaluated to be a list or something, but not a material. I’m thinking that a PointerProperty pointing to my own PropertyGroup could do it, which in turn stores some attributes pointing to different objects such as materials etc. However, I can’t seem to change the attributes’ value if the values are not blender’s properties.

For example:


import bpy


class MyData(bpy.types.PropertyGroup):
    a = bpy.props.StringProperty()
    b = ""


bpy.utils.register_class(MyData)


bpy.types.Scene.my_data = bpy.props.PointerProperty(type=MyData)


bpy.context.scene.my_data.a = "something"
bpy.context.scene.my_data.b = "nothing"


If I now check the values in the console:


bpy.context.scene.my_data.a
>>> 'something'

bpy.context.scene.my_data.a
>>> ''

So the main question stands: What is the proper way of saving blender’s data (such as a material or scene object) with the .blend file?

I’m not sure what you mean, Blender stores materials etc. automatically. You can’t create something like a MaterialProperty with Python, only the available bpy.props properties can be used. PointerProperty can be used to group own properties, but it cannot store a reference to an ID datablock (there is no IDProperty), see http://blender.stackexchange.com/questions/6975/is-it-possible-to-use-bpy-props-pointerproperty-to-store-a-pointer-to-an-object

ID datablocks are managed by C code, not Python. All you can do is store the name of a datablock in a StringProperty to later refer to it - but if you rename the datablock, the string prop will remain the same and the “link” be broken. Only C code can set up properties that store actual references / pointers to ID datablocks.

Oh, okay, I guess that answers my question. In my add-on I want to hold references to data in blender which would be saved with the blend file so that I would not have to rely on names which can be changed… Thanks for the help!

Let’s say I wanted to do this in C then, I could write that small part of my add-on in C and then use it together with the rest in python? Or do I have to write everything in C then?

Just the property definition in C, but it can’t be an addon. You will need to compile whole Blender to use it (there is no interface for C plugins/addons).

Is it usual in this community to do so? If you want to share it with others, they’ll need your blender build then right?

Yes they will need the custom build. It’s unlikely to do so, except for like game studios, that need a custom build anyway.
You should keep in mind that you may sacrifice compatibility. It should not be much of an issue actually, but you might loose the custom data if you save it in a standard Blender - I believe it preserves everything however.

Okay. Why do game studios need a custom build anyway? And do you then mean game studios who use blender game engine, or do you also mean those who only use blender for modeling/animation in their game production? I’m just very curious about this topic… Thank you.

Well, SOME game studios MIGHT use custom builds, because they may need custom features for production. This could also be an “unstable” version (compiled from latest source code) if a certain required feature is not yet available in a stable release, or a ported version of a branch, that didn’t get to trunk. What I meant to say was that it’s more likely in game studios or similar, so a closed group of people with someone that manages the process and decides whether they need a custom build or not, and everyone will use it then. Random people may not be willing to use a custom build just for an addon, or go back to official builds if there are no regular custom build releases to keep up with Blender’s development.

Oh I see. Thanks a lot for all the help! I really appreciate it!