1
$\begingroup$

I have managed to serialize materials with the following code:

from rna_xml import rna2xml
import bpy
import sys
import os

print(os.getcwd())
scn = bpy.data.scenes["Scene"]

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

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") :
        print(f)
        xml = minidom.parse(join(materials_path, f))  
        xml2rna(xml)

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?

$\endgroup$
1
  • $\begingroup$ I can serialize and deserialize any Blender material configuration in JSON. It is possible for me to save my materials in a FreeCAD file and restore all these materials in Blender from this FreeCAD file. for more information see this thread on the FreeCAD forum $\endgroup$
    – psilocybe
    Dec 10, 2023 at 9:42

1 Answer 1

2
$\begingroup$

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
$\endgroup$

You must log in to answer this question.

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