MaterialX / glTF Texture Procedurals  0.0.1
Utilities for interoperability between MaterialX and glTF Texture Procedurals
Loading...
Searching...
No Matches
utilities.py
Go to the documentation of this file.
1# core.py
2
3'''
4@file utilities.py
5This module contains the utilities for MaterialX glTF Procedural Texture graph conversion.
6'''
7import os
8import json
9import MaterialX as mx
10import logging as lg
11
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
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
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
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
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
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
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
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
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
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
materialX_doc_to_string(materialx_doc)
Convert a MaterialX document to a string.
Definition utilities.py:57
get_files(rootPath, extension)
Get all files with a given extension in a directory.
Definition utilities.py:72
create_working_document(libraries)
Create a working document and import any libraries.
Definition utilities.py:31
load_json_file(filename)
Load a JSON file.
Definition utilities.py:12
have_version(major, minor, patch)
Check if the current vesion matches a given version @parm major: The major version number @parm minor...
Definition utilities.py:86
load_standard_libraries()
Load standard MaierialX libraries.
Definition utilities.py:23
remove_comments(doc)
Remove all comments from a MaterialX document.
Definition utilities.py:106
import_libraries(doc, libraries)
Import libraries into a document.
Definition utilities.py:42
validate_document(doc)
Validate a MaterialX document.
Definition utilities.py:64
read_materialX_document(materialx_doc, input_file)
Read a MaterialX document from a file.
Definition utilities.py:49