1
$\begingroup$

I would like to run a function once the addon is installed, I don't know if it is possible, but it would be interesting to understand how to run this function only once.

I've tried calling some functions from the register() function but there are things in it that can't be done at that specific time.

It would be nice to run a function like, on_addon_installed_post

$\endgroup$
2
  • 2
    $\begingroup$ as a "cheap" workaround you could - if somebody starts your add-on for the first time, write a file which changes e.g. simple a text from ONE_TIME_CALL_WAITING to ONE_TIME_CALL_DONE. And you can check whether this text file has already that content $\endgroup$
    – Chris
    Commented Dec 20, 2021 at 9:13
  • 1
    $\begingroup$ It is not possible to run a function at install time. (see userpref.py) because there is no install time hook. What is an example of something that can't be done at registration that you want to do? Perhaps there is an alternative to a run-once. $\endgroup$ Commented Dec 25, 2021 at 20:38

1 Answer 1

1
$\begingroup$

There may be other solutions, this could be one of them.

When Blender is opened or addon is activated, the current time is compared with the installation time of the addon. For this we need modules addon_utils and datetime. First is not in API. Look at BlenderDefault/scripts/modules/addon_utils.py

Less than 1 minute is considered new in this example. The disadvantage is that you need to activate the addon for the first call within 1 minute.

bl_info = {
    "name": "first call test",
    "blender": (2, 83, 0),
    "category" : "All"
}

import bpy

def first_time():
    print("\n...check addon")
    import addon_utils   # not in API 
    from datetime import datetime 

    for mod in addon_utils.modules():    
        if mod.bl_info.get('name') == 'first call test':   # get module

            time_mod = mod.__time__                         # get mod_install_time as timestamp
            time_now = datetime.timestamp(datetime.now())   # get actual_time as timestamp
            
            print("Modul:", time_mod, datetime.fromtimestamp(time_mod))
            print("Now:  ", time_now, datetime.fromtimestamp(time_now))

            if (time_now - time_mod) / 60 < 1:              # difference less than 1 min
                print("i am new")
            else:
                print("i am old")

### REGISTRY
classes = ()

def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)
    first_time()  # testcall

def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)
            
if __name__ == "__main__":
    register()
$\endgroup$

You must log in to answer this question.

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