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

Utility class for MaterialX JSON. More...

Static Public Member Functions

dict readJson (str fileName)
 Read a JSON file.
 
dict jsonStringToJson (str jsonString)
 Convert a JSON string to a JSON document.
 
str jsonToJSONString (dict jsonObject, indentation=2)
 Convert a JSON document to a JSON string.
 
str documentToXMLString (mx.Document doc)
 Convert a MaterialX document to XML string.
 
None xmlStringToDocument (mx.Document doc, str xmlString)
 Convert an XML string to a MaterialX document.
 
None writeJson (dict jsonObject, str fileName, indentation=2)
 Write a JSON document to file.
 
list getFiles (str rootPath, str extension)
 Get all files with the given extension from the given root path.
 
tuple loadLibraries (mx.FileSearchPath searchPath, list libraryFolders)
 Load all libraries from the given search path and library folders.
 
mx.Document jsonFileToXml (str fileName, JsonReadOptions readOptions=None)
 Convert a JSON file to an XML file.
 
bool jsonFileToXmlFile (str fileName, str outputFilename, JsonReadOptions readOptions=None)
 Convert a JSON file to an XML file.
 
None xmlFileToJsonFile (str xmlFileName, str jsonFileName, JsonWriteOptions writeOptions=None)
 Convert an MaterialX XML file to a JSON file.
 

Detailed Description

Utility class for MaterialX JSON.

Definition at line 244 of file core.py.

Member Function Documentation

◆ documentToXMLString()

str materialxjson.core.Util.documentToXMLString ( mx.Document doc)
static

Convert a MaterialX document to XML string.

Parameters
docThe MaterialX document to convert
Returns
The XML string

Definition at line 284 of file core.py.

284 def documentToXMLString(doc: mx.Document) -> str:
285 '''
286 @brief Convert a MaterialX document to XML string
287 @param doc The MaterialX document to convert
288 @return The XML string
289 '''
290 return mx.writeToXmlString(doc)
291

◆ getFiles()

list materialxjson.core.Util.getFiles ( str rootPath,
str extension )
static

Get all files with the given extension from the given root path.

Parameters
rootPathThe root path to search from
extensionThe extension to search for
Returns
A list of file paths

Definition at line 312 of file core.py.

312 def getFiles(rootPath: str, extension: str) -> list:
313 '''
314 @brief Get all files with the given extension from the given root path
315 @param rootPath The root path to search from
316 @param extension The extension to search for
317 @return A list of file paths
318 '''
319 filelist = []
320 exts = (extension, extension.upper() )
321 for subdir, dirs, files in os.walk(rootPath):
322 for file in files:
323 if file.lower().endswith(exts):
324 filelist.append(os.path.join(subdir, file))
325 return filelist
326

◆ jsonFileToXml()

mx.Document materialxjson.core.Util.jsonFileToXml ( str fileName,
JsonReadOptions readOptions = None )
static

Convert a JSON file to an XML file.

Parameters
fileNameThe file name to read from
readOptionsThe read options to use. Default is None
Returns
The MaterialX document if successful, None otherwise

Definition at line 346 of file core.py.

346 def jsonFileToXml(fileName: str, readOptions: JsonReadOptions = None) -> mx.Document:
347 '''
348 @brief Convert a JSON file to an XML file
349 @param fileName The file name to read from
350 @param readOptions The read options to use. Default is None
351 @return The MaterialX document if successful, None otherwise
352 '''
353 mtlxjson = MaterialXJson()
354
355 jsonFile = open(fileName, 'r')
356 if not jsonFile:
357 return None
358 jsonObject = json.load(jsonFile)
359 if not jsonObject:
360 return None
361
362 newDoc = mx.createDocument()
363 readDoc = mtlxjson.documentFromJSON(jsonObject, newDoc, readOptions)
364 if readDoc:
365 return newDoc
366
367 return None
368

◆ jsonFileToXmlFile()

bool materialxjson.core.Util.jsonFileToXmlFile ( str fileName,
str outputFilename,
JsonReadOptions readOptions = None )
static

Convert a JSON file to an XML file.

Parameters
fileNameThe file name to read from
outputFilenameThe file name to write to
readOptionsThe read options to use. Default is None
Returns
True if successful, false otherwise

Definition at line 370 of file core.py.

370 def jsonFileToXmlFile(fileName: str, outputFilename: str, readOptions: JsonReadOptions = None) -> bool:
371 '''
372 @brief Convert a JSON file to an XML file
373 @param fileName The file name to read from
374 @param outputFilename The file name to write to
375 @param readOptions The read options to use. Default is None
376 @return True if successful, false otherwise
377 '''
378 jsonFile = open(fileName, 'r')
379 if not jsonFile:
380 return None
381 jsonObject = json.load(jsonFile)
382 if not jsonObject:
383 return None
384
385 #newDoc = mx.createDocument()
386 mtlxjson = MaterialXJson()
387 newDoc = mx.createDocument()
388 created = mtlxjson.documentFromJSON(jsonObject, newDoc, readOptions)
389
390 if newDoc.getChildren():
391 mx.writeToXmlFile(newDoc, outputFilename)
392 return True
393
394 return False
395

◆ jsonStringToJson()

dict materialxjson.core.Util.jsonStringToJson ( str jsonString)
static

Convert a JSON string to a JSON document.

Parameters
jsonStringThe JSON string to convert
Returns
The JSON document

Definition at line 266 of file core.py.

266 def jsonStringToJson(jsonString: str) -> dict:
267 '''
268 @brief Convert a JSON string to a JSON document
269 @param jsonString The JSON string to convert
270 @return The JSON document
271 '''
272 return json.loads(jsonString)
273

◆ jsonToJSONString()

str materialxjson.core.Util.jsonToJSONString ( dict jsonObject,
indentation = 2 )
static

Convert a JSON document to a JSON string.

Parameters
jsonObjectThe JSON document to convert
Returns
The JSON string

Definition at line 275 of file core.py.

275 def jsonToJSONString(jsonObject: dict, indentation = 2) -> str:
276 '''
277 @brief Convert a JSON document to a JSON string
278 @param jsonObject The JSON document to convert
279 @return The JSON string
280 '''
281 return json.dumps(jsonObject, indent=indentation)
282

◆ loadLibraries()

tuple materialxjson.core.Util.loadLibraries ( mx.FileSearchPath searchPath,
list libraryFolders )
static

Load all libraries from the given search path and library folders.

Parameters
searchPathThe search path to use
libraryFoldersThe library folders to use
Returns
A tuple containing the library document and a status string

Definition at line 328 of file core.py.

328 def loadLibraries(searchPath: mx.FileSearchPath, libraryFolders: list) -> tuple:
329 '''
330 @brief Load all libraries from the given search path and library folders
331 @param searchPath The search path to use
332 @param libraryFolders The library folders to use
333 @return A tuple containing the library document and a status string
334 '''
335 status = ''
336 lib = mx.createDocument()
337 try:
338 libFiles = mx.loadLibraries(libraryFolders, searchPath, lib)
339 status = '- Loaded %d library definitions from %d files' % (len(lib.getNodeDefs()), len(libFiles))
340 except mx.Exception as err:
341 status = '- Failed to load library definitions: "%s"' % err
342
343 return lib, status
344

◆ readJson()

dict materialxjson.core.Util.readJson ( str fileName)
static

Read a JSON file.

Parameters
fileNameThe file name to read
Returns
The JSON document

Definition at line 249 of file core.py.

249 def readJson(fileName: str) -> dict:
250 '''
251 @brief Read a JSON file
252 @param fileName The file name to read
253 @return The JSON document
254 '''
255 jsonFile = open(fileName, 'r')
256 if not jsonFile:
257 return False
258
259 jsonObject = json.load(jsonFile)
260 if not jsonObject:
261 return False
262
263 return jsonObject
264

◆ writeJson()

None materialxjson.core.Util.writeJson ( dict jsonObject,
str fileName,
indentation = 2 )
static

Write a JSON document to file.

Parameters
jsonObjectThe JSON document to write
fileNameThe file name to write to

Definition at line 302 of file core.py.

302 def writeJson(jsonObject: dict, fileName: str, indentation = 2) -> None:
303 '''
304 @brief Write a JSON document to file
305 @param jsonObject The JSON document to write
306 @param fileName The file name to write to
307 '''
308 with open(fileName, 'w') as outfile:
309 json.dump(jsonObject, outfile, indent=indentation)
310

◆ xmlFileToJsonFile()

None materialxjson.core.Util.xmlFileToJsonFile ( str xmlFileName,
str jsonFileName,
JsonWriteOptions writeOptions = None )
static

Convert an MaterialX XML file to a JSON file.

Parameters
xmlFileNameThe XML file to read from
jsonFileNameThe JSON file to write to
writeOptionsThe write options to use. Default is None

Definition at line 397 of file core.py.

397 def xmlFileToJsonFile(xmlFileName: str, jsonFileName: str, writeOptions: JsonWriteOptions = None) -> None:
398 '''
399 Convert an MaterialX XML file to a JSON file
400 @param xmlFileName The XML file to read from
401 @param jsonFileName The JSON file to write to
402 @param writeOptions The write options to use. Default is None
403 '''
404 mtlxjson = MaterialXJson()
405
406 doc = mx.createDocument()
407 mx.readFromXmlFile(doc, xmlFileName)
408 if doc:
409 # Convert entire document to JSON
410 doc_result = mtlxjson.documentToJSON(doc, writeOptions)
411
412 # Write JSON to file
413 with open(jsonFileName, 'w') as outfile:
414 indentation = 2
415 sep = (',', ': ')
416 if writeOptions:
417 indentation = writeOptions.indent
418 sep = writeOptions.separators
419 json.dump(doc_result, outfile, indent=indentation, separators=sep)
420

◆ xmlStringToDocument()

None materialxjson.core.Util.xmlStringToDocument ( mx.Document doc,
str xmlString )
static

Convert an XML string to a MaterialX document.

Parameters
docThe MaterialX document to write to
xmlStringThe XML string to convert

Definition at line 293 of file core.py.

293 def xmlStringToDocument(doc: mx.Document, xmlString: str) -> None:
294 '''
295 @brief Convert an XML string to a MaterialX document
296 @param doc The MaterialX document to write to
297 @param xmlString The XML string to convert
298 '''
299 mx.readFromXmlString(doc, xmlString)
300

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