When you want to create a Maya UI, every modern Maya tutorial will chastise you if you don’t put that UI in a class. It makes sense… that way once you’ve initialized the UI class, you can access all of its methods and variables. It’s cleaner that way. You can also use a Python property to set, get, and delete variables if you need to access them from outside the script. I really wish I had known that last year… *
Here, you can gather any 2 lists you like to feed into the UI, and then throw them back and forth in the UI window as you please. The other functions connect or disconnect maps in a very specific way, so I’ll leave them out so as not to reveal any company secrets :).
I realize there is some redundancy here– I’ll come back and try using a decorator to consolidate the doConnect and doDisconnect functions!
*(I had to put together a huge info-gathering UI with fields and file dialogs and radio buttons, and then use all the info to perform specific actions involving that data. I did NOT know about using classes for this, so it’s one veeeery long Python script. While I did realize that using the UI command callbacks all over the place was going to be a pain in the ass or impossible due to scope, I didn’t have an elegant solution. So, I created a blank dictionary and bunch of functions that were called when certain buttons were pressed. I used partial in the buttons’ commands flag to pass the dictionary to the respective functions, got the data from the functions and stored it in that one big dictionary. Then I could look up the values in said dictionary when I needed them later on. It definitely worked, but… not pretty. It’s still being used, but one day if I get the chance I’d like to revamp it. I might make a post about that later. I’ll call it “Don’t make the same mistakes I did!” I might need to make that a tag :/ Anyway, that’s in the past– Onwards! Upwards!)
import maya.cmds as cmds
class connectListUI(object):
def __init__(self, leftList, rightList):
self.leftList = leftList
self.rightList = rightList
self.winTitle = "Texture Connect"
#check to see if our window exists
if cmds.window(self.winTitle, exists = True):
cmds.deleteUI(self.winTitle)
self.winName = cmds.window(title = self.winTitle)
cmds.rowColumnLayout(numberOfColumns = 2, columnWidth = [(1, 300), (2, 300), (3, 300)],
annotation = "Is this thing on?")
cmds.text(label = 'Regular Maps - Disconnected', align = 'left', ebg = True, bgc = [0.4, 0.4, 0.4], font = 'obliqueLabelFont')
cmds.text(label = 'Regular Maps - Connected', align = 'right', ebg = True, bgc = [0.4, 0.4, 0.4], font = 'obliqueLabelFont')
cmds.separator( height=5, style='in' )
cmds.separator( height=5, style='in' )
self.regConnLST = cmds.textScrollList("tsRegConnList", numberOfRows=10, allowMultiSelection=True, height = 180,
append=self.leftList, showIndexedItem=1 )
self.regDisconnLST = cmds.textScrollList("tsRegDisconnList", numberOfRows=10, allowMultiSelection=True, height = 180,
append = self.rightList, showIndexedItem = 1)
cmds.separator( height=15, style='none' )
cmds.separator( height=15, style='none' )
self.connectBTN = cmds.button(label = "CONNECT >>", command = self.doConnect)
self.disconnectBTN = cmds.button(label = "<< DISCONNECT", command = self.doDisconnect)
cmds.separator( height=20, style='in' )
cmds.separator( height=20, style='in' )
cmds.showWindow(self.winName)
def doConnect(self, _):
print "Connecting..."
self.checkList = cmds.textScrollList("tsRegConnList", query = True, selectItem = True)
#if checklist is populated, do stuff. If not, that means the user has not made any selections. Tell them.
if self.checkList != None:
#grab the OTHER list, to see what's in it
self.unsortedC = cmds.textScrollList("tsRegDisconnList", query = True, allItems = True)
#if that list isn't empty
if self.unsortedC != None:
print "List is not empty, adding to it..."
print self.unsortedC
#smash the 2 lists together (yay Python!), then sort them
self.sortedCList = self.unsortedC + self.checkList
self.sortedCList.sort()
print "Combined Sorted List: "
print self.sortedCList
#remove everything from the disconnected list in the UI
self.currentDisconList = cmds.textScrollList("tsRegDisconnList", edit = True, removeAll = True)
#now repopulate it with the sorted list
for eachItem in self.sortedCList:
print "\tEach item: " + eachItem
cmds.textScrollList("tsRegDisconnList", edit = True, append = eachItem)
for eachDisconnItem in self.checkList:
print "\tEach Disconnected Item to remove: " + eachDisconnItem
cmds.textScrollList("tsRegConnList", edit = True, removeItem = eachDisconnItem)
else:
self.checkList.sort()
print "Destination List is empty..."
for eachItem in self.checkList:
print "\tAdd : " + eachItem
cmds.textScrollList("tsRegConnList", edit = True, removeItem = eachItem)
cmds.textScrollList("tsRegDisconnList", edit = True, append = eachItem)
#now take the initial selected list and pass it to whatever function
print "Sending list to Connect function!"
self.connectMaps(self.checkList)
else:
print "You haven't selected anything! Select something!"
def doDisconnect(self, _):
print "Disconnecting..."
self.checkDisList = cmds.textScrollList("tsRegDisconnList", query = True, selectItem = True)
#if checklist is populated, do something. If not, that means the user has not made any selections. Tell them.
if self.checkDisList != None:
#grab the other list
self.unsortedD = cmds.textScrollList("tsRegConnList", query = True, allItems = True)
if self.unsortedD != None:
print "List is not empty, adding to it.."
print self.unsortedD
#smash the 2 lists together (yay Python!), then sort them
self.sortedList = self.unsortedD + self.checkDisList
self.sortedList.sort()
print "Combined Sorted List: "
print self.sortedList
#remove everything from the connected list in the UI
self.currentConList = cmds.textScrollList("tsRegConnList", edit = True, removeAll = True)
#now repopulate it with the sorted list
for eachDisItem in self.sortedList:
print "\tEach item: " + eachDisItem
#cmds.textScrollList("tsRegDisconnList", edit = True, removeItem = eachDisItem)
cmds.textScrollList("tsRegConnList", edit = True, append = eachDisItem)
for eachConnItem in self.checkDisList:
print "\tEach Connected item to remove: " + eachConnItem
cmds.textScrollList("tsRegDisconnList", edit = True, removeItem = eachConnItem)
else:
self.checkDisList.sort()
print "Destination list is empty..."
for eachDItem in self.checkDisList:
print "\tAdd : " + eachDItem
cmds.textScrollList("tsRegDisconnList", edit = True, removeItem = eachDItem)
cmds.textScrollList("tsRegConnList", edit = True, append = eachDItem)
#now take the initial selected list and pass it to whatever function
print "Sending list to disconnect function!"
self.disconnectMaps(self.checkList)
else:
print "You haven't selected anything! Select something!"
#you'd write your connect (or whatever) functionality here
def connectMaps(self, _):
print "I reach here to connect!"
print self.checkList
#you'd write your disconnect (or whatever) functionality here
def disconnectMaps(self, _):
print "I reach here to disconnect!"
print self.checkDisList
#just some test lists
toConnect = ["bump", "specColor", "opacity", "bump_var1", "specColor90_var2"]
alreadyConnected = ["reflective", "incandescence"]
basicMaps = ["incandescence", "specColor", "opacity", "bump"]
#here you would pass 'connectListUI' 2 lists to initially populate the UI window
def doUI():
testUI = connectListUI(toConnect, alreadyConnected)
doUI()