Getting the animated object in `custom_exec_cb`


I try to use a custom_exec_cb like below and it works well:

  a1.set_custom_exec_cb(lambda a, v: btn1.set_x(v))

But the goal would be to get the animated object from a in the lambda function. I’ve tried this

  a1.set_custom_exec_cb(lambda a, v: lv.obj_t(a.var).set_x(v))

and got:

AttributeError: no such attribute

So could you help me with how to cast a.var to lv.obj_t?

lv_anim_set_custom_exec_cb is defined like this:

static inline void lv_anim_set_custom_exec_cb(lv_anim_t * a, lv_anim_custom_exec_cb_t exec_cb)
    a->var     = a;
    a->exec_cb = (lv_anim_exec_xcb_t)exec_cb;

var is passed as the first argument of exec_cb so you can obtain lv_anim_t from var, but not the btn itself.
This needs to be defined like this in order to register the Micropython callback on user_data, and to access it when the callback is called.

Could you explain why your goal is to obtain btn1 from a? You can always access it from the lambda directly.

Ah, really. I should have figured it out.

That’s correct. Seemingly I live too much n the C world :slight_smile:

In case anyone needs it, here is an example:

def my_anim(target):
  anim = lv.anim_t()
  anim.set_values(0, 100)
  anim.set_custom_exec_cb(lambda a, v: target.set_x(v))


Thank you for your help!