3
$\begingroup$

I need to run a script several times, which among other things adds a custom function to a handler list. How can I check that it doesn't eist already, so as not to have several identical functions in my handlers list, without clearing the list altogether?

This doesn't work.

import bpy

def test(self):
    print('test')

if not test in bpy.app.handlers.render_pre:
    bpy.app.handlers.render_pre.append(test)
$\endgroup$

1 Answer 1

4
$\begingroup$

You can use a list comprehension to check if your function already exists:

def myFunc():
    """Your function"""

if not myFunc.__name__ in [hand.__name__ for hand in bpy.app.handlers.save_pre]:
    bpy.app.handlers.save_pre.append(myFunc)

It'll get the function's name from the pointer for each function in your handler list. Then, you can compare it to your function's name.

[hand.__name__ for hand in bpy.app.handlers.save_pre]

It's equivalent to:

handlerList = []
for handler in bpy.app.handlers.save_pre:
    handlerList.append(handler.__name__)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .