I’ve been playing with custom parameters on components set to the ‘Python’ style to get some dynamic callbacks associated stored in the custom parameter. If I type in an expression like:
[op.Mycomp.Firstcallback, op.Mycomp.Secondcallback]
That evaluates to a list of references to functions that are callable from scripts inside the component with something like:
callbacks = parent().par.Callbacks.eval()
kwargs = {
'some_arg': some_arg,
'val': val,
}
for func in callbacks:
if callable(func):
func(**kwargs)
This is pretty great! But it only works if I type in the expression or provide it as a string.
If I want to build the list of callbacks dynamically and then stick it in the Callbacks custom parameter I get errors because when I set Callbacks.par = myList, it is expecting a Python expression. When passed a python list it seems to stringify it with the list’s repr method and I end up with something like the following in the parameter expression:
[<bound method MyClass.MyCallback of </project1/MyComp/Extension.Class object at 0x0000012B1E0400F0>>]
Which of course can’t be evaluated as an expression. It would be nice if there was a way to stick a reference to a python object into a custom parameter without having to evaluate an expression, like if par.val on Python style custom parameters took references and par.expr still handled the expressions?
I can get around it for now by passing a list of strings and looping over them to evaluate them into references to function objects, but it doesn’t feel as clean. I could also use operator storage and I realise I’m being a bit greedy here but again it would be nice to keep everything in the customer parameters paradigm.
Cheers!
Corey