opexists vs. op().valid

Admittedly I’m a python freshman, and I apologise in advance for what must be a very silly question, but I don’t understand the .valid member of the op class.

I’m looking for a python equivalent of opexists(’/nonexistentop’)

the doc says that the op class has a .valid attribute that should say if the operator exists or it has been deleted (it doesn’t say “it does not exist” not sure if there’s a difference there…)

and in fact op(’/’).valid is True

but, as I expected, op(’/nonexistent’).valid generates an error, instead of returning False.

so, what can I use in python to see if an operator exists before deciding whether or not to act on it? And what’s the use of .valid since it’s either True or else it generates an error?

thanks!
dani

You could handle the exception? Something like this?

try:
    z = op('/nonexistent').valid
except:
    print("z doesn't exist")

https://wiki.python.org/moin/HandlingExceptions

I’ll also use if statements a lot of for testing if OPs or cells(or rows) or channels exist.

if op('someOP'):

or

if op('table')['cellName',0]:

Right, so to summarize:

op('somepath')   #either returns an OP object, or None.

If you currently have an OP object, OP.valid will return whether its still there or not.

So, easiest method to first test if something exists or not is:

if op('somepath'):
    op('somepath').someaction...

As Keith mentioned, you can wrap this up with exceptions instead:

try:
    op('somepath').someaction...
except:
    pass

Secondly, if you have that OP as an object already in your script (or in storage) but you think the original operator it referes to, may have been deleted, you can quickly check with its .valid member (or add more try/accept pairs in your code).

Cheers,
Rob.

Thanks to all! I had used
if op(‘someop’):

but forgot about it (not used enough to embed it in my brain yet :slight_smile:).
on the other hand, the
try:

except:

form is interesting and it the try: statement which I had just so far left untested from my end.

Thanks again!
dani

I use try and except because in the node.js world such things are wonderful so it kind of just translated over :stuck_out_tongue:

Is there a way to do this with global shortcuts in any meaningful way? lets say i have ‘constantCHOP1’ and ‘base1’, and ‘base1’ has TEST as a global shortcut. The following expression works while base1 exists, but errors out because of the shortcut if it is deleted:

1 if op.TEST else 0

Hey @drmbt

is 'TEST' in op what you are looking for ?

I basically want to do if op(‘whatever’) 1 else 0, or if op(‘whatever’).valid etc. but if i try to do this with a global shortcut, it errors if invalid instead of providing a 0 because the expression it is testing for (global shortcut) doesn’t exist. Simply looking for an inline way to test for an op in the project that doesn’t have an absolute path, such as referring to a global op.SETTINGS if it is in the project, but assuming a default value if such an op with a global shortcut does not exist

hoping that maybe the .valid member could somehow return false if a global shortcut isn’t valid instead of erroring out the node

Try

'TEST' in op

And it will do just that, return True when the global shortcut is in the project and False when it’s not.

TEST in op.toe (3.5 KB)

Ok, that seems to be what i want, but it doesn’t actually work because it isn’t dependable… as seen in this example, cutting and pasting either of the example COMPs does not update in the desired locations, defeating the purpose

Ok, looks like I got what i wanted going with op.TEST.par.Pulse if hasattr(op, ‘TEST’) else 0, that seems to update throughout my session without forcing a cook, and doesn’t require op.TEST to exist, avoids an error!

1 Like