Utilities handling I/O of MaterialX in JSON format
Project description
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.
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:
- A MaterialX file in
XMLformat is read in and written to a string inJSONformat. - A MaterialX file in
JSONformat is read in and written to a string inXMLformat.
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
- LinkedIn: Bernard Kwok
- GitHub: kwokcb
- Email: kwokcb@gmail.com
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file materialxjson-1.39.2.tar.gz.
File metadata
- Download URL: materialxjson-1.39.2.tar.gz
- Upload date:
- Size: 14.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1fe32ed9a1364fcef18cb17b7770f77c075b790e24e2e3c5e4c65e30d3e82572
|
|
| MD5 |
66d291021c81e61f8b8507497b6e6cba
|
|
| BLAKE2b-256 |
6e3741682cef16f08de3724bb8ca4ce267c45b52cacb41c841eb2b65969bf5a2
|
File details
Details for the file materialxjson-1.39.2-py3-none-any.whl.
File metadata
- Download URL: materialxjson-1.39.2-py3-none-any.whl
- Upload date:
- Size: 17.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6abe11eb7f0281caf0ae042692c36566531ac71795338c98d02cf7fefdb7f721
|
|
| MD5 |
1a8b37799af7f43aabb13cbfe6916a26
|
|
| BLAKE2b-256 |
18dc47e7269662b565ba20c0118e5347d1b77a16a5cc9a0f74bfe4937fd8c050
|