Default Value UI


'''
default_value_ui.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 DefaultValueWindow(ui.BaseMelWindow):
    WINDOW_NAME = 'dv_win'
    WINDOW_TITLE = 'Default Value UI'
    DEFAULT_SIZE =220, 250
    DEFAULT_MENU = None
    
    
    def __init__(self, *args):
        lyt1 = ui.MelColumnLayout(self, *args)
        
        #Define all ui controls/elements
        self.build_lyt1(lyt1)
        
        self.show()
        self.source_channelbox()

    def build_lyt1 (self, parent):

        ui.MelLabel(parent, label='Attribute:', align='center' )
        self.attributeField = ui.MelTextField(parent)
        ui.MelButton(parent, l='Source from Channel Box', command=self.source_channelbox)
        ui.MelSeparator(parent, height=18, style='in')
        #--------------------------------------------
            
        row1 = ui.MelHLayout( self )        
        ui.MelLabel(row1, label='Minimum Value', align='center' )
        self.minField = ui.MelTextField(row1)
        row1.layout()

        row2 = ui.MelHLayout( self )        
        ui.MelLabel(row2, label='Maximum Value', align='center' )
        self.maxField = ui.MelTextField(row2)
        row2.layout()

        row3 = ui.MelHLayout( self )        
        ui.MelLabel(row3, label='Default Value', align='center' )
        self.defaultField = ui.MelTextField(row3)
        row3.layout()
        
        row4 = ui.MelHLayout ( self )
        ui.MelButton(row4, l='Get Values', command=self.update_button)
        row4.layout()
        
        row5 = ui.MelHLayout ( self )
        ui.MelButton(row5, l='Set Values', command=self.set_values_button)
        row5.layout()

    def update_limit_field(self, minExist, maxExist, minLimit, maxLimit):
            if (minExist) == False:
                self.minField.setValue ("")
            else:
                self.minField.setValue (minLimit)
            if (maxExist) == False:
                self.maxField.setValue ("")
            else:
                self.maxField.setValue (maxLimit)
                
    def update_button(self, *args):
        attrField = self.attributeField.getValue()
        selected = mc.ls (sl=1, fl=1)
        if DEBUG:
            print "Updating UI: "+selected[0]+"."+attrField
        
        #user created attr min/max
        minValueExists = mc.attributeQuery (attrField, node = selected[0], mne=1)
        if minValueExists == 1:
            minValue = mc.attributeQuery (attrField, node = selected[0], min=1)
            self.minField.setValue (minValue[0])
        else:
            self.minField.setValue ("")
        maxValueExists = mc.attributeQuery (attrField, node = selected[0], mxe=1)
        if maxValueExists == 1:
            maxValue = mc.attributeQuery (attrField, node = selected[0], max=1)
            self.maxField.setValue (maxValue[0])
        else:
            self.maxField.setValue ("")
            
        #transformLimit Min/Max        
        if attrField == "tx":
            tLimitExist =  mc.transformLimits (selected[0], etx=1, q=1)
            tLimitList =  mc.transformLimits (selected[0], tx=1, q=1)
            self.update_limit_field (tLimitExist[0], tLimitExist[1], tLimitList[0], tLimitList[1])
        if attrField == "ty":
            tLimitExist =  mc.transformLimits (selected[0], ety=1, q=1)
            tLimitList =  mc.transformLimits (selected[0], ty=1, q=1)
            self.update_limit_field (tLimitExist[0], tLimitExist[1], tLimitList[0], tLimitList[1])
        if attrField == "tz":
            tLimitExist =  mc.transformLimits (selected[0], etz=1, q=1)
            tLimitList =  mc.transformLimits (selected[0], tz=1, q=1)
            self.update_limit_field (tLimitExist[0], tLimitExist[1], tLimitList[0], tLimitList[1])

        if attrField == "rx":
            tLimitExist =  mc.transformLimits (selected[0], erx=1, q=1)
            tLimitList =  mc.transformLimits (selected[0], rx=1, q=1)
            self.update_limit_field (tLimitExist[0], tLimitExist[1], tLimitList[0], tLimitList[1])
        if attrField == "ry":
            tLimitExist =  mc.transformLimits (selected[0], ery=1, q=1)
            tLimitList =  mc.transformLimits (selected[0], ry=1, q=1)
            self.update_limit_field (tLimitExist[0], tLimitExist[1], tLimitList[0], tLimitList[1])
        if attrField == "rz":
            tLimitExist =  mc.transformLimits (selected[0], erz=1, q=1)
            tLimitList =  mc.transformLimits (selected[0], rz=1, q=1)
            self.update_limit_field (tLimitExist[0], tLimitExist[1], tLimitList[0], tLimitList[1])

        if attrField == "sx":
            tLimitExist =  mc.transformLimits (selected[0], esx=1, q=1)
            tLimitList =  mc.transformLimits (selected[0], sx=1, q=1)
            self.update_limit_field (tLimitExist[0], tLimitExist[1], tLimitList[0], tLimitList[1])
        if attrField == "sy":
            tLimitExist =  mc.transformLimits (selected[0], esy=1, q=1)
            tLimitList =  mc.transformLimits (selected[0], sy=1, q=1)
            self.update_limit_field (tLimitExist[0], tLimitExist[1], tLimitList[0], tLimitList[1])
        if attrField == "sz":
            tLimitExist =  mc.transformLimits (selected[0], esz=1, q=1)
            tLimitList =  mc.transformLimits (selected[0], sx=1, q=1)
            self.update_limit_field (tLimitExist[0], tLimitExist[1], tLimitList[0], tLimitList[1])

        #devault Value
        defaultValue = mc.attributeQuery (attrField, node = selected[0], ld=1)
        self.defaultField.setValue (defaultValue[0])
        

    def set_values_button(self, *args):
        attrFieldLongName = self.attributeField.getValue()
        selected = mc.ls (sl=1, fl=1)
        attrFieldList = mc.listAttr (selected[0] + "." + attrFieldLongName, sn=1)
        attrField = attrFieldList[0]
        
        if DEBUG:
            print "Saving values for: "+selected[0]+"."+attrField
        
        
        if attrField in ["tx", "ty", "tz", "rx", "ry", "rz", "sx", "sy", "sz"]:

            minf = self.minField.getValue()
            maxf = self.maxField.getValue()

            if len(minf) == 0:
                if len(maxf) == 0:
                    if DEBUG:
                        print "Empty min/max"
                    kwargs = {'e%s' % str(attrField):(0, 0)}
                else:
                    if DEBUG:
                        print "Empty min, set max " + maxf
                    kwargs = {'%s' % str(attrField):(maxf, maxf),
                              'e%s' % str(attrField):(0, 1)}
            else:
                if len(maxf) == 0:
                    if DEBUG:
                        print "Empty max, set min " + minf
                    kwargs = {'%s' % str(attrField):(minf, minf),
                              'e%s' % str(attrField):(1, 0)}
                else:
                    if DEBUG:
                        print "Set min " + minf + ", set max " + maxf
                    kwargs = {'%s' % str(attrField):(minf, maxf),
                              'e%s' % str(attrField):(1, 1)}

            mc.transformLimits(**kwargs)
            
                
        else:    
            if len(self.minField.getValue()) == 0:
                if DEBUG:
                    print "Empty min field"
                mc.addAttr (selected[0]+"."+ attrField, e=1, hnv = 0)
            else:
                minf = self.minField.getValue()
                if DEBUG:
                    print "Set min: "+ minf
                mc.addAttr (selected[0]+"."+ attrField, e=1, min = float(minf))
            if len(self.maxField.getValue()) == 0:
                if DEBUG:
                    print "Empty max field"
                mc.addAttr (selected[0]+"."+ attrField, e=1, hxv = 0)
            else:
                maxf = self.maxField.getValue()
                print "Set max: "+ maxf
                mc.addAttr (selected[0]+"."+ attrField, e=1, max = float(maxf))
                
            if len(self.defaultField.getValue()) == 0:
                if DEBUG:
                    print "Empty default value field"
            else:
                df = self.defaultField.getValue()
                if DEBUG:
                    print "Set default: "+ df
                mc.addAttr (selected[0]+"."+ attrField, e=1, dv = float(df))
                
            
        #devault Value
        defaultValue = mc.attributeQuery (attrField, node = selected[0], ld=1)
        self.defaultField.setValue (defaultValue[0])
        
        
    def source_channelbox(self, *args):
        selectedChannelBoxAttr = mc.channelBox ("mainChannelBox", q=1, sma=1)
        if selectedChannelBoxAttr == None:
            if DEBUG:
                print "nothing selected"
        else:
            self.attributeField.setValue (selectedChannelBoxAttr[0])
            self.update_button()
Advertisements