Create USD material
# The following is a scripting example of how to create a USD material.
import ufe
import mayaUsd_createStageWithNewLayer
# USD materials live under a USD stage, so it's necessary to create a USD stage first. This can be
# done as follows.
stagePathString = mayaUsd_createStageWithNewLayer.createStageWithNewLayer()
# 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 path object. 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.
stagePath = ufe.PathString.path(stagePathString)
stageItem = ufe.Hierarchy.createItem(stagePath)
# The context menu of USD stages contains entries to create new USD prims like materials. All
# context menu actions that are visible in the UI can be accessed through the `ufe.ContextOps`
# interface. It can be used as follows to create a new material.
stageContextOps = ufe.ContextOps.contextOps(stageItem)
stageContextOps.doOp(['Add New Prim', 'Material'])
# Note that the UI displays nice names which might differ from the internal names used by the
# interface. 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 material, as it's the container for shading
# graphs. Thus, they'll need the scene item of the material. Unfortunately, the context ops don't
# return the item, but since the material is the most recently added child of the stage, it can be
# found easily in the scene hierarchy.
stageHierarchy = ufe.Hierarchy.hierarchy(stageItem)
materialItem = stageHierarchy.children()[-1]
print(f'Created new USD material at path "{ufe.PathString.string(materialItem.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 USD material or its equivalent in other data models.
shadingContainer = materialItem