Python utilities for converting MaterialX documents to and from Protocol Buffer format.
Protocol Buffers (protobuf) is Google's mechanism for serializing structured data.
Protobuf focuses on data integrity, version safety, and cross-language consistency. Key benefits include:
- Compact: Binary format is smaller than XML
- Fast: Efficient serialization and deserialization
- Cross-platform: Language-agnostic format works across different platforms and programming languages
- Typed: Strong typing for data integrity
- Versioning: Built-in support for schema versioning and backward compatibility
This project provides tools to:
- Convert MaterialX XML documents to Protocol Buffer format
- Serialize MaterialX documents to binary
.mxpbfiles - Export MaterialX documents to JSON format
- Convert Protocol Buffer documents back to MaterialX XML
- Generate visual diagrams of MaterialX document structure
Install Protocol Buffers compiler:
brew install protobufInstall Protocol Buffers compiler:
choco install protocInstall Protocol Buffers compiler:
sudo apt-get install -y protobuf-compiler- Install Python protobuf library:
pip install --only-binary=all protobuf
- Install MaterialX Python library:
pip install MaterialX
The schema defines two main message types:
- Attribute: String key value pair. This is added to allow usage of repeated attributes as opposed to usage of maps. Maps in ProtoBuf are ordered which is undesirable for MaterialX where attribute order matters for types such as node inputs and outputs as these define the signature of the node.
- MaterialXElement: Represents a MaterialX element with name, type, attributes, and child elements.
- MaterialXDocument: Represents the root of the element hierarchy and contains top-level attributes.
This schema reflects the simplicity of the MaterialX representation.
A schema Version message is also provided for version management including allowing for an upgrade mechanism if ever required.
The schema is defined in the materialx.proto file.
Using the materialx.proto, Python bindings can be generated with the following command:
protoc --python_out=. materialx.protoThis generates materialx_pb2.py which contains the Protocol Buffer message classes.
These classes are used in the conversion logic to serialize and deserialize MaterialX documents.
- materialx.proto # Protocol Buffer schema definition
- materialx_pb2.py # Generated Python protobuf bindings
- materialx_serializer.py # Conversion logic between MaterialX and Protobuf
- main.py # Command-line utility for conversion
- *.mtlx # Sample MaterialX documents
Convert a MaterialX document to Protobuf format:
python main.py path/to/<document>.mtlxThis command can optionally generate four output files:
.json- JSON representation.mxpb- Binary Protocol Buffer format (compact serialization).mmd- Mermaid diagram_converted.mtlx- Round-trip conversion back to MaterialX XML (for validation)
python main.py standard_surface_chess_set.mtlxOutputs:
standard_surface_chess_set.json- JSON formatstandard_surface_chess_set.mxpb- Binary protobufstandard_surface_chess_set_converted.mtlx- Converted back to MaterialX
Convert a Protobuf binary file back to MaterialX XML:
python main.py path/to/<document>.mxpbThis will generate a MaterialX XML file named <document>_from_pb.mtlx.
This command can optionally generate two output files:
.json- JSON representation.mmd- Mermaid diagram
Converts MaterialX documents to Protocol Buffer format:
- Extracts document-level attributes
- Traverses the MaterialX document
- Extracts element names, types, and attributes
- Recursively converts child elements
Converts Protocol Buffer format back to MaterialX:
- Creates MaterialX document
- Reconstructs MaterialX document attributes
- Restores element hierarchy and attributes
Utility functions for debugging and visualization:
from_string()- PB doc from stringto_string()- PB doc to stringto_json()- PB doc to jsongenerate_mermaid_diagram()- Generate Mermaid diagrams for visualization
The repository includes sample MaterialX documents:
standard_surface_chess_set.mtlx- Chess set with standard surface shaderunlit_cross.mtlx- Simple unlit cross shader
Each sample includes its converted outputs as appropriate (.json, .mxpb, _converted.mtlx, from_pb.mtlx).
Enable Mermaid diagram generation in main.py by setting write_mermaid = True. This generates a .md file with a Mermaid diagram showing the document structure.