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):
        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')
    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


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.