Create MaterialX Document
# The following is a scripting example of how to create a MaterialX document.
import maya.mel as mel
import ufe
# MaterialX documents live under a MaterialX stack, so it's necessary to create a MaterialX stack
# first. The MaterialX stack is a Maya node, which can be created using mel.
stackName = mel.eval("createNode materialxStack")
# The context menu of MaterialX stacks contains an entry to create a new MaterialX document. Context
# menu actions are accessible through UFE. To call UFE interfaces on an object, the full path of the
# object is required. The `createNode` mel command only returns the name, but the `ls` command can
# bue used to get the full path.
stackPathString = mel.eval("ls -l " + stackName)[0]
# UFE stores paths as `ufe.Path` objects. `ufe.PathString.path()` and `ufe.PathString.string()` can
# be used to convert between `ufe.Path` objects an their string representation.
stackPath = ufe.PathString.path(stackPathString)
# UFE usually operates on `ufe.SceneItem` objects, which are handles to existing items in the scene.
# `ufe.Hierarchy.createItem()` can be used to create a `ufe.SceneItem` from a `ufe.Path`.
stackItem = ufe.Hierarchy.createItem(stackPath)
# The context menu actions can be accessed through the `ufe.ContextOps` interface. All context menu
# actions that are visible in the UI can be accessed through this interface.
stackContextOps = ufe.ContextOps.contextOps(stackItem)
stackContextOps.doOp(['MxCreateDocument'])
# Note that the UI displays nice names like "Create MaterialX Document" but the interface uses
# internal names like "MxCreateDocument" for each action. To find the names of other actions, use:
# ```python
# for contextItem in contextOps.getItems([]):
# if not contextItem.separator:
# print(f'Label = "{contextItem.label}", Internal Name = "{contextItem.item}"')
# ```
# Most other scripting examples will operate on the MaterialX document, as it's the container for
# shading graphs. Thus, they'll need the scene item of the MaterialX document. Unfortunately, the
# context ops don't return the item, but since the document is the most recently added child of the
# stack, it can be found easily in the scene hierarchy.
stackHierarchy = ufe.Hierarchy.hierarchy(stackItem)
documentItem = stackHierarchy.children()[-1]
print(f'Created new MaterialX document at path "{ufe.PathString.string(documentItem.path())}"')
# Most of the other examples use generic UFE code that works for any data model. Thus, they use
# `shadingContainer` as a generic alias for the MaterialX document or its equivalent in other data
# models.
shadingContainer = documentItem