I'm trying to create an expression binding where compares the text in '.label' from an array equals a certain string it changes the color of the object binding.
It works fine if I define an array number, but I want to dynamically apply it to all events in the array as items are deleted and added to the array structure.
def transform(self, value, quality, timestamp):
# Transform script for property binding. Assume "value" is the incoming array of event‐objects.
events = value # e.g. [{ "itemId":1, "eventId":16, "label":"TEST", ... }, {...}, ...]
# We'll produce a new list, so that each event gets a "color" attribute based on its label.
out = []
for e in events:
# Make a shallow copy so we do not overwrite the original
newEvent = dict(e)
if newEvent.get('label') == "TEST":
newEvent['backgroundColor'] = "#6ECEB2"
else:
# either leave it blank or give it a default color
newEvent['backgroundColor'] = "#FFBE9F"
out.append(newEvent)
return out
Likely unimportant performance consideration: You don't need to worry about making a copy of the original list, you can edit the original without concern. Nothing else is touching or using the original list.
Depending on the source of the data, the e might be immutable, in which case he does need to build a new dict to be able to add the backgroundColor key.
Consider, having the color map as a separate custom property, and reference that from the script transform. Much more visible and you don't have to edit the script transform to make small tweaks or add items
Yes that's not a bad idea.
Could be used to make the color map dynamic.
It also allows other properties to use the same colors.
But then he should use a structure binding, to bind on both the original data and the color map.
If it doesn't change much, and it's only used by that transform, I'd stick to a simple color map in the script.