|
Materialx Protobuf API 1.39.5
Serialization API to convert between MaterialX and Google Protobuf formats.
|
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:
This project provides tools to:
.mxpb filesInstall Protocol Buffers compiler:
Install Protocol Buffers compiler:
Install Protocol Buffers compiler:
For the C++ version, you'll need:
choco install protobuf or use vcpkgbrew install protobufsudo apt-get install libprotobuf-dev protobuf-compilerSee cpp/README.md for detailed C++ build instructions.
The schema defines two main message types:
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:
This generates materialx_pb2.py which contains the Protocol Buffer message classes.
For C++ development, generate C++ protobuf code:
This generates materialx.pb.h and materialx.pb.cc in the cpp directory.
Use the provided script to generate both Python and C++ code:
The library provides both Python and C++ implementations with identical functionality.
Convert a MaterialX document to Protobuf format:
Or using the installed command:
This 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)Outputs:
standard_surface_chess_set.json - JSON formatstandard_surface_chess_set.mxpb - Binary protobufstandard_surface_chess_set_diagram.mmd - Mermaid diagramConvert a Protobuf binary file back to MaterialX XML:
This will generate a MaterialX XML file named <document>_from_pb.mtlx.
The C++ version provides the same functionality with identical command-line arguments:
See cpp/README.md for detailed build instructions.
All command-line options are identical between Python and C++ versions.
Both Python and C++ versions support these options:
| Option | Short | Description |
|---|---|---|
--json | -j | Output JSON representation of the Protobuf document |
--convert_back | -cb | Convert back to MaterialX after Protobuf conversion |
--write_mermaid | -wm | Output Mermaid diagram of the Protobuf document |
--output_folder | -of | Output folder for converted files |
--debug_pb_doc | -dbg | Enable debug output (1=simple, 2=compact, 3=full) |
The library is implemented in both Python and C++ with equivalent functionality.
Converts MaterialX documents to Protocol Buffer format:
Converts Protocol Buffer format back to MaterialX:
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 visualizationThe C++ version provides the same functionality with native performance:
MaterialXToProtobuf - Converts MaterialX to ProtobufProtobufToMaterialX - Converts Protobuf to MaterialXUtil - Serialization, JSON export, and debug toolsVersionUpgrader - Schema version managementSee cpp/README.md for C++-specific details.
The repository includes sample MaterialX documents:
standard_surface_chess_set.mtlx - Chess set with standard surface shaderunlit_cross.mtlx - Simple unlit cross shaderEach 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.