The following utility is provided to display formatted data.
from IPython.display import display_markdown
def displaySource(title, string, language='xml', open=True):
text = '<details '
text = text + (' open' if open else '')
text = text + '><summary><b>' + title + '</b></summary>\n\n' + '```' + language + '\n' + string + '\n```\n' + '</details>\n'
display_markdown(text, raw=True)
The basic requirement is to import the following packages:
materialxjson
MaterialX
json
In the example below we import the packages and display the version of each. Additionaly we query the help on the core
module of the materialxjson
package.
import MaterialX as mx
from materialxjson import core
import json
import materialxjson as mtlxjson
import sys, io
stdout = sys.stdout
print("MaterialX version: " + mx.getVersionString())
print("JSON version: " + json.__version__)
print("MaterialXJSON version: " + mtlxjson.__version__)
help(core.MaterialXJson)
help(core.Util)
MaterialX version: 1.39.0 JSON version: 2.0.9 MaterialXJSON version: 0.0.2 Help on class MaterialXJson in module materialxjson.core: class MaterialXJson(builtins.object) | Class for handling read and write of MaterialX from and to JSON | | Methods defined here: | | documentFromJSON(self, jsonDoc: dict, doc: MaterialX.PyMaterialXCore.Document, readOptions: materialxjson.core.JsonReadOptions = None) -> bool | @brief Convert a JSON document to MaterialX | @param jsonDoc The JSON document to read | @param doc The MaterialX document to write to | @param readOptions The read options to use. Default is None | | documentFromJSONString(self, jsonString: str, doc: MaterialX.PyMaterialXCore.Document, readOptions: materialxjson.core.JsonReadOptions = None) -> bool | @brief Convert a JSON document to MaterialX | @param jsonString The JSON string to read | @param doc The MaterialX document to write to | @param readOptions The read options to use. Default is None | @return True if successful, false otherwise | | documentToJSON(self, doc: MaterialX.PyMaterialXCore.Document, writeOptions: materialxjson.core.JsonWriteOptions = None) -> dict | Convert an MaterialX XML document to JSON | @param doc The MaterialX document to convert | @param writeOptions The write options to use. Default is None | @return The JSON document | | documentToJSONString(self, doc: MaterialX.PyMaterialXCore.Document, writeOptions: materialxjson.core.JsonWriteOptions = None) -> str | Convert an MaterialX XML document to JSON string | @param doc The MaterialX document to convert | @param writeOptions The write options to use. Default is None | @return The JSON string | | elementFromJSON(self, node: dict, elem: MaterialX.PyMaterialXCore.Element, readOptions: materialxjson.core.JsonReadOptions = None) -> None | @brief Convert an JSON element to MaterialX | @param node The JSON element to read | @param elem The MaterialX element to write to | @param readOptions The read options to use. Default is None | | elementToJSON(self, elem: MaterialX.PyMaterialXCore.Element, jsonParent: dict, writeOptions: materialxjson.core.JsonWriteOptions = None) -> dict | @brief Convert an MaterialX XML element to JSON. | Will recursively traverse the parent/child Element hierarchy. | @param elem The MaterialX element to convert | @param jsonParent The JSON element append to | @param writeOptions The write options to use. Default is None | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) Help on class Util in module materialxjson.core: class Util(builtins.object) | Utility class for MaterialX JSON | | Static methods defined here: | | documentToXMLString(doc: MaterialX.PyMaterialXCore.Document) -> str | @brief Convert a MaterialX document to XML string | @param doc The MaterialX document to convert | @return The XML string | | getFiles(rootPath: str, extension: str) -> list | @brief Get all files with the given extension from the given root path | @param rootPath The root path to search from | @param extension The extension to search for | @return A list of file paths | | jsonFileToXml(fileName: str, readOptions: materialxjson.core.JsonReadOptions = None) -> MaterialX.PyMaterialXCore.Document | @brief Convert a JSON file to an XML file | @param fileName The file name to read from | @param readOptions The read options to use. Default is None | @return The MaterialX document if successful, None otherwise | | jsonFileToXmlFile(fileName: str, outputFilename: str, readOptions: materialxjson.core.JsonReadOptions = None) -> bool | @brief Convert a JSON file to an XML file | @param fileName The file name to read from | @param outputFilename The file name to write to | @param readOptions The read options to use. Default is None | @return True if successful, false otherwise | | jsonStringToJson(jsonString: str) -> dict | @brief Convert a JSON string to a JSON document | @param jsonString The JSON string to convert | @return The JSON document | | jsonToJSONString(jsonObject: dict, indentation=2) -> str | @brief Convert a JSON document to a JSON string | @param jsonObject The JSON document to convert | @return The JSON string | | loadLibraries(searchPath: MaterialX.PyMaterialXFormat.FileSearchPath, libraryFolders: list) -> tuple | @brief Load all libraries from the given search path and library folders | @param searchPath The search path to use | @param libraryFolders The library folders to use | @return A tuple containing the library document and a status string | | readJson(fileName: str) -> dict | @brief Read a JSON file | @param fileName The file name to read | @return The JSON document | | writeJson(jsonObject: dict, fileName: str, indentation=2) -> None | @brief Write a JSON document to file | @param jsonObject The JSON document to write | @param fileName The file name to write to | | xmlFileToJsonFile(xmlFileName: str, jsonFileName: str, writeOptions: materialxjson.core.JsonWriteOptions = None) -> None | Convert an MaterialX XML file to a JSON file | @param xmlFileName The XML file to read from | @param jsonFileName The JSON file to write to | @param writeOptions The write options to use. Default is None | | xmlStringToDocument(doc: MaterialX.PyMaterialXCore.Document, xmlString: str) -> None | @brief Convert an XML string to a MaterialX document | @param doc The MaterialX document to write to | @param xmlString The XML string to convert | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined)
import pkg_resources
directory_name = "data"
files = pkg_resources.resource_listdir('materialxjson', directory_name)
result = ''
for file in files:
result = result + file + '\n'
displaySource('Available data files', result, 'text', True)
MaterialsVariantsShoe.gltf_converted.mtlx
MaterialsVariantsShoe.gltf_converted_mtlx.json
MaterialsVariantsShoe.gltf_converted_mtlx_json.mtlx
standard_surface_default.mtlx
standard_surface_default_mtlx.json
standard_surface_default_mtlx_json.mtlx
import pkg_resources
import MaterialX as mx
from materialxjson import core
from IPython.display import display_markdown
# Create I/O handler
mtlxjson = core.MaterialXJson()
# Read XML document
mtlxFileName = pkg_resources.resource_filename('materialxjson', 'data/standard_surface_default.mtlx')
print('Using sample file: %s' % mx.FilePath(mtlxFileName).getBaseName())
doc = mx.createDocument()
mx.readFromXmlFile(doc, mtlxFileName)
# Convert to JSON Object
jsonObject = mtlxjson.documentToJSON(doc)
# Convert to JSON String
jsondump = core.Util.jsonToJSONString(jsonObject, 2)
displaySource('Document to JSON', jsondump, 'json', True)
Using sample file: standard_surface_default.mtlx
{
"mimetype": "application/mtlx+json",
"materialx": {
"version": "1.39",
"colorspace": "lin_rec709",
"children": [
{
"name": "SR_default",
"category": "standard_surface",
"type": "surfaceshader",
"inputs": [
{
"name": "base",
"type": "float",
"value": "1.0"
},
{
"name": "base_color",
"type": "color3",
"value": "0.8, 0.8, 0.8"
},
{
"name": "diffuse_roughness",
"type": "float",
"value": "0"
},
{
"name": "specular",
"type": "float",
"value": "1"
},
{
"name": "specular_color",
"type": "color3",
"value": "1, 1, 1"
},
{
"name": "specular_roughness",
"type": "float",
"value": "0.2"
},
{
"name": "specular_IOR",
"type": "float",
"value": "1.5"
},
{
"name": "specular_anisotropy",
"type": "float",
"value": "0"
},
{
"name": "specular_rotation",
"type": "float",
"value": "0"
},
{
"name": "metalness",
"type": "float",
"value": "0"
},
{
"name": "transmission",
"type": "float",
"value": "0"
},
{
"name": "transmission_color",
"type": "color3",
"value": "1, 1, 1"
},
{
"name": "transmission_depth",
"type": "float",
"value": "0"
},
{
"name": "transmission_scatter",
"type": "color3",
"value": "0, 0, 0"
},
{
"name": "transmission_scatter_anisotropy",
"type": "float",
"value": "0"
},
{
"name": "transmission_dispersion",
"type": "float",
"value": "0"
},
{
"name": "transmission_extra_roughness",
"type": "float",
"value": "0"
},
{
"name": "subsurface",
"type": "float",
"value": "0"
},
{
"name": "subsurface_color",
"type": "color3",
"value": "1, 1, 1"
},
{
"name": "subsurface_radius",
"type": "color3",
"value": "1, 1, 1"
},
{
"name": "subsurface_scale",
"type": "float",
"value": "1"
},
{
"name": "subsurface_anisotropy",
"type": "float",
"value": "0"
},
{
"name": "sheen",
"type": "float",
"value": "0"
},
{
"name": "sheen_color",
"type": "color3",
"value": "1, 1, 1"
},
{
"name": "sheen_roughness",
"type": "float",
"value": "0.3"
},
{
"name": "thin_walled",
"type": "boolean",
"value": "false"
},
{
"name": "coat",
"type": "float",
"value": "0"
},
{
"name": "coat_color",
"type": "color3",
"value": "1, 1, 1"
},
{
"name": "coat_roughness",
"type": "float",
"value": "0.1"
},
{
"name": "coat_anisotropy",
"type": "float",
"value": "0.0"
},
{
"name": "coat_rotation",
"type": "float",
"value": "0.0"
},
{
"name": "coat_IOR",
"type": "float",
"value": "1.5"
},
{
"name": "coat_affect_color",
"type": "float",
"value": "0"
},
{
"name": "coat_affect_roughness",
"type": "float",
"value": "0"
},
{
"name": "thin_film_thickness",
"type": "float",
"value": "0"
},
{
"name": "thin_film_IOR",
"type": "float",
"value": "1.5"
},
{
"name": "emission",
"type": "float",
"value": "0"
},
{
"name": "emission_color",
"type": "color3",
"value": "1, 1, 1"
},
{
"name": "opacity",
"type": "color3",
"value": "1, 1, 1"
}
]
},
{
"name": "Default",
"category": "surfacematerial",
"type": "material",
"inputs": [
{
"name": "surfaceshader",
"type": "surfaceshader",
"nodename": "SR_default"
}
]
}
]
}
}
import pkg_resources
import MaterialX as mx
from materialxjson import core
from IPython.display import display_markdown
# Create I/O handler
mtlxjson = core.MaterialXJson()
# Read XML document
mtlxFileName = pkg_resources.resource_filename('materialxjson', 'data/standard_surface_default.mtlx')
print('Using sample file: %s' % mx.FilePath(mtlxFileName).getBaseName())
doc = mx.createDocument()
mx.readFromXmlFile(doc, mtlxFileName)
# Convert to JSON String
jsonString = mtlxjson.documentToJSONString(doc)
displaySource('Document to JSON String (direct)', jsonString, 'json', True)
Using sample file: standard_surface_default.mtlx
{
"mimetype": "application/mtlx+json",
"materialx": {
"version": "1.39",
"colorspace": "lin_rec709",
"children": [
{
"name": "SR_default",
"category": "standard_surface",
"type": "surfaceshader",
"inputs": [
{
"name": "base",
"type": "float",
"value": "1.0"
},
{
"name": "base_color",
"type": "color3",
"value": "0.8, 0.8, 0.8"
},
{
"name": "diffuse_roughness",
"type": "float",
"value": "0"
},
{
"name": "specular",
"type": "float",
"value": "1"
},
{
"name": "specular_color",
"type": "color3",
"value": "1, 1, 1"
},
{
"name": "specular_roughness",
"type": "float",
"value": "0.2"
},
{
"name": "specular_IOR",
"type": "float",
"value": "1.5"
},
{
"name": "specular_anisotropy",
"type": "float",
"value": "0"
},
{
"name": "specular_rotation",
"type": "float",
"value": "0"
},
{
"name": "metalness",
"type": "float",
"value": "0"
},
{
"name": "transmission",
"type": "float",
"value": "0"
},
{
"name": "transmission_color",
"type": "color3",
"value": "1, 1, 1"
},
{
"name": "transmission_depth",
"type": "float",
"value": "0"
},
{
"name": "transmission_scatter",
"type": "color3",
"value": "0, 0, 0"
},
{
"name": "transmission_scatter_anisotropy",
"type": "float",
"value": "0"
},
{
"name": "transmission_dispersion",
"type": "float",
"value": "0"
},
{
"name": "transmission_extra_roughness",
"type": "float",
"value": "0"
},
{
"name": "subsurface",
"type": "float",
"value": "0"
},
{
"name": "subsurface_color",
"type": "color3",
"value": "1, 1, 1"
},
{
"name": "subsurface_radius",
"type": "color3",
"value": "1, 1, 1"
},
{
"name": "subsurface_scale",
"type": "float",
"value": "1"
},
{
"name": "subsurface_anisotropy",
"type": "float",
"value": "0"
},
{
"name": "sheen",
"type": "float",
"value": "0"
},
{
"name": "sheen_color",
"type": "color3",
"value": "1, 1, 1"
},
{
"name": "sheen_roughness",
"type": "float",
"value": "0.3"
},
{
"name": "thin_walled",
"type": "boolean",
"value": "false"
},
{
"name": "coat",
"type": "float",
"value": "0"
},
{
"name": "coat_color",
"type": "color3",
"value": "1, 1, 1"
},
{
"name": "coat_roughness",
"type": "float",
"value": "0.1"
},
{
"name": "coat_anisotropy",
"type": "float",
"value": "0.0"
},
{
"name": "coat_rotation",
"type": "float",
"value": "0.0"
},
{
"name": "coat_IOR",
"type": "float",
"value": "1.5"
},
{
"name": "coat_affect_color",
"type": "float",
"value": "0"
},
{
"name": "coat_affect_roughness",
"type": "float",
"value": "0"
},
{
"name": "thin_film_thickness",
"type": "float",
"value": "0"
},
{
"name": "thin_film_IOR",
"type": "float",
"value": "1.5"
},
{
"name": "emission",
"type": "float",
"value": "0"
},
{
"name": "emission_color",
"type": "color3",
"value": "1, 1, 1"
},
{
"name": "opacity",
"type": "color3",
"value": "1, 1, 1"
}
]
},
{
"name": "Default",
"category": "surfacematerial",
"type": "material",
"inputs": [
{
"name": "surfaceshader",
"type": "surfaceshader",
"nodename": "SR_default"
}
]
}
]
}
}
There are currently a few options available for XML to JSON conversion. These include:
indent
- The number of spaces to indent the JSON output. Default is 2.elementPredicate
- A function that takes an element name and returns a boolean indicating whether the element should be included in the JSON output. Default is to include all elements.separators`` - A tuple of strings to use for separating items in a list and a dictionary respectively. Default is
(', ', ': ')`.In the example below we skip any surface material elements and adjust the formatting to remove unnecessary spacing.
The mtlx2json
utility script exposes options as command line arguments.
import pkg_resources
import MaterialX as mx
from materialxjson import core
from IPython.display import display_markdown
# Create I/O handler
mtlxjson = core.MaterialXJson()
# Read XML document
mtlxFileName = pkg_resources.resource_filename('materialxjson', 'data/standard_surface_default.mtlx')
print('Using sample file: %s' % mx.FilePath(mtlxFileName).getBaseName())
doc = mx.createDocument()
mx.readFromXmlFile(doc, mtlxFileName)
def skipMaterial(element):
print('- Skip material element: ' + element.getName())
return element.getCategory() != 'surfacematerial'
# Set write options
writeOptions = core.JsonWriteOptions()
writeOptions.indent = None # No indentation
writeOptions.separators = (',', ':') # No whitespace
writeOptions.elementPredicate = skipMaterial # Skip materials
# Convert to JSON String
jsonString = mtlxjson.documentToJSONString(doc, writeOptions)
displaySource('Document to JSON String (direct)', jsonString, 'json', True)
Using sample file: standard_surface_default.mtlx - Skip material element: SR_default - Skip material element: Default
{"mimetype":"application/mtlx+json","materialx":{"version":"1.39","colorspace":"lin_rec709","children":[{"name":"SR_default","category":"standard_surface","type":"surfaceshader","inputs":[{"name":"base","type":"float","value":"1.0"},{"name":"base_color","type":"color3","value":"0.8, 0.8, 0.8"},{"name":"diffuse_roughness","type":"float","value":"0"},{"name":"specular","type":"float","value":"1"},{"name":"specular_color","type":"color3","value":"1, 1, 1"},{"name":"specular_roughness","type":"float","value":"0.2"},{"name":"specular_IOR","type":"float","value":"1.5"},{"name":"specular_anisotropy","type":"float","value":"0"},{"name":"specular_rotation","type":"float","value":"0"},{"name":"metalness","type":"float","value":"0"},{"name":"transmission","type":"float","value":"0"},{"name":"transmission_color","type":"color3","value":"1, 1, 1"},{"name":"transmission_depth","type":"float","value":"0"},{"name":"transmission_scatter","type":"color3","value":"0, 0, 0"},{"name":"transmission_scatter_anisotropy","type":"float","value":"0"},{"name":"transmission_dispersion","type":"float","value":"0"},{"name":"transmission_extra_roughness","type":"float","value":"0"},{"name":"subsurface","type":"float","value":"0"},{"name":"subsurface_color","type":"color3","value":"1, 1, 1"},{"name":"subsurface_radius","type":"color3","value":"1, 1, 1"},{"name":"subsurface_scale","type":"float","value":"1"},{"name":"subsurface_anisotropy","type":"float","value":"0"},{"name":"sheen","type":"float","value":"0"},{"name":"sheen_color","type":"color3","value":"1, 1, 1"},{"name":"sheen_roughness","type":"float","value":"0.3"},{"name":"thin_walled","type":"boolean","value":"false"},{"name":"coat","type":"float","value":"0"},{"name":"coat_color","type":"color3","value":"1, 1, 1"},{"name":"coat_roughness","type":"float","value":"0.1"},{"name":"coat_anisotropy","type":"float","value":"0.0"},{"name":"coat_rotation","type":"float","value":"0.0"},{"name":"coat_IOR","type":"float","value":"1.5"},{"name":"coat_affect_color","type":"float","value":"0"},{"name":"coat_affect_roughness","type":"float","value":"0"},{"name":"thin_film_thickness","type":"float","value":"0"},{"name":"thin_film_IOR","type":"float","value":"1.5"},{"name":"emission","type":"float","value":"0"},{"name":"emission_color","type":"color3","value":"1, 1, 1"},{"name":"opacity","type":"color3","value":"1, 1, 1"}]}]}}
import pkg_resources
import MaterialX as mx
from materialxjson import core
from IPython.display import display_markdown
# Create I/O handler
mtlxjson = core.MaterialXJson()
# Load a MaterialX JSON file to object
jsonFileName = pkg_resources.resource_filename('materialxjson', 'data/standard_surface_default_mtlx.json')
print('Using sample file: %s' % mx.FilePath(jsonFileName).getBaseName())
jsonObject = core.Util.readJson(jsonFileName)
# Read JSON object into document
doc = mx.createDocument()
mtlxjson.documentFromJSON(jsonObject, doc)
# Write to JSON String
docstring = core.Util.documentToXMLString(doc)
displaySource('XML from JSON', docstring, 'xml', True)
Using sample file: standard_surface_default_mtlx.json
<?xml version="1.0"?>
<materialx version="1.39" colorspace="lin_rec709">
<standard_surface name="SR_default" type="surfaceshader">
<input name="base" type="float" value="1.0" />
<input name="base_color" type="color3" value="0.8, 0.8, 0.8" />
<input name="diffuse_roughness" type="float" value="0" />
<input name="specular" type="float" value="1" />
<input name="specular_color" type="color3" value="1, 1, 1" />
<input name="specular_roughness" type="float" value="0.2" />
<input name="specular_IOR" type="float" value="1.5" />
<input name="specular_anisotropy" type="float" value="0" />
<input name="specular_rotation" type="float" value="0" />
<input name="metalness" type="float" value="0" />
<input name="transmission" type="float" value="0" />
<input name="transmission_color" type="color3" value="1, 1, 1" />
<input name="transmission_depth" type="float" value="0" />
<input name="transmission_scatter" type="color3" value="0, 0, 0" />
<input name="transmission_scatter_anisotropy" type="float" value="0" />
<input name="transmission_dispersion" type="float" value="0" />
<input name="transmission_extra_roughness" type="float" value="0" />
<input name="subsurface" type="float" value="0" />
<input name="subsurface_color" type="color3" value="1, 1, 1" />
<input name="subsurface_radius" type="color3" value="1, 1, 1" />
<input name="subsurface_scale" type="float" value="1" />
<input name="subsurface_anisotropy" type="float" value="0" />
<input name="sheen" type="float" value="0" />
<input name="sheen_color" type="color3" value="1, 1, 1" />
<input name="sheen_roughness" type="float" value="0.3" />
<input name="thin_walled" type="boolean" value="false" />
<input name="coat" type="float" value="0" />
<input name="coat_color" type="color3" value="1, 1, 1" />
<input name="coat_roughness" type="float" value="0.1" />
<input name="coat_anisotropy" type="float" value="0.0" />
<input name="coat_rotation" type="float" value="0.0" />
<input name="coat_IOR" type="float" value="1.5" />
<input name="coat_affect_color" type="float" value="0" />
<input name="coat_affect_roughness" type="float" value="0" />
<input name="thin_film_thickness" type="float" value="0" />
<input name="thin_film_IOR" type="float" value="1.5" />
<input name="emission" type="float" value="0" />
<input name="emission_color" type="color3" value="1, 1, 1" />
<input name="opacity" type="color3" value="1, 1, 1" />
</standard_surface>
<surfacematerial name="Default" type="material">
<input name="surfaceshader" type="surfaceshader" nodename="SR_default" />
</surfacematerial>
</materialx>
import pkg_resources
import MaterialX as mx
from materialxjson import core
from IPython.display import display_markdown
# Create I/O handler
mtlxjson = core.MaterialXJson()
# Load a MaterialX JSON document
jsonFileName = pkg_resources.resource_filename('materialxjson', 'data/standard_surface_default_mtlx.json')
print('Using sample file: %s' % mx.FilePath(jsonFileName).getBaseName())
jsonObject = core.Util.readJson(jsonFileName)
jsonString = core.Util.jsonToJSONString(jsonObject, 2)
# Read into document
doc = mx.createDocument()
mtlxjson.documentFromJSONString(jsonString, doc)
# Write out to XML string
docstring = core.Util.documentToXMLString(doc)
displaySource('XML from JSON String', docstring, 'xml', True)
Using sample file: standard_surface_default_mtlx.json
<?xml version="1.0"?>
<materialx version="1.39" colorspace="lin_rec709">
<standard_surface name="SR_default" type="surfaceshader">
<input name="base" type="float" value="1.0" />
<input name="base_color" type="color3" value="0.8, 0.8, 0.8" />
<input name="diffuse_roughness" type="float" value="0" />
<input name="specular" type="float" value="1" />
<input name="specular_color" type="color3" value="1, 1, 1" />
<input name="specular_roughness" type="float" value="0.2" />
<input name="specular_IOR" type="float" value="1.5" />
<input name="specular_anisotropy" type="float" value="0" />
<input name="specular_rotation" type="float" value="0" />
<input name="metalness" type="float" value="0" />
<input name="transmission" type="float" value="0" />
<input name="transmission_color" type="color3" value="1, 1, 1" />
<input name="transmission_depth" type="float" value="0" />
<input name="transmission_scatter" type="color3" value="0, 0, 0" />
<input name="transmission_scatter_anisotropy" type="float" value="0" />
<input name="transmission_dispersion" type="float" value="0" />
<input name="transmission_extra_roughness" type="float" value="0" />
<input name="subsurface" type="float" value="0" />
<input name="subsurface_color" type="color3" value="1, 1, 1" />
<input name="subsurface_radius" type="color3" value="1, 1, 1" />
<input name="subsurface_scale" type="float" value="1" />
<input name="subsurface_anisotropy" type="float" value="0" />
<input name="sheen" type="float" value="0" />
<input name="sheen_color" type="color3" value="1, 1, 1" />
<input name="sheen_roughness" type="float" value="0.3" />
<input name="thin_walled" type="boolean" value="false" />
<input name="coat" type="float" value="0" />
<input name="coat_color" type="color3" value="1, 1, 1" />
<input name="coat_roughness" type="float" value="0.1" />
<input name="coat_anisotropy" type="float" value="0.0" />
<input name="coat_rotation" type="float" value="0.0" />
<input name="coat_IOR" type="float" value="1.5" />
<input name="coat_affect_color" type="float" value="0" />
<input name="coat_affect_roughness" type="float" value="0" />
<input name="thin_film_thickness" type="float" value="0" />
<input name="thin_film_IOR" type="float" value="1.5" />
<input name="emission" type="float" value="0" />
<input name="emission_color" type="color3" value="1, 1, 1" />
<input name="opacity" type="color3" value="1, 1, 1" />
</standard_surface>
<surfacematerial name="Default" type="material">
<input name="surfaceshader" type="surfaceshader" nodename="SR_default" />
</surfacematerial>
</materialx>
The options available for JSON to XML conversion include:
upgradeVersion
- A boolean indicating whether to upgrade the version of the MaterialX document to the latest version. Default is True
.import pkg_resources
import MaterialX as mx
from materialxjson import core
from IPython.display import display_markdown
# Create I/O handler
mtlxjson = core.MaterialXJson()
# Load a MaterialX JSON document
jsonFileName = pkg_resources.resource_filename('materialxjson', 'data/standard_surface_default_mtlx.json')
print('Using sample file: %s' % mx.FilePath(jsonFileName).getBaseName())
jsonObject = core.Util.readJson(jsonFileName)
jsonString = core.Util.jsonToJSONString(jsonObject, 2)
# Read into document
doc = mx.createDocument()
# Set read options
readOptions = core.JsonReadOptions()
readOptions.upgradeVersion = False # Do not upgrade version
mtlxjson.documentFromJSONString(jsonString, doc, readOptions)
Using sample file: standard_surface_default_mtlx.json
True
This example shows the gltf Sample Models "shoe" model read in XML format, and converted to JSON format. The sample mode was converted from gltf to MaterialX XML using the gltf2MaterialX converter.
import pkg_resources
import MaterialX as mx
from materialxjson import core
from IPython.display import display_markdown
# Create I/O handler
mtlxjson = core.MaterialXJson()
# Load the MaterialX file
mtlxFileName = pkg_resources.resource_filename('materialxjson', 'data/MaterialsVariantsShoe.gltf_converted.mtlx')
doc = mx.createDocument()
mx.readFromXmlFile(doc, mtlxFileName)
xmlString = core.Util.documentToXMLString(doc)
mtlxjson = core.MaterialXJson()
writeOptions = core.JsonWriteOptions()
writeOptions.indent = 1
writeOptions.separators = (',', ':')
jsonString = mtlxjson.documentToJSONString(doc, writeOptions)
xmlString = xmlString.replace('\n', '<br>')
xmlString = xmlString.replace(' ', ' ')
xmlString = xmlString.replace('\"', '\\"')
jsonString = jsonString.replace('\n', '<br>')
jsonString = jsonString.replace(' ', ' ')
output = '| JSON Format | XML Format |\n'
output = output + '| :--- | :--- |\n'
output = output + '| ' + jsonString + '| ' + xmlString + '|\n'
text = '<details open><summary><b>JSON / XML Comparison</b></summary>\n\n' + output + '\n</details>\n'
display_markdown(text, raw=True)
JSON Format | XML Format |
---|---|
{ "mimetype":"application/mtlx+json", "materialx":{ "version":"1.39", "children":[ { "name":"phong1SG", "category":"gltf_pbr", "type":"surfaceshader", "nodedef":"ND_gltf_pbr_surfaceshader", "inputs":[ { "name":"base_color", "type":"color3", "nodename":"image_base_color", "output":"outcolor" }, { "name":"alpha", "type":"float", "nodename":"image_base_color", "output":"outa" }, { "name":"metallic", "type":"float", "nodename":"extract_orm3" }, { "name":"roughness", "type":"float", "nodename":"extract_orm2" }, { "name":"occlusion", "type":"float", "nodename":"image_occlusion" }, { "name":"normal", "type":"vector3", "nodename":"image_normal" }, { "name":"emissive", "type":"color3", "value":"0, 0, 0", "colorspace":"srgb_texture" } ] }, { "name":"MAT_phong1SG", "category":"surfacematerial", "type":"material", "inputs":[ { "name":"surfaceshader", "type":"surfaceshader", "nodename":"phong1SG" } ] }, { "name":"image_base_color", "category":"gltf_colorimage", "type":"multioutput", "inputs":[ { "name":"file", "type":"filename", "value":"diffuseMidnight.jpg", "colorspace":"srgb_texture" } ] }, { "name":"image_orm", "category":"gltf_image", "type":"vector3", "inputs":[ { "name":"file", "type":"filename", "value":"occlusionRougnessMetalness.jpg" }, { "name":"filtertype", "type":"string", "value":"closest" }, { "name":"uaddressmode", "type":"string", "value":"periodic" }, { "name":"vaddressmode", "type":"string", "value":"periodic" } ] }, { "name":"extract_orm", "category":"extract", "type":"float", "inputs":[ { "name":"in", "type":"vector3", "nodename":"image_orm" }, { "name":"index", "type":"integer", "value":"0" } ] }, { "name":"extract_orm2", "category":"extract", "type":"float", "inputs":[ { "name":"in", "type":"vector3", "nodename":"image_orm" }, { "name":"index", "type":"integer", "value":"1" } ] }, { "name":"extract_orm3", "category":"extract", "type":"float", "inputs":[ { "name":"in", "type":"vector3", "nodename":"image_orm" }, { "name":"index", "type":"integer", "value":"2" } ] }, { "name":"image_normal", "category":"gltf_normalmap", "type":"vector3", "inputs":[ { "name":"file", "type":"filename", "value":"normal.jpg" }, { "name":"filtertype", "type":"string", "value":"closest" }, { "name":"uaddressmode", "type":"string", "value":"periodic" }, { "name":"vaddressmode", "type":"string", "value":"periodic" } ] }, { "name":"image_occlusion", "category":"gltf_image", "type":"float", "inputs":[ { "name":"file", "type":"filename", "value":"occlusionRougnessMetalness.jpg" }, { "name":"filtertype", "type":"string", "value":"closest" }, { "name":"uaddressmode", "type":"string", "value":"periodic" }, { "name":"vaddressmode", "type":"string", "value":"periodic" } ] }, { "name":"phong1SG2", "category":"gltf_pbr", "type":"surfaceshader", "nodedef":"ND_gltf_pbr_surfaceshader", "inputs":[ { "name":"base_color", "type":"color3", "nodename":"image_base_color2", "output":"outcolor" }, { "name":"alpha", "type":"float", "nodename":"image_base_color2", "output":"outa" }, { "name":"metallic", "type":"float", "nodename":"extract_orm6" }, { "name":"roughness", "type":"float", "nodename":"extract_orm5" }, { "name":"occlusion", "type":"float", "nodename":"image_occlusion2" }, { "name":"normal", "type":"vector3", "nodename":"image_normal2" }, { "name":"emissive", "type":"color3", "value":"0, 0, 0", "colorspace":"srgb_texture" } ] }, { "name":"MAT_phong1SG2", "category":"surfacematerial", "type":"material", "inputs":[ { "name":"surfaceshader", "type":"surfaceshader", "nodename":"phong1SG2" } ] }, { "name":"image_base_color2", "category":"gltf_colorimage", "type":"multioutput", "inputs":[ { "name":"file", "type":"filename", "value":"diffuseBeach.jpg", "colorspace":"srgb_texture" } ] }, { "name":"image_orm2", "category":"gltf_image", "type":"vector3", "inputs":[ { "name":"file", "type":"filename", "value":"occlusionRougnessMetalness.jpg" }, { "name":"filtertype", "type":"string", "value":"closest" }, { "name":"uaddressmode", "type":"string", "value":"periodic" }, { "name":"vaddressmode", "type":"string", "value":"periodic" } ] }, { "name":"extract_orm4", "category":"extract", "type":"float", "inputs":[ { "name":"in", "type":"vector3", "nodename":"image_orm2" }, { "name":"index", "type":"integer", "value":"0" } ] }, { "name":"extract_orm5", "category":"extract", "type":"float", "inputs":[ { "name":"in", "type":"vector3", "nodename":"image_orm2" }, { "name":"index", "type":"integer", "value":"1" } ] }, { "name":"extract_orm6", "category":"extract", "type":"float", "inputs":[ { "name":"in", "type":"vector3", "nodename":"image_orm2" }, { "name":"index", "type":"integer", "value":"2" } ] }, { "name":"image_normal2", "category":"gltf_normalmap", "type":"vector3", "inputs":[ { "name":"file", "type":"filename", "value":"normal.jpg" }, { "name":"filtertype", "type":"string", "value":"closest" }, { "name":"uaddressmode", "type":"string", "value":"periodic" }, { "name":"vaddressmode", "type":"string", "value":"periodic" } ] }, { "name":"image_occlusion2", "category":"gltf_image", "type":"float", "inputs":[ { "name":"file", "type":"filename", "value":"occlusionRougnessMetalness.jpg" }, { "name":"filtertype", "type":"string", "value":"closest" }, { "name":"uaddressmode", "type":"string", "value":"periodic" }, { "name":"vaddressmode", "type":"string", "value":"periodic" } ] }, { "name":"phong1SG3", "category":"gltf_pbr", "type":"surfaceshader", "nodedef":"ND_gltf_pbr_surfaceshader", "inputs":[ { "name":"base_color", "type":"color3", "nodename":"image_base_color3", "output":"outcolor" }, { "name":"alpha", "type":"float", "nodename":"image_base_color3", "output":"outa" }, { "name":"metallic", "type":"float", "nodename":"extract_orm9" }, { "name":"roughness", "type":"float", "nodename":"extract_orm8" }, { "name":"occlusion", "type":"float", "nodename":"image_occlusion3" }, { "name":"normal", "type":"vector3", "nodename":"image_normal3" }, { "name":"emissive", "type":"color3", "value":"0, 0, 0", "colorspace":"srgb_texture" } ] }, { "name":"MAT_phong1SG3", "category":"surfacematerial", "type":"material", "inputs":[ { "name":"surfaceshader", "type":"surfaceshader", "nodename":"phong1SG3" } ] }, { "name":"image_base_color3", "category":"gltf_colorimage", "type":"multioutput", "inputs":[ { "name":"file", "type":"filename", "value":"diffuseStreet.jpg", "colorspace":"srgb_texture" } ] }, { "name":"image_orm3", "category":"gltf_image", "type":"vector3", "inputs":[ { "name":"file", "type":"filename", "value":"occlusionRougnessMetalness.jpg" }, { "name":"filtertype", "type":"string", "value":"closest" }, { "name":"uaddressmode", "type":"string", "value":"periodic" }, { "name":"vaddressmode", "type":"string", "value":"periodic" } ] }, { "name":"extract_orm7", "category":"extract", "type":"float", "inputs":[ { "name":"in", "type":"vector3", "nodename":"image_orm3" }, { "name":"index", "type":"integer", "value":"0" } ] }, { "name":"extract_orm8", "category":"extract", "type":"float", "inputs":[ { "name":"in", "type":"vector3", "nodename":"image_orm3" }, { "name":"index", "type":"integer", "value":"1" } ] }, { "name":"extract_orm9", "category":"extract", "type":"float", "inputs":[ { "name":"in", "type":"vector3", "nodename":"image_orm3" }, { "name":"index", "type":"integer", "value":"2" } ] }, { "name":"image_normal3", "category":"gltf_normalmap", "type":"vector3", "inputs":[ { "name":"file", "type":"filename", "value":"normal.jpg" }, { "name":"filtertype", "type":"string", "value":"closest" }, { "name":"uaddressmode", "type":"string", "value":"periodic" }, { "name":"vaddressmode", "type":"string", "value":"periodic" } ] }, { "name":"image_occlusion3", "category":"gltf_image", "type":"float", "inputs":[ { "name":"file", "type":"filename", "value":"occlusionRougnessMetalness.jpg" }, { "name":"filtertype", "type":"string", "value":"closest" }, { "name":"uaddressmode", "type":"string", "value":"periodic" }, { "name":"vaddressmode", "type":"string", "value":"periodic" } ] }, { "name":"look", "category":"look", "children":[ { "name":"MAT_phong1SG", "category":"materialassign", "material":"MAT_phong1SG", "geom":"/Shoeobj/g_Shoe/Shoe/shoe" } ] } ] } } |
<materialx version="1.39"> <gltf_pbr name="phong1SG" type="surfaceshader" nodedef="ND_gltf_pbr_surfaceshader"> <input name="base_color" type="color3" nodename="image_base_color" output="outcolor" /> <input name="alpha" type="float" nodename="image_base_color" output="outa" /> <input name="metallic" type="float" nodename="extract_orm3" /> <input name="roughness" type="float" nodename="extract_orm2" /> <input name="occlusion" type="float" nodename="image_occlusion" /> <input name="normal" type="vector3" nodename="image_normal" /> <input name="emissive" type="color3" value="0, 0, 0" colorspace="srgb_texture" /> </gltf_pbr> <surfacematerial name="MAT_phong1SG" type="material"> <input name="surfaceshader" type="surfaceshader" nodename="phong1SG" /> <gltf_colorimage name="image_base_color" type="multioutput"> <input name="file" type="filename" value="diffuseMidnight.jpg" colorspace="srgb_texture" /> </gltf_colorimage> <gltf_image name="image_orm" type="vector3"> <input name="file" type="filename" value="occlusionRougnessMetalness.jpg" /> <input name="filtertype" type="string" value="closest" /> <input name="uaddressmode" type="string" value="periodic" /> <input name="vaddressmode" type="string" value="periodic" /> </gltf_image> <extract name="extract_orm" type="float"> <input name="in" type="vector3" nodename="image_orm" /> <input name="index" type="integer" value="0" /> <extract name="extract_orm2" type="float"> <input name="in" type="vector3" nodename="image_orm" /> <input name="index" type="integer" value="1" /> <extract name="extract_orm3" type="float"> <input name="in" type="vector3" nodename="image_orm" /> <input name="index" type="integer" value="2" /> <gltf_normalmap name="image_normal" type="vector3"> <input name="file" type="filename" value="normal.jpg" /> <input name="filtertype" type="string" value="closest" /> <input name="uaddressmode" type="string" value="periodic" /> <input name="vaddressmode" type="string" value="periodic" /> </gltf_normalmap> <gltf_image name="image_occlusion" type="float"> <input name="file" type="filename" value="occlusionRougnessMetalness.jpg" /> <input name="filtertype" type="string" value="closest" /> <input name="uaddressmode" type="string" value="periodic" /> <input name="vaddressmode" type="string" value="periodic" /> </gltf_image> <gltf_pbr name="phong1SG2" type="surfaceshader" nodedef="ND_gltf_pbr_surfaceshader"> <input name="base_color" type="color3" nodename="image_base_color2" output="outcolor" /> <input name="alpha" type="float" nodename="image_base_color2" output="outa" /> <input name="metallic" type="float" nodename="extract_orm6" /> <input name="roughness" type="float" nodename="extract_orm5" /> <input name="occlusion" type="float" nodename="image_occlusion2" /> <input name="normal" type="vector3" nodename="image_normal2" /> <input name="emissive" type="color3" value="0, 0, 0" colorspace="srgb_texture" /> </gltf_pbr> <surfacematerial name="MAT_phong1SG2" type="material"> <input name="surfaceshader" type="surfaceshader" nodename="phong1SG2" /> <gltf_colorimage name="image_base_color2" type="multioutput"> <input name="file" type="filename" value="diffuseBeach.jpg" colorspace="srgb_texture" /> </gltf_colorimage> <gltf_image name="image_orm2" type="vector3"> <input name="file" type="filename" value="occlusionRougnessMetalness.jpg" /> <input name="filtertype" type="string" value="closest" /> <input name="uaddressmode" type="string" value="periodic" /> <input name="vaddressmode" type="string" value="periodic" /> </gltf_image> <extract name="extract_orm4" type="float"> <input name="in" type="vector3" nodename="image_orm2" /> <input name="index" type="integer" value="0" /> <extract name="extract_orm5" type="float"> <input name="in" type="vector3" nodename="image_orm2" /> <input name="index" type="integer" value="1" /> <extract name="extract_orm6" type="float"> <input name="in" type="vector3" nodename="image_orm2" /> <input name="index" type="integer" value="2" /> <gltf_normalmap name="image_normal2" type="vector3"> <input name="file" type="filename" value="normal.jpg" /> <input name="filtertype" type="string" value="closest" /> <input name="uaddressmode" type="string" value="periodic" /> <input name="vaddressmode" type="string" value="periodic" /> </gltf_normalmap> <gltf_image name="image_occlusion2" type="float"> <input name="file" type="filename" value="occlusionRougnessMetalness.jpg" /> <input name="filtertype" type="string" value="closest" /> <input name="uaddressmode" type="string" value="periodic" /> <input name="vaddressmode" type="string" value="periodic" /> </gltf_image> <gltf_pbr name="phong1SG3" type="surfaceshader" nodedef="ND_gltf_pbr_surfaceshader"> <input name="base_color" type="color3" nodename="image_base_color3" output="outcolor" /> <input name="alpha" type="float" nodename="image_base_color3" output="outa" /> <input name="metallic" type="float" nodename="extract_orm9" /> <input name="roughness" type="float" nodename="extract_orm8" /> <input name="occlusion" type="float" nodename="image_occlusion3" /> <input name="normal" type="vector3" nodename="image_normal3" /> <input name="emissive" type="color3" value="0, 0, 0" colorspace="srgb_texture" /> </gltf_pbr> <surfacematerial name="MAT_phong1SG3" type="material"> <input name="surfaceshader" type="surfaceshader" nodename="phong1SG3" /> <gltf_colorimage name="image_base_color3" type="multioutput"> <input name="file" type="filename" value="diffuseStreet.jpg" colorspace="srgb_texture" /> </gltf_colorimage> <gltf_image name="image_orm3" type="vector3"> <input name="file" type="filename" value="occlusionRougnessMetalness.jpg" /> <input name="filtertype" type="string" value="closest" /> <input name="uaddressmode" type="string" value="periodic" /> <input name="vaddressmode" type="string" value="periodic" /> </gltf_image> <extract name="extract_orm7" type="float"> <input name="in" type="vector3" nodename="image_orm3" /> <input name="index" type="integer" value="0" /> <extract name="extract_orm8" type="float"> <input name="in" type="vector3" nodename="image_orm3" /> <input name="index" type="integer" value="1" /> <extract name="extract_orm9" type="float"> <input name="in" type="vector3" nodename="image_orm3" /> <input name="index" type="integer" value="2" /> <gltf_normalmap name="image_normal3" type="vector3"> <input name="file" type="filename" value="normal.jpg" /> <input name="filtertype" type="string" value="closest" /> <input name="uaddressmode" type="string" value="periodic" /> <input name="vaddressmode" type="string" value="periodic" /> </gltf_normalmap> <gltf_image name="image_occlusion3" type="float"> <input name="file" type="filename" value="occlusionRougnessMetalness.jpg" /> <input name="filtertype" type="string" value="closest" /> <input name="uaddressmode" type="string" value="periodic" /> <input name="vaddressmode" type="string" value="periodic" /> </gltf_image> <look name="look"> <materialassign name="MAT_phong1SG" material="MAT_phong1SG" geom="/Shoeobj/g_Shoe/Shoe/shoe" /> |
A few additional examples are provided in the documentation data
folder.