How to serialize simple materials? I have a bunch of Blender files with similar content. The files share a lot of common objects with identical names. I would like to propagate materials between the files. I plan to achieve this with a script that maintains a database of materials and associates them with objects based on their names.

All the materials will probably be simple - just diffuse and specular components. Some of them will perhaps also have a texture. However, it seems that rna2xml does not work on such a simple material:

for o in context.scene.objects:
    for m in o.material_slots:
        rna2xml(root_node="Material", root_rna=m.material)


Here's the output:

&lt;Material&gt;
  &lt;texture_paint_images&gt;
  &lt;/texture_paint_images&gt;
  &lt;texture_paint_slots&gt;
  &lt;/texture_paint_slots&gt;
  &lt;texture_slots&gt;
  &lt;/texture_slots&gt;
&lt;/Material&gt;


Looks a bit too empty (no diffuse, specular components).

Moreover, it seems that Material does not have _ _dict__ attribute nor an _asdict() method, and _ _slots__ appears to be empty. Well, theoretically could look through the attribute list provided by dir(), but I'm probably approaching this issue from the wrong side already.

Edit: For completeness, I also verified that pickle produces identical output for materials with different diffuse colors.

Except for hard-coding material parameters (which should be simple in my case - because I just need simple materials anyway), are there any ideas or suggestions?

I'm using Blender 2.76 on Windows 7 x64.
Decided to try the dir() approach until someone proposes a better solution. Seems to be working for simple materials without textures, as well as some modifiers, etc. To me this looks pretty dangerous, tho.

In order to later update Blender objects based on the generated dict, I had to skip read-only properties.

def bl_to_dict(bl_object, ignore_getters=True):
    """Generic Blender object to dict converter."""
    skip_attributes = ['__doc__', '__module__', '__slots__']
    supported_basetypes = [
        "&lt;class 'bool'&gt;", 
        "&lt;class 'int'&gt;", 
        "&lt;class 'float'&gt;", 
        "&lt;class 'str'&gt;", 
        "&lt;class 'list'&gt;",
        "&lt;class 'set'&gt;"
        ]
    # TODO:: Add support for more types for which casting helps.
    supported_casts = {
        "&lt;class 'Color'&gt;": 'list'
    }
    # TODO:: Figure out how to support bpy_prop_array, bpy_prop_collection, etc.
    # TODO:: Perhaps recurse on members of more complex classes.
    d = {}

    attributes = [x for x in dir(bl_object) if x not in skip_attributes]
    for name in attributes:
        # Get attribute value.
        val = getattr(bl_object, name)
        # Ignore getter properties.
        if ignore_getters:
            try:
                setattr(bl_object, name, val)
            except AttributeError as err:
                continue
        # Check against supported types.
        type_str = str(type(val))
        if type_str in supported_basetypes:
            d[name] = val
        elif type_str in supported_casts:
            d[name] = eval(supported_casts[type_str])(val)
        #else:
        #    print("Type {} of attribute {} not supported yet.".format(type(val), name))
    return d

def dict_to_bl(bl_obj, bl_dict):
    """Update a Blender object from a dict."""
    for key, val in bl_dict.iteritems():
        setattr(bl_obj, key, val)
    return bl_obj

# From here on it's easy to either convert the dict 
# to a string, to pickle, json, p-code or whatever it.


Disclaimer: The code is provided as-is. It's not guaranteed to work or anything. Getting and resetting random properties (basically what this code does) might seriously mess up stuff in Blender.