listConnections returns a list of connected nodes rather than the actual attribute that is connected. However,
disconnectAttr accepts an attribute as a parameter, e.g.
"pCube1.translateX". It won’t work if you just give it a node name, which is what you’re currently getting from
listConnections has a parameter, though, called “connections” which will give you both attributes involved in each connection. From the docs:
connections(c) - boolean:
If true, return both attributes involved in the connection. The one on the specified object is given first. Default false.
Seems a little silly, but that’s how it works. Probably a legacy code support thing. I remember it tripping me up when I was learning too. EDIT: If you want
listConnections to return pairs of (this object’s attr, other object’s attr), instead of (this object’s attr, other object), you have to be sure to set the
plugs flag to true.
So instead of
conn = mc.listConnections(obj)
You’d have to do
This is not correct. I was thinking of the way
pymel.core.disconnectAttr works. The pymel version can work on one attribute and will disconnect from both sides of it, but
maya.cmds.disconnectAttr needs two attributes as parameters for the source and destination side of the connection. See my other post below for how to do this correctly.
connections = mc.listConnections(obj, connections=True)
One important thing to be aware of… the “pairs” that
listConnections(connections=True) returns are not sublists, but rather it puts everything in one large list (which is annoying). If you want to only disconnect attrs from the specified object you’ll have to iterate over the list two at a time and only disconnect the first of each pair or you can accidentally break other things. The pairs are also not always sorted in the order that
mc.disconnectAttr needs them to be, which isn’t super helpful.
All of this is said to help you understand how it works, but to be honest with you I never use
listConnections with the “connections” flag unless I need to do something very specific. Instead if I want to disconnect all attributes I just get all the keyable attributes with
mc.listAttr(obj, keyable=True) and disconnect each of them in a for loop. I would recommend doing it that way instead if it works for your use case.