Skip to main content

MaterialX JSON I/O

Contents

Introduction

This is a Python package for supporting interoperability of MaterialX documents by adding support conversion to / from a JSON representation.

The JSON representation is lossless and is targeted at integrations which prefer to use JSON as a data exchange format instead of XML.

This will work against any version of MaterialX 1.39.0 or greater.

Documentation

Visit the home page for For further information about this package, including links to API documentation and a Jupyter notebook with sample code that expands on the basic Usage example.

Integrations

A plugin using this package is available as a sample as part of QuilitX as of this change [https://github.com/PrismPipeline/QuiltiX/commit/e34ebfec22a334d323803b6797beb8db581ebb18].

Installation

The Python package is losted on PyPi and can be installed using `pip``:

pip install materialxjson

or the source repository can be cloned and the package built from the command line:

python -m build

This will build a distribution folder called dist which contains a zip file which can be installed using:

pip --install <name of zip>

Usage

The following example shows bidirectional conversion:

  1. A MaterialX file in XML format is read in and written to a string in JSON format.
  2. A MaterialX file in JSON format is read in and written to a string in XML format.

Note that any JSON package can be used to perform JSON I/O. The json package is used by the utilities provided in this package.

Package Setup

The materialjson package can be loaded as follows, along with MaterialX and json packages:

import json
import MaterialX as mx
from materialxjson import core

XML to JSON Format Conversion

A MaterialX document can be read in from an XML file as follows:

import pkg_resources

# Read in MaterialX file
mtlxFileName = pkg_resources.resource_filename('materialxjson', 'data/standard_surface_default.mtlx')

doc = mx.createDocument()
mx.readFromXmlFile(doc, mtlxFileName)

and then converted to JSON format as follows by creating a MaterialXJson object and calling the documentToJSON method:

# Create I/O handler
mtlxjson = core.MaterialXJson()

# Write to JSON format
jsonObject = mtlxjson.documentToJSON(doc)

The contents of the JSON object can be extracted out using the json package as follows. (An indentation level of 2 is used to make the output more readable.)

# Convert JSON object to JSON string
jsonString = core.Util.jsonToJSONString(jsonObject)

JSON to XML Format Conversion

A JSON file can be read in as follows. In this case we load the sample file included with the package:

# Get file to load
jsonFileName = pkg_resources.resource_filename('materialxjson', 'data/standard_surface_default_mtlx.json')

# Load JSON file
jsonObject = core.Util.readJson(jsonFileName)

The JSON object is then converted to a MaterialX document and written out to an XML string as follows:

# Create I/O handler
mtlxjson = core.MaterialXJson()

# Read JSON object into document
doc = mx.createDocument()
mtlxjson.documentFromJSON(jsonObject, doc)

# Write to XML String
docstring = core.Util.documentToXMLString(doc)

Caveats

The following are some caveats to be aware of when using this package:

  • There is no equivalent support for "include files" for JSON conversion. The JSON representation is intended to be a single file representation.
  • XML comments are not preserved in the JSON representation.

Author

Metadata

Release files for materialxjson 1.39.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for materialxjson 1.39.5
File Size Uploaded Provenance Actions
materialxjson-1.39.5.tar.gz 14.6 kB Without provenance Details for materialxjson-1.39.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for materialxjson 1.39.5
File Interpreter ABI Platform Actions
materialxjson-1.39.5-py3-none-any.whl Python 3 none any Details for materialxjson-1.39.5-py3-none-any.whl

Total release size: 31.7 kB

Release files / materialxjson-1.39.5.tar.gz

Download URL materialxjson-1.39.5.tar.gz
Size 14.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e7e8e3757be070857283b20c7489f79be7610caa2c38b3b6b04ed3911532de95
BLAKE2b-256 checksum
How to use checksums
1ae54c6f5370cc0c61864f5fd0411b59317847f9c883ecd3e003f5cf73d5e325
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.0

Release files / materialxjson-1.39.5-py3-none-any.whl

Download URL materialxjson-1.39.5-py3-none-any.whl
Size 17.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a7cbbfe7b26c949d6830ddff4cdee1b479bc879753d72d9f7bc727dfbde4b325
BLAKE2b-256 checksum
How to use checksums
7f0f3aa6c231d212a51199c566bdde308d8ba9ad91f6fd3a62caa7314f0d0cfa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.0

Security for materialxjson 1.39.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page