20 March, 2011

Removing multiple items from textScrollList

I have started working more with Maya UI and I can see why people get frustrated sometimes with handling UI interaction using mel. For example, there is no function to get an item at a given index.

Deletion of multiple items from a list or an array is always tricky because, while deleting multiple items the indices are updated automatically. One solution is to start deleting at the highest index so that the indices to be deleted later are always unaffected.

Here is the function I wrote for the extended textScrollList class. Python's built-in function to sort a list comes in handy here.

def removeAt(self, inds):
"""
Remove item(s) at given indices from textScrollList
@inds: takes a single int or a list of int
"""
if not isinstance(inds,list):
inds = [inds]
# indices should be sorted first into descending order
# so that the indices to be deleted next are unaffected
inds.sort(reverse=True)
for i in inds:
self.edit(removeIndexedItem=i)