Materialx Protobuf API 1.39.4
Serialization API to convert between MaterialX and Google Protobuf formats.
Loading...
Searching...
No Matches
materialx_serializer.Util Class Reference

Public Member Functions

 from_string (data)
 Convert string data to Protobuf document.
 to_string (pb_doc)
 Convert Protobuf document to string.
 to_json (pb_doc, indent=2)
 Convert Protobuf document to JSON string.

Static Public Member Functions

 debug_inspect (pb_doc, max_depth=10, _current_depth=0)
 debug_inspect_compact (pb_doc, max_depth=10, _current_depth=0)
 debug_inspect_simple (pb_doc)
 generate_mermaid_diagram (pb_doc)
 Generate a Mermaid diagram (graph LR) from a protobuf document hierarchy.

Member Function Documentation

◆ debug_inspect()

materialx_serializer.Util.debug_inspect ( pb_doc,
max_depth = 10,
_current_depth = 0 )
static
197 def debug_inspect(pb_doc, max_depth=10, _current_depth=0):
198
199 data = MessageToDict(pb_doc)
200
201 def print_element(element, depth):
202 indent = " " * depth
203 name = element.get('name', 'unnamed')
204 elem_type = element.get('type', 'no-type')
205 print(f"{indent}{name} ({elem_type})")
206 # Print attributes (now a list of dicts)
207 attrs = element.get('attributes', [])
208 for attr in attrs:
209 key = attr.get('key', '')
210 value = attr.get('value', '')
211 print(f"{indent} │ {key}: {value}")
212 # Print children recursively
213 children = element.get('children', [])
214 if children:
215 print(f"{indent} └─ Children ({len(children)}):")
216 for child in children:
217 print_element(child, depth + 2)
218
219 pb_version = pb_doc.schema_version
220 print("Schema Version:", f"{pb_version.major}.{pb_version.minor}.{pb_version.patch}")
221
222 attribs = ", ".join([f"{attr.get('key', '')}={attr.get('value', '')}" for attr in data.get('attributes', [])])
223 print("Document:" + (f" [{attribs}]" if attribs else ""))
224
225 for element in data.get('elements', []):
226 print_element(element, 0)
227
228 return data
229

◆ debug_inspect_compact()

materialx_serializer.Util.debug_inspect_compact ( pb_doc,
max_depth = 10,
_current_depth = 0 )
static
231 def debug_inspect_compact(pb_doc, max_depth=10, _current_depth=0):
232
233 data = MessageToDict(pb_doc)
234
235 def print_attributes(element, depth):
236 indent = " " * depth
237 name = element.get('name', '')
238 elem_type = element.get('type', '')
239 if elem_type:
240 elem_type = f'({elem_type})'
241 attrs = element.get('attributes', [])
242 attrs_preview = ", ".join([f"{attr.get('key', '')}={attr.get('value', '')}" for attr in attrs[:2]])
243 if attrs_preview:
244 attrs_preview = f" [{attrs_preview}]"
245 print(f"{indent}{name} {elem_type}{attrs_preview}")
246
247 def print_element(element, depth):
248 if depth > max_depth:
249 return
250 # Compact line showing key info
251 print_attributes(element, depth)
252 children_count = len(element.get('children', []))
253
254 # Recursively print children
255 for child in element.get('children', []):
256 print_element(child, depth + 1)
257
258 pb_version = pb_doc.schema_version
259 print("Schema Version:", f"{pb_version.major}.{pb_version.minor}.{pb_version.patch}")
260
261 attribs = ", ".join([f"{attr.get('key', '')}={attr.get('value', '')}" for attr in data.get('attributes', [])])
262 print("Document:" + (f" [{attribs}]" if attribs else ""))
263
264 for element in data.get('elements', []):
265 print_element(element, 0)
266
267 return data
268

◆ debug_inspect_simple()

materialx_serializer.Util.debug_inspect_simple ( pb_doc)
static
270 def debug_inspect_simple(pb_doc):
271
272 def print_tree(element, prefix="", is_last=True):
273 """Print tree structure with branches"""
274 connector = "└── " if is_last else "├── "
275 print(f"{prefix}{connector}{element.get('name', 'unnamed')} ({element.get('type', 'no-type')})")
276
277 # Update prefix for children
278 new_prefix = prefix + (" " if is_last else "│ ")
279
280 # Print children
281 children = element.get('children', [])
282 for i, child in enumerate(children):
283 print_tree(child, new_prefix, i == len(children) - 1)
284
285 data = MessageToDict(pb_doc)
286
287 pb_version = pb_doc.schema_version
288 print("Schema Version:", f"{pb_version.major}.{pb_version.minor}.{pb_version.patch}")
289
290 attribs = ", ".join([f"{attr.get('key', '')}={attr.get('value', '')}" for attr in data.get('attributes', [])])
291 print("Document:" + (f" [{attribs}]" if attribs else ""))
292 for i, element in enumerate(data.get('elements', [])):
293 print_tree(element, "", i == len(data.get('elements', [])) - 1)
294
295 return data
296

◆ from_string()

materialx_serializer.Util.from_string ( data)

Convert string data to Protobuf document.

175 def from_string(data):
176 """Convert string data to Protobuf document."""
177 pb_doc = pb.MaterialXDocument()
178 pb_doc.ParseFromString(data)
179 return pb_doc
180

◆ generate_mermaid_diagram()

materialx_serializer.Util.generate_mermaid_diagram ( pb_doc)
static

Generate a Mermaid diagram (graph LR) from a protobuf document hierarchy.

  • Converts the protobuf message to a dict via google.protobuf.json_format.MessageToDict.
  • Creates one node per element labeled "name : type" (node IDs use the element name with spaces replaced by underscores).
  • Connects each element to its children with directed edges.
    Note
    This representation can be less readable when elements have many input/output children, as they are rendered as regular child nodes.
    Parameters
    pb_docProtobuf message describing the document; expected schema:
    • elements: list of elements, each with:
      • name: str
      • type: str
      • children: list of elements (same structure)
    Returns
    str Mermaid diagram source code.
298 def generate_mermaid_diagram(pb_doc):
299 """
300 @brief Generate a Mermaid diagram (graph LR) from a protobuf document hierarchy.
301 @details
302 - Converts the protobuf message to a dict via google.protobuf.json_format.MessageToDict.
303 - Creates one node per element labeled "name : type" (node IDs use the element name with spaces replaced by underscores).
304 - Connects each element to its children with directed edges.
305 @note This representation can be less readable when elements have many input/output children, as they are rendered as regular child nodes.
306 @param pb_doc Protobuf message describing the document; expected schema:
307 - elements: list of elements, each with:
308 - name: str
309 - type: str
310 - children: list of elements (same structure)
311 @return str Mermaid diagram source code.
312 """
313 data = MessageToDict(pb_doc)
314 mermaid_lines = ["graph LR"]
315
316 def add_mermaid_elements(element):
317 node_id = element.get('name').replace(' ', '_')
318 label = f"{element.get('name')} : {element.get('type')}"
319 mermaid_lines.append(f" {node_id}[{label}]")
320
321 for child in element.get('children', []):
322 child_id = child.get('name').replace(' ', '_')
323 add_mermaid_elements(child)
324 mermaid_lines.append(f" {node_id} --> {child_id}")
325
326 for element in data.get('elements', []):
327 add_mermaid_elements(element)
328
329 mermaid_code = "\n".join(mermaid_lines)
330
331 return mermaid_code
332

◆ to_json()

materialx_serializer.Util.to_json ( pb_doc,
indent = 2 )

Convert Protobuf document to JSON string.

185 def to_json(pb_doc, indent=2):
186 """Convert Protobuf document to JSON string."""
187 return MessageToJson(pb_doc, indent)
188

◆ to_string()

materialx_serializer.Util.to_string ( pb_doc)

Convert Protobuf document to string.

181 def to_string(pb_doc):
182 """Convert Protobuf document to string."""
183 return (pb_doc.SerializeToString())
184

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