cmcPasserby
cmcPasserby

Reputation: 51

Set attribute in callback, with lambda

Trying to figure out how to set a object attribute using a lambda function.

i got a case where i need to set the attribute with a UI callBack, that requires a function object.

was trying something like this but it dosnt work, and gives me a syntax error.

pm.button(command=lambda *args: uvOpts.grid = True)

Upvotes: 1

Views: 1544

Answers (1)

furins
furins

Reputation: 5048

this solves the syntax error (you cannot have a second "=" in pm.button) and works if uvOpts is in the scope.

pm.button(command=lambda *args: setattr(uvOpts, 'grid', True))

to set the object's attribute I'm using setattr, in order to avoid the use of a = symbol in lambda function definition.


I would like to add that if you need to make uvOpts visible inside the lambda scope you may eventually need to make it global, at least at module level (e.g. read the answers in Using global variables in a function other than the one that created them).

Upvotes: 3

Related Questions