Ok that’s great you say, but what if I want to disconnect every connection to a node, and not just to a single attribute.
Well you can also call listConnections on a node. So:
cmds.listConnections(node)
will return all the connections to all attributes on that node. This introduces the wrinkle that you no longer know which attribute the connection is for, so you need to use the connections flag, which will cause it to return both ends of the connection. It returns this in a flat list: [thisNode, thatNode, thisNode, thatNode]. It ALWAYS returns the connection that goes to the node you are querying first. Meaning that regardless of whether it’s returning source, destination, or both types of connections, the connection to the node you are querying will come first. This is and important distinction because disconnectAttr requires the source attribute first, and listConnections doesn’t always return it that way.
To return all outgoing connections to a node:
outgoingPairs = cmds.listConnections(node, plug=True, connections=True, source=False)
# ['thisNode.attr1', 'thatNode.attr2', 'thisNode.translateZ', 'thatNode.rotateY']
Where every two items is a connection:
thisNode.attr1 -> thatNode.attr2
thisNode.translateZ -> thatNode.rotateY
I use zip to combine them into pairs. Look it up if you aren’t familiar with it:
outgoingPairs = zip(outgoingConns[::2], outgoingConns[1::2])
# This is easy to work with because that's the order disconnectAttr expects. So you can do:
for srcAttr, destAttr in outgoingPairs:
cmds.disconnectAttr(srcAttr, destAttr)
However when you query the incoming connections it’s flipped:
cmds.listConnections(node, plugs=True, connections=True, destination=False)
# ['thisNode.translateY', 'thatNode.upValue', 'thisNode.forward', 'thatNode.translateZ']
Same idea that every two items is a connection, however the order is reversed, now it’s:
thatNode.upValue -> thisNode.translateY
thatNode.translateZ -> thisNode.forward
So you have to flip the list when you zip it:
incomingPairs = zip(inConns[1::2], inConns[::2])
for srcAttr, destAttr in incomingPairs:
cmds.disconnectAttr(srcAttr, destAttr)
You can put this together in one big disconnectAll function:
def disconnectAll(node, source=True, destination=True):
connectionPairs = []
if source:
conns = cmds.listConnections('pCube2', plugs=True, connections=True, destination=False)
if conns:
connectPairs.extend(zip(conns[1::2], conns[::2]))
if destination:
conns = cmds.listConnections('pCube2', plugs=True, connections=True, source=False)
if conns:
connectionPairs.extend(zip(conns[::2], conns[1::2]))
for srcAttr, destAttr in connectionPairs:
cmds.disconnectAttr(srcAttr, destAttr)
The real kicker is that this is how you do all that in PyMel:
pm.disconnectAttr(node)
I wrote this fast and with an assumption that you are very new to python in maya so please ask for additional details if something isn’t clear or I skipped over it (or tell me I’m being pedantic if you know parts of this). Also I wrote all this without testing it so there’s a chance I got the order or a flag wrong.