Load MaterialX Document
# The following is a scripting example of how to import or load a MaterialX document.
import maya.mel as mel
import ufe
# Create a new MaterialX stack.
stackName = mel.eval("createNode materialxStack")
stackPathString = mel.eval("ls -l " + stackName)[0]
stackItem = ufe.Hierarchy.createItem(ufe.PathString.path(stackPathString))
# Use the context ops to import or load a document. Usually, the context menu action will open a
# file picker, but if a filepath is specified, that file will be read without requiring manual
# interaction.
filepath = 'C:/dev/temp/some/path/to/materialxFile.mtlx'
stackContextOps = ufe.ContextOps.contextOps(stackItem)
stackContextOps.doOp(['MxImportDocument', filepath])
stackContextOps.doOp(['MxLoadDocument', filepath])
# Note that there are two ways to read from a MaterialX document. Importing will copy the MaterialX
# data into the Maya scene and won't rely on the MaterialX file on disk. Loading will keep the
# MaterialX file as a sidecar and allows using relative texture paths within the document. Check the
# LookdevX documentation for more information.
# Get the scene items of the documents.
stackHierarchy = ufe.Hierarchy.hierarchy(stackItem)
importedDocument = stackHierarchy.children()[-2]
loadedDocument = stackHierarchy.children()[-1]
print(f'Imported MaterialX document "{ufe.PathString.string(importedDocument.path())}" from file "{filepath}"')
print(f'Loaded MaterialX document "{ufe.PathString.string(loadedDocument.path())}" from file "{filepath}"')