Explode Compound

# The following is an example on how to explode a compound.
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 and connect them.
runTimeId = shadingContainer.runTimeId()
items = []
for _ in range(5):
    items.append(ufe.NodeDef.definition(runTimeId, 'ND_add_float').createNode(shadingContainer, ufe.PathComponent('add')))
for i in range(1, 5):
    srcInfo = ufe.AttributeInfo(items[i - 1].path(), 'outputs:out')
    dstInfo = ufe.AttributeInfo(items[i].path(), 'inputs:in1')
    ufe.RunTimeMgr.instance().connectionHandler(runTimeId).connect(srcInfo, dstInfo)

# Group a few of the nodes into a compound.
compound = lx.createCompound(shadingContainer, items=[items[1], items[2], items[3]])

# Explode the compound. The function only takes a single parameter which is the compound to explode.
# Note that exploding compounds maintains the connections across the compound boundary. It simply
# flattens the graph.
pathMap = lx.explodeCompound(compound)

# The returned map maps the paths of the compounds children to their new items.
print('The children of the compound were moved outside of the compound:')
for oldPath, newItem in pathMap.items():
    print(f'- {oldPath.back()} was moved')
    print(f'    from {ufe.PathString.string(oldPath)}')
    print(f'    to   {ufe.PathString.string(newItem.path())}')