Connection UI


'''
quickConnect.py
@author: Kert Saville
@summary: Creates a pop-up window for connecting attributes
@version: 1.0

For use with zooTools
http://www.macaronikazoo.com/?page_id=311

'''


import zooPyMaya.baseMelUI as ui
import maya.cmds as mc

DEBUG = True



class QuickConnectWindow(ui.BaseMelWindow):
    WINDOW_NAME = 'qc_win'
    WINDOW_TITLE = 'Quick Connect'
    DEFAULT_SIZE = 210, 350
    DEFAULT_MENU = None
    
    
    def __init__(self):
        #text field layout (bottom half)
        lyt1 = ui.MelColumnLayout(self, cw = (1), cat = ('both', 1))
        #check box Layout (top half)
        lyt2 = mc.rowColumnLayout(self, p = lyt1, nr=4,
                                    cat=[(1, 'both', 1), (2, 'both', 1),
                                    (3, 'both', 1), (4, 'both', 1)])
        
        #Define all ui controls/elements
        self.build_checkboxes(lyt2)
        self.build_fields(lyt1)
        
        #list of checkboxes
        self.cboxes = (self.txBox, self.tyBox, self.tzBox,
                    self.rxBox, self.ryBox, self.rzBox,
                    self.sxBox, self.syBox, self.szBox,
                    self.visibilityBox)
        
        #self.setTitle("Quick Connect")
        self.show()
        
    def build_checkboxes(self, parent):        

        self.txBox = ui.MelCheckBox(parent, ann='.translateX', l='TranslateX', v=True)
        self.tyBox = ui.MelCheckBox(parent, ann='.translateY', l='TranslateY', v=True)
        self.tzBox = ui.MelCheckBox(parent, ann='.translateZ', l='TranslateZ', v=True)
        ui.MelButton(parent, l='Translate', command=self.toggle_translate)
        self.rxBox = ui.MelCheckBox(parent, ann='.rotateX', l='RotateX', v=True)
        self.ryBox = ui.MelCheckBox(parent, ann='.rotateY', l='RotateY', v=True)
        self.rzBox = ui.MelCheckBox(parent, ann='.rotateZ', l='RotateZ', v=True)
        ui.MelButton(parent, l='Rotate', command=self.toggle_rotate)
        self.sxBox = ui.MelCheckBox(parent, ann='.scaleX', l='ScaleX', v=True)
        self.syBox = ui.MelCheckBox(parent, ann='.scaleY', l='ScaleY', v=True)
        self.szBox = ui.MelCheckBox(parent, ann='.scaleZ', l='ScaleZ', v=True)
        ui.MelButton(parent, l='Scale', command=self.toggle_scale)
        
        
        
    def build_fields (self, parent):
        #--------------------------------------------
        ui.MelSeparator(parent, height=6, style='in')        
        self.visibilityBox = ui.MelCheckBox (parent, ann='.visibility', l='Visibility',
                                            v=False, align='center')
        #--------------------------------------------
        ui.MelSeparator(parent, height=3, style='in')        
        ui.MelLabel(parent, label='Source Attribute', align='center' )
        self.sourceField = ui.MelTextField(parent)
        ui.MelButton(parent, l='Source from Channel Box', command=self.source_channel_box)
        ui.MelLabel(parent, label='Target Attribute', align='center' )
        self.targetField = ui.MelTextField(parent)
        ui.MelButton(parent, l='Target from Channel Box', command=self.target_channel_box)
        #--------------------------------------------
        ui.MelSeparator(parent, height=20, style='in')        
        ui.MelButton(parent, l='Connect', command=self.connect_button)
        ui.MelButton(parent, l='Disconnect', command=self.disconnect_button)

    def toggle_translate(self, *args):
        if DEBUG:
            print "translate toggle"
        self.txBox.setValue(1-(self.txBox.getValue()))
        self.tyBox.setValue(1-(self.tyBox.getValue()))
        self.tzBox.setValue(1-(self.tzBox.getValue()))
        
    def toggle_rotate(self, *args):
        if DEBUG:
            print "rotate toggle"
        self.rxBox.setValue(1-(self.rxBox.getValue()))
        self.ryBox.setValue(1-(self.ryBox.getValue()))
        self.rzBox.setValue(1-(self.rzBox.getValue()))
        
    def toggle_scale(self, *args):
        if DEBUG:
            print "scale toggle"
        self.sxBox.setValue(1-(self.sxBox.getValue()))
        self.syBox.setValue(1-(self.syBox.getValue()))
        self.szBox.setValue(1-(self.szBox.getValue()))
        
        
    def connect(self, sourceAttr, targetAttr):
        if DEBUG:
            print "source: "+ sourceAttr + ", target: " + targetAttr
        if mc.isConnected (sourceAttr, targetAttr, iuc=1) == False:
            mc.connectAttr (sourceAttr, targetAttr, f=1)
        else:
            if DEBUG:
                print sourceAttr + " and " + targetAttr + " already connected"
    
    def disconnect(self, sourceAttr, targetAttr):
        if DEBUG:
            print "source: "+ sourceAttr + ", target: " + targetAttr
        if mc.isConnected (sourceAttr, targetAttr, iuc=1) == True:
            mc.disconnectAttr (sourceAttr, targetAttr)
        else:
            if DEBUG:
                print sourceAttr + " and " + targetAttr + " already disconnected"
            
    def connect_button(self, *args):
        targetObjs = mc.ls (sl=1, fl=1)
        src = targetObjs.pop(0)
        sourceField = self.sourceField.getValue()
        targetFieldList = self.targetField.getValue()
        wsTargetField = targetFieldList.split(",")
        for targetField in wsTargetField:
            targetField.strip()  
            
            #connect checked boxes
            for cbox in self.cboxes:
                attribute = str(cbox.getAnnotation())
                if cbox.getValue() == 1:
                    if DEBUG:
                        print cbox+" is checked"
                    for targetObj in targetObjs:
                        self.connect(src + attribute, targetObj + attribute)
            #connect field Boxes unless they are both empty
            if len(sourceField + targetField) == 0:
                if DEBUG:
                    print "fields are empty."
            elif len(sourceField) == 0:
                if DEBUG:
                    print "source field is empty."
                self.sourceField.setValue(targetField)
                for targetObj in targetObjs:
                        self.connect (src + "." + targetField, targetObj + "." + targetField)
            elif len(targetField) == 0:
                if DEBUG:
                    print "target field is empty."
                self.targetField.setValue(sourceField)
                for targetObj in targetObjs:
                        self.connect(src + "." + sourceField, targetObj + "." + sourceField)
            else:    
                if DEBUG:
                    print "Connect " + sourceField + " to " + targetField
                for targetObj in targetObjs:
                    self.connect(src + "." + sourceField, targetObj + "." + targetField)

    def disconnect_button(self, *args):
        targetObjs = mc.ls (sl=1, fl=1)
        src = targetObjs.pop(0)
        sourceField = self.sourceField.getValue()
        targetFieldList = self.targetField.getValue()
        wsTargetField = targetFieldList.split(",")
        for targetField in wsTargetField:
            targetField.strip()  

            #connect checked boxes
            for cbox in self.cboxes:
                attribute = str(cbox.getAnnotation())
                if cbox.getValue() == 1:
                    if DEBUG:
                        print cbox+" is checked"
                    for targetObj in targetObjs:
                        self.disconnect (src + attribute, targetObj + attribute)
            #connect field Boxes unless they are both empty
            if len(sourceField + targetField) == 0:
                if DEBUG:
                    print "fields are empty."
            elif len(sourceField) == 0:
                if DEBUG:
                    print "source field is empty."
                self.sourceField.setValue(targetField[0])
                for targetObj in targetObjs:
                    self.disconnect(src + "." + targetField[0], targetObj + "." + targetField)
            elif len(targetField) == 0:
                if DEBUG:
                    print "target field is empty."
                self.targetField.setValue(sourceField)
                for targetObj in targetObjs:
                    self.disconnect(src + "." + sourceField, targetObj + "." + sourceField)
            else:    
                if DEBUG:
                    print "Connect " + sourceField + " to " + targetField
                for targetObj in targetObjs:
                    self.disconnect(src + "." + sourceField, targetObj + "." + targetField)

    def source_channel_box(self, *args):
        selectedChannelBoxAttr = mc.channelBox ("mainChannelBox", q=1, sma=1)
        self.sourceField.setValue (selectedChannelBoxAttr[0])
        
    def target_channel_box(self, *args):
        selectedChannelBoxAttr = mc.channelBox ("mainChannelBox", q=1, sma=1)
        self.targetField.setValue (selectedChannelBoxAttr[0])

Advertisements