71 @class QuiltiX_JSON_serializer
73 def __init__(self, editor, root):
75 Initialize the JSON serializer.
83 editor.file_menu.addSeparator()
84 gltfMenu = editor.file_menu.addMenu(
"JSON")
87 export_json = QAction(
"Save JSON...", editor)
88 export_json.triggered.connect(self.export_json_triggered)
89 gltfMenu.addAction(export_json)
92 import_json = QAction(
"Load JSON...", editor)
93 import_json.triggered.connect(self.import_json_triggered)
94 gltfMenu.addAction(import_json)
97 show_json_text = QAction(
"Show as JSON...", editor)
98 show_json_text.triggered.connect(self.show_json_triggered)
99 gltfMenu.addAction(show_json_text)
101 def set_indent(self, indent):
103 Set the indent for the JSON output.
107 def get_json_from_graph(self):
109 Get the JSON for the given MaterialX document.
111 doc = self.editor.qx_node_graph.get_current_mx_graph_doc()
113 exporter = jsoncore.MaterialXJson()
114 json_result = exporter.documentToJSON(doc)
118 def show_json_triggered(self):
120 Show the JSON for the current MaterialX document.
122 json_result = self.get_json_from_graph()
126 text = jsoncore.Util.jsonToJSONString(json_result, self.indent)
127 self.show_text_box(text,
"JSON Representation")
129 def export_json_triggered(self, editor):
131 Export the current graph to a JSON file.
133 start_path = self.editor.mx_selection_path
135 start_path = self.editor.geometry_selection_path
138 start_path = os.path.join(self.root,
"resources",
"materials")
140 path = self.editor.request_filepath(
141 title=
"Save JSON file",
142 start_path=start_path,
143 file_filter=
"JSON files (*.json)",
150 json_result = self.get_json_from_graph()
154 with open(path,
"w"):
155 jsoncore.Util.writeJson(json_result, path, 2)
156 logger.info(
"Wrote JSON file: " + path)
158 self.editor.set_current_filepath(path)
160 def import_json_triggered(self, editor):
162 Import a JSON file into the current graph.
164 start_path = self.editor.mx_selection_path
166 start_path = self.editor.geometry_selection_path
169 start_path = os.path.join(self.root,
"resources",
"materials")
171 path = self.editor.request_filepath(
172 title=
"Load JSON file",
173 start_path=start_path,
174 file_filter=
"JSON files (*.json)",
180 if not os.path.exists(path):
181 logger.error(
"Cannot find input file: " + path)
184 doc = jsoncore.Util.jsonFileToXml(path)
186 logger.info(
"Loaded JSON file: " + path)
187 self.editor.mx_selection_path = path
188 self.editor.qx_node_graph.load_graph_from_mx_doc(doc)
189 self.editor.qx_node_graph.mx_file_loaded.emit(path)
192 def show_text_box(self, text, title=""):
194 Show a text box with the given text.
196 te_text = QTextEdit()
197 te_text.setReadOnly(
True)
198 te_text.setParent(self.editor, QtCore.Qt.Window)
199 te_text.setWindowTitle(title)
200 te_text.resize(1000, 800)
203 jsonHighlighter = JsonHighlighter()
204 highlighted_html = jsonHighlighter.highlight(text)
205 te_text.setHtml(highlighted_html)
207 te_text.setPlainText(text)