156 @class ShadingLangaugeXSerializer
158 def __init__(self, editor, root):
160 Initialize the serializer.
168 editor.file_menu.addSeparator()
169 slxMenu = editor.file_menu.addMenu(
"SLX")
172 export_slx = QAction(
"Save SLX...", editor)
173 export_slx.triggered.connect(self.export_slx_triggered)
174 slxMenu.addAction(export_slx)
177 import_slx = QAction(
"Load SLX...", editor)
178 import_slx.triggered.connect(self.import_slx_triggered)
179 slxMenu.addAction(import_slx)
182 show_slx_text = QAction(
"Show as SLX...", editor)
183 show_slx_text.triggered.connect(self.show_slx_triggered)
184 slxMenu.addAction(show_slx_text)
186 def get_header(self):
188 Get the header for the SLX output.
190 header =
"// MaterialX SLX content generated via QuiltiX plugin\n"
191 header += f
"// Materialx version: {mx.getVersionString()}\n"
192 header += f
"// SLX version: {importlib_version('mxslc')}\n"
195 def set_indent(self, indent):
197 Set the indent for the SLX output.
201 def get_mtlx_from_graph(self):
203 Get the MateriaLX for the current graph.
205 doc = self.editor.qx_node_graph.get_current_mx_graph_doc()
207 result = mx.writeToXmlString(doc)
212 def decompile_string(self, mtlx_content: str) -> str:
214 Decompile MaterialX in XML format to SLX string.
216 result =
"// Faied to decompile MaterialX content."
218 decompiler = Decompiler(mtlx_content)
219 result = decompiler.decompile()
220 except Exception
as e:
221 result = f
"// Unable to decompile MaterialX content: {e}"
225 def show_slx_triggered(self):
227 Show the SLX for the current MaterialX document.
229 mtlx_content = self.get_mtlx_from_graph()
230 mxsl_output = self.decompile_string(mtlx_content)
231 mxsl_output = self.get_header() + mxsl_output
235 self.show_c_code_dialog(mxsl_output,
"SLX Representation")
237 def export_slx_triggered(self, editor):
239 Export the current graph to a SLX file.
241 start_path = self.editor.mx_selection_path
243 start_path = self.editor.geometry_selection_path
246 start_path = os.path.join(self.root,
"resources",
"materials")
248 path = self.editor.request_filepath(
249 title=
"Save SLX file",
250 start_path=start_path,
251 file_filter=
"SLX files (*.mxsl)",
257 mtlx_content = self.get_mtlx_from_graph()
258 mxsl_output = self.decompile_string(mtlx_content)
263 mxsl_output_file = self.get_header() + mxsl_output
264 with open(path,
"w")
as f:
265 f.write(mxsl_output_file)
266 logger.info(
"Wrote SLX file: " + path)
268 self.editor.set_current_filepath(path)
270 def import_slx_triggered(self, editor):
272 Import a SLX file into the current graph.
274 start_path = self.editor.mx_selection_path
276 start_path = self.editor.geometry_selection_path
279 start_path = os.path.join(self.root,
"resources",
"materials")
281 path = self.editor.request_filepath(
282 title=
"Load SLX file",
283 start_path=start_path,
284 file_filter=
"SLX files (*.mxsl)",
290 if not os.path.exists(path):
291 logger.error(
"Cannot find input file: " + path)
297 mtlx_path = path.replace(
'.mxsl',
'.mtlx')
299 compile_file(Path(path), mtlx_path)
301 if not os.path.exists(mtlx_path):
302 logger.error(
"Failed to compile SLX file to MaterialX: " + path)
304 logger.info(
"Compiled SLX file to MaterialX: " + mtlx_path)
305 doc = mx.createDocument()
306 mx.readFromXmlFile(doc, mtlx_path)
308 with open(path,
"r")
as f:
309 mxsl_output = f.read()
310 self.show_c_code_dialog(mxsl_output,
"SLX Loaded")
311 except Exception
as e:
312 logger.error(f
"Failed to compile SLX file: {e}")
315 logger.info(
"Loaded SLX file: " + path)
323 self.editor.mx_selection_path = mtlx_path
326 self.editor.qx_node_graph.load_graph_from_mx_file(mtlx_path)
328 self.editor.qx_node_graph.mx_file_loaded.emit(mtlx_path)
330 def show_code_dialog(self, text, title="", language='c'):
333 highlighted_html = slxHighlighter.highlight(text)
335 web_view = QWebEngineView()
336 web_view.setHtml(highlighted_html)
337 web_view.setParent(self.editor, QtCore.Qt.Window)
338 web_view.setWindowTitle(title)
339 web_view.resize(1000, 800)
342 te_text = QTextEdit()
343 te_text.setReadOnly(
True)
344 te_text.setParent(self.editor, QtCore.Qt.Window)
345 te_text.setWindowTitle(title)
346 te_text.resize(1000, 800)
347 te_text.setPlainText(text)
351 def show_cpp_code_dialog(self, text, title="C++ Code"):
353 Show a text box with C++ syntax highlighting.
354 @param text The C++ code to display
355 @param title The window title
357 self.show_code_dialog(text, title,
'cpp')
359 def show_c_code_dialog(self, text, title="C Code"):
361 Show a text box with C syntax highlighting.
362 @param text The C code to display
363 @param title The window title
365 self.show_code_dialog(text, title,
'c')