Save MaterialX Document
# The following is a scripting example of how to export or save a MaterialX document.
import maya.mel as mel
import ufe
# Create a new MaterialX stack and a document.
stackName = mel.eval("createNode materialxStack")
stackPathString = mel.eval("ls -l " + stackName)[0]
stackItem = ufe.Hierarchy.createItem(ufe.PathString.path(stackPathString))
filepath = 'C:/dev/temp/some/path/to/materialxFile.mtlx'
stackContextOps = ufe.ContextOps.contextOps(stackItem)
stackContextOps.doOp(['MxCreateDocument'])
stackContextOps.doOp(['MxImportDocument', filepath])
stackContextOps.doOp(['MxLoadDocument', filepath])
# Get the scene items of the documents.
stackHierarchy = ufe.Hierarchy.hierarchy(stackItem)
emptyDocument = stackHierarchy.children()[-3]
importedDocument = stackHierarchy.children()[-2]
loadedDocument = stackHierarchy.children()[-1]
# The context ops of each document can be used to either export or save it:
# If the document was created from scratch or imported, it can be exported like this. Usually, the
# export action of the context menu action will open a file picker, but if a filepath is specified,
# that file will be written without requiring manual interaction.
exportDir = 'C:/dev/temp/some/path'
ufe.ContextOps.contextOps(emptyDocument).doOp(['MxExportDocument', f"{exportDir}/emptyDocument.mtlx"])
ufe.ContextOps.contextOps(importedDocument).doOp(['MxExportDocument', f"{exportDir}/importedDocument.mtlx"])
print(f'Exported "{ufe.PathString.string(emptyDocument.path())}" to directory "{exportDir}"')
print(f'Exported "{ufe.PathString.string(importedDocument.path())}" to directory "{exportDir}"')
# If the document was loaded, any unsaved changes can we written like this.
ufe.ContextOps.contextOps(loadedDocument).doOp(['MxSaveDocument'])
print(f'Saved changes of "{ufe.PathString.string(loadedDocument.path())}" to "{filepath}"')
# Check the LookdevX documentation for more information about the import/export and load/save workflows.