Nodes within Maya can be connected via node connections between
attributes. We can use the mel function listConnections
to retrieve any connections to a node. For example
Example 1 : retrieving a list of source
connections
Source connections are basically output connections. If we
use the -destination/-d flags and
the -source/-s flags for the listConnections
command we can choose which sets of connections to return.
// a procedure to print all source connections from the
// specified node.procSrcConnectionInfo(string $node) {
// get the source and destination connections
$src_connections = `listConnections -s true -d false $node`;
print("src: ");
for($c in $src_connections) {
print($c + ",");
}
print("\n");
}
Example 2 : retrieving a list of destination
connections
Destination connections are basically input connections. If
we use the -destination/-d flags and
the -source/-s flags for the listConnections
command we can choose which sets of connections to return.
// a procedure to print all destination connections from the
// specified node.procDstConnectionInfo(string $node) {
// get the source and destination connections
$dst_connections = `listConnections -s false -d true $node`;
print("dst: ");
for($c in $dst_connections) {
print($c + ",");
}
print("\n");
}
Example 3 : Retrieving the plugs
If you specify the -p flag for listConnections it returns the
attributes on the other node to which it is connected. This
example wraps up the previous two...
// a procedure to print all src and dst connections from the
// specified node.procConnectionInfo(string $node) {
// get the source and destination connections
$src_connections = `listConnections -s 1 -d 0 -p 1 $node`;
$dst_connections = `listConnections -s 0 -d 1 -p 1 $node`;
// if no connections are present to this node, returnif(size($src_connections) == 0 ||
size($dst_connections) == 0)
return;
// print the node name and its connectionsprint($node + " {\n");
if(size($src_connections) != 0) {
print("\tsrc: ");
for($c in $src_connections) {
print($c + ",");
}
print("\n");
}
if(size($dst_connections) != 0) {
print("\tdst: ");
for($c in $dst_connections) {
print($c + ",");
}
print("\n");
}
print("}\n");
}
{
// list all scene nodes
$nodes = `ls`;
// print connection info for all nodesfor($node in $nodes)
ConnectionInfo($node);
}
Example 4 : Retrieving the plugs 2
If you specify the -c flag for listConnections it returns the
attributes on the other node to which it is connected, as well
as the attribute on this node to which it is connected.
// a procedure to print all src and dst connections from the
// specified node.procConnectionInfo(string $node) {
// get the source and destination connections
$src = `listConnections -s 1 -d 0 -c 1 $node`;
$dst = `listConnections -s 0 -d 1 -c 1 $node`;
// if no connections are present to this node, returnif(size($src) == 0 ||
size($dst) == 0)
return;
// print the node name and its connectionsprint($node + " {\n");
if(size($src) != 0) {
print("\tsrc: ");
for($i=0;$i<size($src);$i+=2) {
print($src[$i]+"<-"+$src[$i+1]+" ");
}
print("\n");
}
if(size($dst) != 0) {
print("\tdst: ");
for($i=0;$i<size($dst);$i+=2) {
print($dst[$i]+"->"+$dst[$i+1]+" ");
}
print("\n");
}
print("}\n");
}
{
// list all scene nodes
$nodes = `ls`;
// print connection info for all nodesfor($node in $nodes)
ConnectionInfo($node);
}
Example 5 : Making Connections
You can make use of the connectAttr function to connect one
attribute to another. Normally if the destination attribute
is in use, then the attempt to make the connection will fail.
To solve this you can force it with the -f
flag.
{
// create two locator nodescreateNode locator -n loc1;
createNode locator -n loc2;
// locator nodes are pretty useless, we want their transforms
$parents1 = `listRelatives -p "loc1"`;
$parents2 = `listRelatives -p "loc2"`;
xform -t 3 2 0 $parents1[0];
// connect the attributesconnectAttr -f ($parents1[0]+".tx") ($parents2[0]+".sy"); }