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)

2 comments:

  1. the right way to say "if type(inds) is not list:" is "if not isinstance(inds,list):".

    ReplyDelete
  2. Ah I see! I will change it. Actually I will have to make this change in many places :)
    Thanks!

    ReplyDelete