Rename Item
# The following is an example of how to rename an item.
import ufe
import lookdevx as lx
if not shadingContainer:
raise Exception('This example requires a shading container i.e., a MaterialX document or a USD '
'material. Check the other examples to see how to create one.')
# Create some nodes.
runTimeId = shadingContainer.runTimeId()
item1 = ufe.NodeDef.definition(runTimeId, 'ND_add_float').createNode(shadingContainer, ufe.PathComponent('add'))
item2 = ufe.NodeDef.definition(runTimeId, 'ND_add_float').createNode(shadingContainer, ufe.PathComponent('add'))
item3 = ufe.NodeDef.definition(runTimeId, 'ND_add_float').createNode(shadingContainer, ufe.PathComponent('usedName1'))
# Items can be renamed using the `ufe.SceneItemOps` interface.
item1Ops = ufe.SceneItemOps.sceneItemOps(item1)
renamedItem1 = item1Ops.renameItem(ufe.PathComponent("newName1"))
print(f'Renamed {item1.nodeName()} to {renamedItem1.nodeName()}.')
# Alternatively, the LookdevX API has a function to rename items as well.
renamedItem2 = lx.renameItem(item2, ufe.PathComponent("newName2"))
print(f'Renamed {item2.nodeName()} to {renamedItem2.nodeName()}.')
# Either way, if the desired name is already taken, a number suffix will be appended or incremented
# to make the name unique.
unique1 = ufe.SceneItemOps.sceneItemOps(renamedItem1).renameItem(ufe.PathComponent("usedName1"))
unique2 = lx.renameItem(renamedItem2, ufe.PathComponent("usedName1"))
print(f'Renamed {renamedItem1.nodeName()} to {unique1.nodeName()}.')
print(f'Renamed {renamedItem2.nodeName()} to {unique2.nodeName()}.')