Tips for effective production : some scripts to know… part. II

The second part arrived !
The last time we saw how to add proxies for our referenced objects. Now we gonna work on a script which can switch between original referenced objects and new proxies.

2 - Quick Proxy Switch in a Maya scene

Like the last one, this script is very useful when you have many different objects in a maya scene.
Here is the script :

$box = `confirmDialog
    -title "Quick Proxies Switch"
    -message "What you want to switch ?"
    -button "All referenced objects" -button "Only selected referenced objects" -button "Fermer"
    -defaultButton "All referenced objects" -cancelButton "Close"
    -dismissString "Close"`;
if ($box == "All referenced objects") {

select -allDagObjects; //select all deletable root level dag objects.

global proc proxyQuickSwitch() {  
    string $selection[] = `ls -sl`;                                                     """create the objects name list"""
    if(size($selection)) {
        for($object in $selection) {                                                    """create the loop to apply the script on all objects"""
        string $currentRN = `referenceQuery -rfn $object`;                              """store current reference node name of the object"""
        string $pmList[] = `listConnections -type "proxyManager" $currentRN`;
        string $proxyManager = $pmList[0];
        string $referenceNodes[] = `listConnections -type "reference" $proxyManager`;   """create list of reference nodes for the object"""
        string $targetRN;
        for ($rn in $referenceNodes) {                                                  """create the loop for all proxies detected in the reference nodes list"""
            if($rn != $currentRN) {                                                     """of course, the reference node name used in the loop doesn't have to be equal to the current one"""
                $targetRN = $rn;                                                        """store the var with our new reference node name"""
                break;
            }
        }
        proxySwitch($targetRN);
        }
    }
    else {
        warning "Please select at least one valid referenced object";
    }

}

proxyQuickSwitch;

}

else if($box == "Only selected referenced objects"){

global proc proxyQuickSwitch() {  
    string $selection[] = `ls -sl`;
    if(size($selection)) {
        for($object in $selection) {
        string $currentRN = `referenceQuery -rfn $object`;
        string $pmList[] = `listConnections -type "proxyManager" $currentRN`;
        string $proxyManager = $pmList[0];
        string $referenceNodes[] = `listConnections -type "reference" $proxyManager`;
        string $targetRN;
        for ($rn in $referenceNodes) {
            if($rn != $currentRN) {
                $targetRN = $rn;
                break;
            }
        }
        proxySwitch($targetRN);
        }
    }
    else {
        warning "Please select at least one valid referenced object";
    }

}

proxyQuickSwitch;

}

We use a dialog box to permit us to choose between a global switch or a specific one. The only thing which changes between them is the select -allDagObjects; line.
If you use it with referenced objects which don't have proxy, nothing happens, because the script has to find a reference node name value which is not equal to the current used one.
The use of for in to create a loop with our list of reference nodes could be a good way to switch more than two different versions of proxy. The script will use the order of your proxies list which can be seen in the Reference Editor :

proxy

That's all !