I have managed to serialize materials with the following code:

from rna_xml import rna2xml
import bpy
import sys
import os

scn = bpy.data.scenes["Scene"]

for i in range(len(bpy.data.materials)):
    with open(bpy.data.materials[i].name + '.xml', "w+") as f:
        rna2xml(fw=f.write, root_node="something", root_rna=bpy.data.materials[i].node_tree)

Now I am trying to de-serialize with the following:

from xml.dom import minidom
from rna_xml import rna2xml, xml2rna
import bpy
import sys

scn = bpy.data.scenes["Scene"]

# reload test

from os import listdir
from os.path import isfile, join

materials_path = '/some/path'

onlyfiles = [f for f in listdir(materials_path) if isfile(join(materials_path, f))]

for f in onlyfiles:
    if f.startswith("Materia") and f.endswith("xml") :
        xml = minidom.parse(join(materials_path, f))  

but I get the following message:

Traceback (most recent call last):
  File "/deserialize_materials.py", line 21, in <module>
  File "/home/simone/blender/blender-2.92.0-linux64/2.92/scripts/modules/rna_xml.py", line 346, in xml2rna
    rna2xml_node(root_xml, root_rna)
  File "/home/simone/blender/blender-2.92.0-linux64/2.92/scripts/modules/rna_xml.py", line 251, in rna2xml_node
    for attr in xml_node.attributes.keys():
AttributeError: 'NoneType' object has no attribute 'keys'

How do I do this round-trip right?


1 Answer 1


Your first argument should be xml.documentElement rather than xml.

xml2rna requires a shader node tree as the root_node argument if you want to restore a material, so you have to create a material before you call it, make sure that the material has use_nodes enabled and pass it the material's node_tree: xml2rna(xml.documentElement, root_rna=material.node_tree) and material would be the new material you created.

Once you've fixed that you'll run into this bug that was closed with a To-Do added to the Blender python todo list. They symptoms are:

>>> xml2rna(xmldoc.documentElement, bpy.data.materials["blue"].node_tree)
Size Mismatch! collection: links
Size Mismatch! collection: nodes

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .