Monday, February 2, 2015

Material IO: The File Format

I just released my Material IO add-on which exports and imports a .bmat file type which is specific to this add-on. It means Blender material and I created it so that the script could be sure that it was getting a file that it could read and understand.

I was originally going to have it just be a plain text file, but then realized that it would take a lot of work to convert it back into a usable format. So I stumbled across XML and it looked very easy to use, which it is. Python has a built-in set of tools, called ElementTree, to work with XML. So then I had to work on a layout out for the file and this is what I ended up with:


You have the overall group which is always called "material", it has several attributes that let the script importing know which order to create the groups in, what images to load, the name of the material and then the other information is just there for if someone just wants to read the file.

Then within material you have all the different groups, the main node_tree is also counted as a group and is always called "main", within each group you have a element for nodes and one for links. Inside of those two you have the majority of the information. The script uses ast.literal_eval() extensively for converting those strings back into usable lists and tuples.

The .write of ElementTree writes it all as a single line which is basically impossible to read. So the add-on writes it out that way, then reads the file using xml.dom.minidom which has a method called toprettyxml() which formats all the text in the file as an easier to read, multi-lined, file. Which is then written back to the file location.

No comments:

Post a Comment