I'm working on an add-on for Blender, and I'm trying to assign a custom update function to a list of properties through an "update" attribute. The update function will only accept 2 parameters (self and context, or s and c). But I want to send a third parameter that identifies which property is being updated. This parameter is an index. This is what I started with:
for i in range(0,len(props)):
props[i].update = lambda s, c: CustomUpdate( s, c, i )
But I soon realized that lambda records the variable being used, rather than the value of that variable. So all of the property update functions end up being generated like this:
for i in range(0,len(props)):
props[i].update = CustomUpdate( s, c, len(props) - 1 )
I looked for answers and found this solution:
for i in range(0,len(props)):
props[i].update = lambda s, c, index=i: CustomUpdate( s, c, index )
However, Blender appears to be double checking the number of parameters for the function supplied here, and throws an error when more than 2 are used, so I can't use a third lambda parameter.
So I'm currently trying to figure out how to convince the lambda to generate a unique index for each property for the callback. Possibly some way to edit the lambda after assigning it? Or any trick to wrap i
in some code to force evaluation during parsing instead of during execution?
Edit: Forgot to mention that my list of props is static. So the loop index counter could be unrolled by the parser, if such a thing is possible.