MaterialXJSON 1.39.0
Loading...
Searching...
No Matches
materialxjson.core.MaterialXJson Class Reference

Class for handling read and write of MaterialX from and to JSON. More...

Public Member Functions

dict elementToJSON (self, mx.Element elem, dict jsonParent, JsonWriteOptions writeOptions=None)
 Convert an MaterialX XML element to JSON.
 
dict documentToJSON (self, mx.Document doc, JsonWriteOptions writeOptions=None)
 Convert an MaterialX XML document to JSON.
 
str documentToJSONString (self, mx.Document doc, JsonWriteOptions writeOptions=None)
 Convert an MaterialX XML document to JSON string.
 
None elementFromJSON (self, dict node, mx.Element elem, JsonReadOptions readOptions=None)
 Convert an JSON element to MaterialX.
 
bool documentFromJSON (self, dict jsonDoc, mx.Document doc, JsonReadOptions readOptions=None)
 Convert a JSON document to MaterialX.
 
bool documentFromJSONString (self, str jsonString, mx.Document doc, JsonReadOptions readOptions=None)
 Convert a JSON document to MaterialX.
 

Detailed Description

Class for handling read and write of MaterialX from and to JSON.

Definition at line 63 of file core.py.

Member Function Documentation

◆ documentFromJSON()

bool materialxjson.core.MaterialXJson.documentFromJSON ( self,
dict jsonDoc,
mx.Document doc,
JsonReadOptions readOptions = None )

Convert a JSON document to MaterialX.

Parameters
jsonDocThe JSON document to read
docThe MaterialX document to write to
readOptionsThe read options to use. Default is None

Definition at line 205 of file core.py.

205 def documentFromJSON(self, jsonDoc: dict, doc: mx.Document, readOptions: JsonReadOptions = None) -> bool:
206 '''
207 @brief Convert a JSON document to MaterialX
208 @param jsonDoc The JSON document to read
209 @param doc The MaterialX document to write to
210 @param readOptions The read options to use. Default is None
211 '''
212 readDoc = False
213 # Check mimetype and existence of MaterialX root element
214 if JSON_MIMETYPE_KEY in jsonDoc and jsonDoc[JSON_MIMETYPE_KEY] == JSON_MIMETYPE:
215 if MATERIALX_DOCUMENT_ROOT in jsonDoc:
216 self.elementFromJSON(jsonDoc['materialx'], doc, readOptions)
217 readDoc = True
218 else:
219 print('JSON document is missing a MaterialX root element')
220 else:
221 print('JSON document is not a MaterialX document')
222
223 if readDoc:
224 # Upgrade to latest version if requested
225 if readOptions and readOptions.upgradeVersion:
226 doc.upgradeVersion()
227
228 return readDoc
229

◆ documentFromJSONString()

bool materialxjson.core.MaterialXJson.documentFromJSONString ( self,
str jsonString,
mx.Document doc,
JsonReadOptions readOptions = None )

Convert a JSON document to MaterialX.

Parameters
jsonStringThe JSON string to read
docThe MaterialX document to write to
readOptionsThe read options to use. Default is None
Returns
True if successful, false otherwise

Definition at line 230 of file core.py.

230 def documentFromJSONString(self, jsonString: str, doc: mx.Document, readOptions: JsonReadOptions = None) -> bool:
231 '''
232 @brief Convert a JSON document to MaterialX
233 @param jsonString The JSON string to read
234 @param doc The MaterialX document to write to
235 @param readOptions The read options to use. Default is None
236 @return True if successful, false otherwise
237 '''
238 jsonDoc = json.loads(jsonString)
239 readDoc = False
240 if jsonDoc:
241 readDoc = self.documentFromJSON(jsonDoc, doc, readOptions)
242 return readDoc
243

◆ documentToJSON()

dict materialxjson.core.MaterialXJson.documentToJSON ( self,
mx.Document doc,
JsonWriteOptions writeOptions = None )

Convert an MaterialX XML document to JSON.

Parameters
docThe MaterialX document to convert
writeOptionsThe write options to use. Default is None
Returns
The JSON document

Definition at line 120 of file core.py.

120 def documentToJSON(self, doc: mx.Document, writeOptions: JsonWriteOptions = None) -> dict:
121 '''
122 Convert an MaterialX XML document to JSON
123 @param doc The MaterialX document to convert
124 @param writeOptions The write options to use. Default is None
125 @return The JSON document
126 '''
127 root = {}
128 # Set the mimetype
129 root[JSON_MIMETYPE_KEY] = JSON_MIMETYPE
130
131 # Create the document
132 documentRoot = {}
133
134 # Add document level attributes
135 for attrName in doc.getAttributeNames():
136 documentRoot[attrName] = doc.getAttribute(attrName)
137
138 # Add children
139 children = []
140 for elem in doc.getChildren():
141 self.elementToJSON(elem, children, writeOptions)
142 documentRoot['children'] = children
143
144 # Set 'materialx' root element
145 root[MATERIALX_DOCUMENT_ROOT] = documentRoot
146
147 return root
148

◆ documentToJSONString()

str materialxjson.core.MaterialXJson.documentToJSONString ( self,
mx.Document doc,
JsonWriteOptions writeOptions = None )

Convert an MaterialX XML document to JSON string.

Parameters
docThe MaterialX document to convert
writeOptionsThe write options to use. Default is None
Returns
The JSON string

Definition at line 149 of file core.py.

149 def documentToJSONString(self, doc: mx.Document, writeOptions: JsonWriteOptions = None) -> str:
150 '''
151 Convert an MaterialX XML document to JSON string
152 @param doc The MaterialX document to convert
153 @param writeOptions The write options to use. Default is None
154 @return The JSON string
155 '''
156 result = self.documentToJSON(doc, writeOptions)
157 json_string = ''
158 if result:
159 indentation = 2
160 sep = (',', ': ')
161 if writeOptions:
162 indentation = writeOptions.indent
163 sep = writeOptions.separators
164 json_string = json.dumps(result, indent=indentation, separators=sep)
165
166 return json_string
167

◆ elementFromJSON()

None materialxjson.core.MaterialXJson.elementFromJSON ( self,
dict node,
mx.Element elem,
JsonReadOptions readOptions = None )

Convert an JSON element to MaterialX.

Parameters
nodeThe JSON element to read
elemThe MaterialX element to write to
readOptionsThe read options to use. Default is None

Definition at line 168 of file core.py.

168 def elementFromJSON(self, node: dict, elem: mx.Element, readOptions: JsonReadOptions = None) -> None:
169 '''
170 @brief Convert an JSON element to MaterialX
171 @param node The JSON element to read
172 @param elem The MaterialX element to write to
173 @param readOptions The read options to use. Default is None
174 '''
175 for key in node:
176 value = node[key]
177
178 # Set attributes
179 if isinstance(value, str):
180 if key not in ['name', 'category']:
181 elem.setAttribute(key, str(value))
182
183 # Traverse children
184 elif key == INPUTS_STRING:
185 for child in value:
186 category = 'input'
187 name = child['name']
188 childElem = elem.addChildOfCategory(category, name)
189 self.elementFromJSON(child, childElem)
190
191 elif key == OUTPUTS_STRING:
192 for child in value:
193 category = 'output'
194 name = child['name']
195 childElem = elem.addChildOfCategory(category, name)
196 self.elementFromJSON(child, childElem)
197
198 elif key == CHILDREN_STRING:
199 for child in value:
200 category = child['category']
201 name = child['name']
202 childElem = elem.addChildOfCategory(category, name)
203 self.elementFromJSON(child, childElem)
204

◆ elementToJSON()

dict materialxjson.core.MaterialXJson.elementToJSON ( self,
mx.Element elem,
dict jsonParent,
JsonWriteOptions writeOptions = None )

Convert an MaterialX XML element to JSON.

Will recursively traverse the parent/child Element hierarchy.

Parameters
elemThe MaterialX element to convert
jsonParentThe JSON element append to
writeOptionsThe write options to use. Default is None

Definition at line 67 of file core.py.

67 def elementToJSON(self, elem: mx.Element, jsonParent: dict, writeOptions: JsonWriteOptions = None) -> dict:
68 '''
69 @brief Convert an MaterialX XML element to JSON.
70 Will recursively traverse the parent/child Element hierarchy.
71 @param elem The MaterialX element to convert
72 @param jsonParent The JSON element append to
73 @param writeOptions The write options to use. Default is None
74 '''
75 if (writeOptions and writeOptions.elementPredicate and not writeOptions.elementPredicate(elem)):
76 return
77
78 if (elem.getSourceUri() != ""):
79 return
80
81 # Create a new JSON element for the MaterialX element
82 jsonElem = {}
83 jsonElem['name'] = elem.getName()
84 category = elem.getCategory()
85 # It is redundant but not incorrect to add in the category
86 # For now always add in the category
87 if (writeOptions and writeOptions.addInputOutputCategories) or (category not in ['input', 'output']):
88 jsonElem['category'] = category
89
90 # Add attributes
91 for attrName in elem.getAttributeNames():
92 jsonElem[attrName] = elem.getAttribute(attrName)
93
94 # Add children. Split based on category: input, output or other
95 inputs = []
96 outputs = []
97 non_input_outputs = []
98 for child in elem.getChildren():
99 category = child.getCategory()
100 if category == 'input':
101 self.elementToJSON(child, inputs)
102 elif category == 'output':
103 self.elementToJSON(child, outputs)
104 else:
105 self.elementToJSON(child, non_input_outputs)
106
107 # Add inputs, outputs and other children
108 if len(inputs) > 0:
109 jsonElem[INPUTS_STRING] = inputs
110 if len(outputs) > 0:
111 jsonElem[OUTPUTS_STRING] = outputs
112 if len(non_input_outputs) > 0:
113 jsonElem[CHILDREN_STRING] = non_input_outputs
114
115 # Add the JSON element to the parent
116 jsonParent.append(jsonElem)
117
118 return jsonParent
119

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