1

I'm looking into serializing node trees as XML for my current project. I saw that there is the rna_xml module that allows that and here's my current take so far.

import rna_xml
import xml.dom.minidom

# Retrieve XML fixture and create new node tree
def fetch_material(name):
    try:
        return MATERIALS['name']
    except KeyError:
        material_path = MATERIALS_PATH + name + '.xml'
        xml_nodes = xml.dom.minidom.parse(material_path)
        material = create_cycles_material(name, clean=True)
        root = xml_nodes.getElementsByTagName("ShaderNodeTree")[0]
        tree = bpy.data.node_groups.new(name, type='ShaderNodeTree')
        rna_xml.xml2rna(root, root_rna=bpy.data.node_groups[-1])
        group_node = material.node_tree.nodes.new('ShaderNodeGroup')
        group_node.node_tree = bpy.data.node_groups[-1]
        MATERIALS['name'] = material
        return material

# Save material to new xml file (local helper, do not run in production)
def serialize_material(obj, name):
    print("Serializing node tree")
    path = MATERIALS_PATH + name + '.xml'
    f = open(MATERIALS_PATH + name + '.xml',"w+")
    rna_xml.rna2xml(fw=f.write, root_node="Root", root_rna=obj.data.materials[-1].node_tree, method='ATTR')
    f.close()
    return path

This properly serializes the node tree as .xml file but when I load it back I dont know how to get it properly. The only way I found is to load the node tree into a new node group but that results in an empty node_group.

Any of you guys have dealt with this sort of thing before? Thanks

paascal
  • 121
  • 5
  • 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 – psilocybe Dec 10 '23 at 09:22

1 Answers1

1

After studying the source code of that module I realized that it is unable to create new Node objects since it only assigns attributes to pre-existing structures.

I decided to get around that problem by having a collection of materials in another scene in the same .blend file. Hacky as hell but it works.

paascal
  • 121
  • 5