Arbitrary "User data" attributes in List COMPs

It would be useful to be able to associate python objects with particular cells in List COMPs and then be able to reference them later. I’ve tried setting arbitrary fields in ListAttributes objects but it throws an exception saying that the field doesn’t exist.

For example, a component that has a list of Thing objects and is arranging them in a grid in a List COMP. To handle a click on cell, it will have to do some calculations to determine which Thing is associated with that cell:

def onRollover(listcomp, row, col, ...):
  if layout == 'horz':
    i = ...
  else:
    i = ...
  thing = ext.ThingManager.things[i]
  thing.dostuff()

If the ListAttributes class supported a data field, you could just do:

def prepareTheThings():
  if horizontal:
     for col in range(..):
       for row in range(..):
         thing = Thing(..)
         listcomp.cellAttribs[x, y] = thing
  else:
    ...

def onRollover(listcomp, row, col, ...):
  listcomp.cellAttribs[row, col].data.dostuff()

+1

Could you maintain an set of objects in the components storage, and reference those instead?

Yeah, that’s the alternative. The problem is when there’s a bit of complexity in figuring out which object corresponds to a particular cell. For example, I have a toolbar component that supports different layout orderings and does the whole thing in a single List COMP, so figuring out which toolbar item corresponds to cell (x,y) requires looking at the layout parameters and doing some calculations. It’s not a huge issue, but it would be a nice shortcut to have.

If you’re interested, the component is the “command_panel” one here: [url]https://github.com/optexture/td-components[/url]

But in this case, could you not use the row and col values to access the storage location directly? You can store a dictionary, matrix of values, etc.

Yeah. That would work.
I like the idea of using a dict with (x,y) tuple keys. Lookup should be cheap (O(1)) and you don’t have to deal with managing lists of lists.