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