1
$\begingroup$

Is there a way to run a block of code when the user activates the add-on?

I am creating my add-on, in which my add-on appends in a few node groups from other .blend file.

I would like my add-on to directly append the nodes rather than the user having to click some button to do that!

Thanks for help....

$\endgroup$
  • $\begingroup$ do you mean when you register the addon, or when blender start and the addon is enabled ( registered )? $\endgroup$ – Chebhou Mar 31 '16 at 17:00
  • $\begingroup$ Yes, just like that.... $\endgroup$ – Akash Hamirwasia Mar 31 '16 at 17:00
  • $\begingroup$ which one exactly? $\endgroup$ – Chebhou Mar 31 '16 at 17:03
  • $\begingroup$ blender start and addon is enabled $\endgroup$ – Akash Hamirwasia Mar 31 '16 at 17:04
2
$\begingroup$

This is an example addon that will execute the function "load_fonts" when blender starts, it use the handlers to run a function at blender start :

import os
import bpy
from bpy.app.handlers import persistent

@persistent
def load_fonts(scene):
    font_dir = bpy.context.user_preferences.filepaths.font_directory
    for file in os.listdir(font_dir):
        if file.endswith(".ttf"):  
            bpy.data.fonts.load(font_dir+file)

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

def unregister():
    bpy.app.handlers.load_post.remove(load_fonts)

if __name__ == "__main__":
    register()

you can call the function you want inside the register() function, but you can't get access to the scene :

def load_fonts(scene):
    print("addon activated ")

def register():
    load_fonts()


if __name__ == "__main__":
    register()

to overcome this problem you can use handlers to access the scene as @CoDEmanX shows in this thread : example

import os
import bpy
from bpy.app.handlers import persistent

@persistent
def load_fonts(scene):
    bpy.app.handlers.scene_update_pre.remove(load_fonts)
    font_dir = bpy.context.user_preferences.filepaths.font_directory
    for file in os.listdir(font_dir):
        if file.endswith(".ttf"):  
            bpy.data.fonts.load(font_dir+file)

def register():
    bpy.app.handlers.scene_update_pre.append(load_fonts)

def unregister():
    pass

if __name__ == "__main__":
    register()
|improve this answer|||||
$\endgroup$
  • $\begingroup$ This works when the addon is activated by default. I want it to work even if the user activates it after blender has loaded... $\endgroup$ – Akash Hamirwasia Mar 31 '16 at 17:33
  • $\begingroup$ @Always4u try calling the function inside the register() function $\endgroup$ – Chebhou Mar 31 '16 at 17:36
  • $\begingroup$ I did not get that, can you please give an example.. :) $\endgroup$ – Akash Hamirwasia Mar 31 '16 at 17:58
  • $\begingroup$ def register(): load_fonts() but I think you can't access the scene in the register function $\endgroup$ – Chebhou Mar 31 '16 at 18:09
  • $\begingroup$ my function needs at least 1 argument, and I don't know how to call it through the register() function! $\endgroup$ – Akash Hamirwasia Mar 31 '16 at 18:23
0
$\begingroup$

This might help explain the source of the problem. https://wiki.blender.org/index.php/Extensions:2.6/Py/API_Changes#Restricted_Context

I think we are left with checking every update cycle, so hundreds of times a second via scene_update_pre handler?! super ugly, super wasteful, something is very wrong with this convoluted situation. I, We or They probably need to rethink from some other examples something totally different here.

bpy.app.handlers.scene_update_pre.append(initsetup_operators_needing_scene_function)
|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.