I’d trying to come up with something like a context manager that can branch based on input.
For example, say I am in Maya and I would like to early out if the selection is None. Normally I would do something like:
sel = cmds.ls(sl=True)
if not sel:
cmds.warning('Nothing Selected!')
else:
myFunc(sel)
If I wanted to personalize the message I would do something like this:
def myFunc(sel):
if not sel:
cmds.warning('myFunc requires at least 1 object selected.')
return
# stuff and things.
I’d like some kind of wrapper that could execute a block of code only if the input passes a test.
Maybe something like:
with somethingSelected(cmds.ls(sl=True)) as sel:
myFunc(sel)
where myFunc would not be executed if the current selection is None(it would also handle printing the warning).
I tried this with a context manager, but they always run the code they wrap. After reading up on context managers, they aren’t designed to handle conditions.
I came across the conditional package, which seems like it is designed specifically to address the same problem, but I’d like to use a home-grown solution.
Any ideas? Maybe a decorator with it’s own arguments would be a better choice?