Materialx Protobuf API 1.39.5
Serialization API to convert between MaterialX and Google Protobuf formats.
Loading...
Searching...
No Matches
ProtobufToMaterialX Class Reference

Converter class to transform Protobuf MaterialXDocument messages back into MaterialX document objects. More...

#include <materialx_serializer.h>

Public Member Functions

mx::DocumentPtr convert (const MaterialXDocument &pb_doc)
 Convert a Protobuf MaterialXDocument message to a MaterialX document object.
 

Private Member Functions

mx::DocumentPtr checkVersionAndUpgrade (const MaterialXDocument &pb_doc)
 Check schema version and perform upgrades if necessary.
 
mx::ElementPtr convertElement (const MaterialXElement &pb_elem, mx::ElementPtr parent)
 Recursively convert a Protobuf MaterialXElement to a MaterialX element.
 

Detailed Description

Converter class to transform Protobuf MaterialXDocument messages back into MaterialX document objects.

  • Traverses the Protobuf message structure and reconstructs the corresponding MaterialX document hierarchy.
  • Handles attributes and child elements recursively.
  • Requires the MaterialX C++ API to create and manipulate document elements.
  • Usage: ProtobufToMaterialX converter; mx::DocumentPtr mx_doc = converter.convert(pb_doc);
  • Where pb_doc is a MaterialXDocument Protobuf message.

Member Function Documentation

◆ checkVersionAndUpgrade()

mx::DocumentPtr ProtobufToMaterialX::checkVersionAndUpgrade ( const MaterialXDocument & pb_doc)
private

Check schema version and perform upgrades if necessary.

Parameters
pb_docThe Protobuf MaterialXDocument message.
Returns
A new MaterialX document pointer.
84 {
85 mx::DocumentPtr doc = mx::createDocument();
86
87 // Check if version is set
88 if (pb_doc.has_schema_version()) {
89 uint32_t major = pb_doc.schema_version().major();
90 uint32_t minor = pb_doc.schema_version().minor();
91 uint32_t patch = pb_doc.schema_version().patch();
92
93 // Version compatibility check
94 if (major > SCHEMA_VERSION_MAJOR) {
95 std::ostringstream oss;
96 oss << "Document schema version " << major << "." << minor << "." << patch
97 << " is newer than supported version "
99 << ". Please upgrade your software.";
100 throw std::runtime_error(oss.str());
101 }
102
103 // Perform upgrades for older versions
104 if (major < SCHEMA_VERSION_MAJOR) {
105 std::cout << "Warning: Upgrading document from version "
106 << major << "." << minor << "." << patch
107 << " to " << SCHEMA_VERSION_MAJOR << "."
108 << SCHEMA_VERSION_MINOR << "." << SCHEMA_VERSION_PATCH << std::endl;
109 // Add upgrade logic here when needed
110 }
111 } else {
112 // No version info - assume oldest supported version
113 std::cout << "Warning: Document has no schema version information. Assuming legacy format." << std::endl;
114 }
115
116 return doc;
117}
constexpr uint32_t SCHEMA_VERSION_MAJOR
Definition materialx_serializer.h:24
constexpr uint32_t SCHEMA_VERSION_PATCH
Definition materialx_serializer.h:26
constexpr uint32_t SCHEMA_VERSION_MINOR
Definition materialx_serializer.h:25

◆ convert()

mx::DocumentPtr ProtobufToMaterialX::convert ( const MaterialXDocument & pb_doc)

Convert a Protobuf MaterialXDocument message to a MaterialX document object.

Parameters
pb_docThe Protobuf MaterialXDocument message to convert.
Returns
The reconstructed MaterialX document pointer.
67 {
68 // Check version compatibility and upgrade if needed
69 mx::DocumentPtr doc = checkVersionAndUpgrade(pb_doc);
70
71 // Set document attributes
72 for (const Attribute& pb_attribute : pb_doc.attributes()) {
73 doc->setAttribute(pb_attribute.key(), pb_attribute.value());
74 }
75
76 // Convert all elements
77 for (const MaterialXElement& pb_element : pb_doc.elements()) {
78 convertElement(pb_element, doc);
79 }
80
81 return doc;
82}
mx::DocumentPtr checkVersionAndUpgrade(const MaterialXDocument &pb_doc)
Check schema version and perform upgrades if necessary.
Definition materialx_serializer.cpp:84
mx::ElementPtr convertElement(const MaterialXElement &pb_elem, mx::ElementPtr parent)
Recursively convert a Protobuf MaterialXElement to a MaterialX element.
Definition materialx_serializer.cpp:119

◆ convertElement()

mx::ElementPtr ProtobufToMaterialX::convertElement ( const MaterialXElement & pb_elem,
mx::ElementPtr parent )
private

Recursively convert a Protobuf MaterialXElement to a MaterialX element.

Parameters
pb_elemThe Protobuf MaterialXElement message to convert.
parentThe parent MaterialX element to which the new element will be added.
Returns
The newly created MaterialX element pointer.
119 {
120 // Create element
121 mx::ElementPtr mx_elem = parent->addChildOfCategory(pb_elem.type(), pb_elem.name());
122
123 // Set all attributes preserving order
124 for (const Attribute& attr : pb_elem.attributes()) {
125 mx_elem->setAttribute(attr.key(), attr.value());
126 }
127
128 // Recursively convert children
129 for (const MaterialXElement& pb_child : pb_elem.children()) {
130 convertElement(pb_child, mx_elem);
131 }
132
133 return mx_elem;
134}

The documentation for this class was generated from the following files: