MaterialX / glTF Texture Procedurals  0.0.1
Utilities for interoperability between MaterialX and glTF Texture Procedurals
Loading...
Searching...
No Matches
gltf_materialx_converter.utilities Namespace Reference

Functions

 load_json_file (filename)
 Load a JSON file.
 
 load_standard_libraries ()
 Load standard MaierialX libraries.
 
 create_working_document (libraries)
 Create a working document and import any libraries.
 
 import_libraries (doc, libraries)
 Import libraries into a document.
 
 read_materialX_document (materialx_doc, input_file)
 Read a MaterialX document from a file.
 
 materialX_doc_to_string (materialx_doc)
 Convert a MaterialX document to a string.
 
 validate_document (doc)
 Validate a MaterialX document.
 
 get_files (rootPath, extension)
 Get all files with a given extension in a directory.
 
 have_version (major, minor, patch)
 Check if the current vesion matches a given version @parm major: The major version number @parm minor: The minor version number @parm patch: The patch version number.
 
 remove_comments (doc)
 Remove all comments from a MaterialX document.
 

Function Documentation

◆ create_working_document()

gltf_materialx_converter.utilities.create_working_document ( libraries)

Create a working document and import any libraries.

Parameters
librariesThe list of definition libraries to import.
Returns
: The new working document

Definition at line 31 of file utilities.py.

31def create_working_document(libraries):
32 '''Create a working document and import any libraries
33 @param libraries: The list of definition libraries to import.
34 @return: The new working document
35 '''
36 doc = mx.createDocument()
37 for lib in libraries:
38 doc.importLibrary(lib)
39
40 return doc
41

◆ get_files()

gltf_materialx_converter.utilities.get_files ( rootPath,
extension )

Get all files with a given extension in a directory.

Parameters
rootPathThe root directory to search.
extensionThe file extension to search for.
Returns
: The list of files with the given extension.

Definition at line 72 of file utilities.py.

72def get_files(rootPath, extension):
73 '''Get all files with a given extension in a directory.
74 @param rootPath: The root directory to search.
75 @param extension: The file extension to search for.
76 @return: The list of files with the given extension.
77 '''
78 filelist = []
79 exts = (extension)
80 for subdir, dirs, files in os.walk(rootPath):
81 for file in files:
82 if file.lower().endswith(exts):
83 filelist.append(os.path.join(subdir, file))
84 return filelist
85

◆ have_version()

gltf_materialx_converter.utilities.have_version ( major,
minor,
patch )

Check if the current vesion matches a given version @parm major: The major version number @parm minor: The minor version number @parm patch: The patch version number.

Returns
: True if the current version is greater or equal to the given version

Definition at line 86 of file utilities.py.

86def have_version(major, minor, patch):
87 '''
88 Check if the current vesion matches a given version
89 @parm major: The major version number
90 @parm minor: The minor version number
91 @parm patch: The patch version number
92 @return: True if the current version is greater or equal to the given version
93 '''
94 imajor, iminor, ipatch = mx.getVersionIntegers()
95
96 if major >= imajor:
97 if major > imajor:
98 return True
99 if iminor >= minor:
100 if iminor > minor:
101 return True
102 if ipatch >= patch:
103 return True
104 return False
105

◆ import_libraries()

gltf_materialx_converter.utilities.import_libraries ( doc,
libraries )

Import libraries into a document.

Parameters
docThe document to import into.
librariesThe list of libraries to import.

Definition at line 42 of file utilities.py.

42def import_libraries(doc, libraries):
43 '''Import libraries into a document.
44 @param doc: The document to import into.
45 @param libraries: The list of libraries to import.
46 '''
47 doc.importLibrary(libraries)
48

◆ load_json_file()

gltf_materialx_converter.utilities.load_json_file ( filename)

Load a JSON file.

Parameters
filenameThe file to load.
Returns
: The JSON string

Definition at line 12 of file utilities.py.

12def load_json_file(filename):
13 '''Load a JSON file.
14 @param filename: The file to load.
15 @return: The JSON string
16 '''
17 json_string = ''
18 with open(filename, 'r') as file:
19 file = json.load(file)
20 json_string = json.dumps(file, indent=2)
21 return json_string
22

◆ load_standard_libraries()

gltf_materialx_converter.utilities.load_standard_libraries ( )

Load standard MaierialX libraries.

Returns
: The standard library and the list of library files.

Definition at line 23 of file utilities.py.

23def load_standard_libraries():
24 '''Load standard MaierialX libraries.
25 @return: The standard library and the list of library files.
26 '''
27 stdlib = mx.createDocument()
28 libFiles = mx.loadLibraries(mx.getDefaultDataLibraryFolders(), mx.getDefaultDataSearchPath(), stdlib)
29 return stdlib, libFiles
30

◆ materialX_doc_to_string()

gltf_materialx_converter.utilities.materialX_doc_to_string ( materialx_doc)

Convert a MaterialX document to a string.

Parameters
materialx_docThe document to convert.
Returns
: The document as a string.

Definition at line 57 of file utilities.py.

57def materialX_doc_to_string(materialx_doc):
58 '''Convert a MaterialX document to a string.
59 @param materialx_doc: The document to convert.
60 @return: The document as a string.
61 '''
62 return mx.writeToXmlString(materialx_doc)
63

◆ read_materialX_document()

gltf_materialx_converter.utilities.read_materialX_document ( materialx_doc,
input_file )

Read a MaterialX document from a file.

Parameters
materialx_docThe MaterialX document to read into.
input_fileThe file to read from.

Definition at line 49 of file utilities.py.

49def read_materialX_document(materialx_doc, input_file):
50 '''
51 Read a MaterialX document from a file.
52 @param materialx_doc: The MaterialX document to read into.
53 @param input_file: The file to read from.
54 '''
55 mx.readFromXmlFile(materialx_doc, input_file)
56

◆ remove_comments()

gltf_materialx_converter.utilities.remove_comments ( doc)

Remove all comments from a MaterialX document.

Parameters
docThe document to remove comments from.

Definition at line 106 of file utilities.py.

106def remove_comments(doc):
107 '''
108 Remove all comments from a MaterialX document.
109 @param doc: The document to remove comments from.
110 '''
111 comments = doc.getChildren()
112 for comment in comments:
113 if comment.getCategory() == 'comment':
114 doc.removeChild(comment.getName())
115

◆ validate_document()

gltf_materialx_converter.utilities.validate_document ( doc)

Validate a MaterialX document.

Parameters
docThe document to validate.
Returns
: The validation result as a tuple of [valid, error string].

Definition at line 64 of file utilities.py.

64def validate_document(doc):
65 '''Validate a MaterialX document.
66 @param doc: The document to validate.
67 @return: The validation result as a tuple of [valid, error string].
68 '''
69 valid, error_string = doc.validate()
70 return valid, error_string
71