Addon - function when addon is started?

Hi,

I tried different handlers, timers, schedules, nothing works

python does not seam to have serious threaded timer functions and bpy handlers never get called

how can I parse all blender scene objects when my addon loads ?

thanks

Add-ons aren’t “started” at any point - they load as part of Blender’s load on new or existing files. Your best option will be the load_post handler

tried that, it never triggers (as I load a blend file)

from bpy.app.handlers import persistent
@persistent
def load_handler(dummy):
    <do whatever>

bpy.app.handlers.load_post.append(load_handler)

It only triggers if you append your function to the existing load_post handers :slight_smile:

yes, never worked for me, my function is never called, hence this post
(yes I did not miss the persistent annotation either)

I put that code in init.py, should I put it somewhere else ?

Interesting :thinking: I don’t mean to invalidate your experience at all, but I copied and pasted that code from an add-on I’ve written and it works perfectly there. It must be because you’re using a multi file addon. I would say you should put it in whichever file has your addon info- the dict with the name, version, etc. failing that, I’d put in it whichever file is calling your operators

yes !!
thanks it works, I have added your code to the panel class file

IMO the best place to register handlers is the addons register function:

# In file __init__.py
@persistent
def my_load_post(arg1, arg2):
    print(".blend file finished loading")

def register():
    bpy.app.handlers.load_post.append(my_load_post)

def unregister():
    bpy.app.handlers.load_post.remove(my_load_post)
1 Like

yep, tried that, but I register in init.py and it’s never called apparently

as far as I understand, registered classes get loaded, but not init.py which is just there for installation

You are correct, init.py can be entirely empty and still work fine

Sounds like something might be wrong with how your files are structured then.

Your __init__.py should be in the main folder of your addon, and contain things like the bl_info dict and the register/unregister functions. Blender will then call the register functions automatically when the addon is enabled/disabled or on startup/quit of Blender.

See for example: https://docs.blender.org/manual/en/latest/advanced/scripting/addon_tutorial.html#what-is-an-add-on

1 Like