172 @class ShadingLangaugeXSerializer
174 def __init__(self, editor, root):
176 Initialize the serializer.
184 editor.file_menu.addSeparator()
185 slxMenu = editor.file_menu.addMenu(
"SLX")
188 export_slx = QAction(
"Save SLX...", editor)
189 export_slx.triggered.connect(self.export_slx_triggered)
190 slxMenu.addAction(export_slx)
193 import_slx = QAction(
"Load SLX...", editor)
194 import_slx.triggered.connect(self.import_slx_triggered)
195 slxMenu.addAction(import_slx)
198 show_slx_text = QAction(
"Show as SLX...", editor)
199 show_slx_text.triggered.connect(self.show_slx_triggered)
200 slxMenu.addAction(show_slx_text)
202 def get_header(self):
204 Get the header for the SLX output.
206 header =
"// MaterialX SLX content generated via QuiltiX plugin\n"
207 header += f
"// Materialx version: {mx.getVersionString()}\n"
208 header += f
"// SLX version: {importlib_version('mxslc')}\n"
211 def set_indent(self, indent):
213 Set the indent for the SLX output.
217 def get_mtlx_from_graph(self):
219 Get the MateriaLX for the current graph.
221 doc = self.editor.qx_node_graph.get_current_mx_graph_doc()
223 result = mx.writeToXmlString(doc)
228 def decompile_string(self, mtlx_content: str) -> str:
230 Decompile MaterialX in XML format to SLX string.
232 result =
"// Faied to decompile MaterialX content."
234 decompiler = Decompiler(mtlx_content)
235 result = decompiler.decompile()
236 except Exception
as e:
237 result = f
"// Unable to decompile MaterialX content: {e}"
241 def show_slx_triggered(self):
243 Show the SLX for the current MaterialX document.
245 mtlx_content = self.get_mtlx_from_graph()
246 mxsl_output = self.decompile_string(mtlx_content)
247 mxsl_output = self.get_header() + mxsl_output
251 self.show_c_code_dialog(mxsl_output,
"SLX Representation")
253 def export_slx_triggered(self, editor):
255 Export the current graph to a SLX file.
257 start_path = self.editor.mx_selection_path
259 start_path = self.editor.geometry_selection_path
262 start_path = os.path.join(self.root,
"resources",
"materials")
264 path = self.editor.request_filepath(
265 title=
"Save SLX file",
266 start_path=start_path,
267 file_filter=
"SLX files (*.mxsl)",
273 mtlx_content = self.get_mtlx_from_graph()
274 mxsl_output = self.decompile_string(mtlx_content)
279 mxsl_output_file = self.get_header() + mxsl_output
280 with open(path,
"w")
as f:
281 f.write(mxsl_output_file)
282 logger.info(
"Wrote SLX file: " + path)
284 self.editor.set_current_filepath(path)
286 def import_slx_triggered(self, editor):
288 Import a SLX file into the current graph.
290 start_path = self.editor.mx_selection_path
292 start_path = self.editor.geometry_selection_path
295 start_path = os.path.join(self.root,
"resources",
"materials")
297 path = self.editor.request_filepath(
298 title=
"Load SLX file",
299 start_path=start_path,
300 file_filter=
"SLX files (*.mxsl)",
306 if not os.path.exists(path):
307 logger.error(
"Cannot find input file: " + path)
313 mtlx_path = path.replace(
'.mxsl',
'.mtlx')
315 compile_file(Path(path), mtlx_path)
317 if not os.path.exists(mtlx_path):
318 logger.error(
"Failed to compile SLX file to MaterialX: " + path)
320 logger.info(
"Compiled SLX file to MaterialX: " + mtlx_path)
321 doc = mx.createDocument()
322 mx.readFromXmlFile(doc, mtlx_path)
324 with open(path,
"r")
as f:
325 mxsl_output = f.read()
326 self.show_c_code_dialog(mxsl_output,
"SLX Loaded")
327 except Exception
as e:
328 logger.error(f
"Failed to compile SLX file: {e}")
331 logger.info(
"Loaded SLX file: " + path)
339 self.editor.mx_selection_path = mtlx_path
342 self.editor.qx_node_graph.load_graph_from_mx_file(mtlx_path)
344 self.editor.qx_node_graph.mx_file_loaded.emit(mtlx_path)
346 def show_code_dialog(self, text, title="", language='c'):
349 highlighted_html = slxHighlighter.highlight(text)
351 web_view = QWebEngineView()
352 web_view.setHtml(highlighted_html)
353 web_view.setParent(self.editor, QtCore.Qt.Window)
354 web_view.setWindowTitle(title)
355 web_view.resize(1000, 800)
358 te_text = QTextEdit()
359 te_text.setReadOnly(
True)
360 te_text.setParent(self.editor, QtCore.Qt.Window)
361 te_text.setWindowTitle(title)
362 te_text.resize(1000, 800)
363 te_text.setPlainText(text)
367 def show_cpp_code_dialog(self, text, title="C++ Code"):
369 Show a text box with C++ syntax highlighting.
370 @param text The C++ code to display
371 @param title The window title
373 self.show_code_dialog(text, title,
'cpp')
375 def show_c_code_dialog(self, text, title="C Code"):
377 Show a text box with C syntax highlighting.
378 @param text The C code to display
379 @param title The window title
381 self.show_code_dialog(text, title,
'c')