Reload modified 2.80 Blender Add-on

I started working on my first Blender add-on for 2.80 and quickly wondered: how do I reload my blender add-on without restarting Blender!? After spending roughly a day researching on how to reload I was able to just hit a key (F8) inside Blender to reload the add-on.

After searching and testing on different approaches for reloading I came across this post: https://developer.blender.org/T67387 . I had gone through many of their steps in trying to figure out how to reload the add-ons :).

The solution:

Two steps are required to easily reload the add-on.
One is setting up the reload key binding because reload scripts action is unbound.
The other one is writing the proper reload code in the __init__.py file.

Reload keybinding (F8)

Adding the new key binding wasn't very intuitive but these are the steps to add the script.reload keybinding: Edit -> Preferences -> Keymap -> Expand Screen -> Expand Screen (Global) -> Add New -> Expand the new action (none) -> set action as "script.reload" -> set key binding. -> You're done!! I set the binding to F8 because that's what many other solutions for 2.79 etc mentioned.

__init__.py

This was the difficult part because many of the things I tried like just using importlib to reload the modules didn't work. But that post from blender forums did something I hadn't seen before in any other post. It used an if statement if bpy existed in locals. Here is how the code looked like for my test add-on when I solved the reload problem:

  1.  
  2. # Check if this add-on is being reloaded
  3. if "bpy" in locals():
  4. # reloading .py files
  5. import importlib
  6.  
  7. from . import nuu_menu_panel
  8. importlib.reload(nuu_menu_panel)
  9. from . import nuu_menu_open_op
  10. importlib.reload(nuu_menu_open_op)
  11. # or if this is the first load of this add-on
  12. else:
  13. import bpy
  14. from . import nuu_menu_panel
  15. from . import nuu_menu_open_op
  16.  
  17. bl_info = {
  18. "name": "Nuu Test",
  19. "author": "Luna",
  20. "version": (0, 0, 1),
  21. "blender": (2, 80, 0),
  22. "category": "Generic",
  23. "location": "View3D",
  24. }
  25.  
  26. classes = (nuu_menu_panel.NUU_PT_menuPanel, nuu_menu_open_op.NUU_OT_menuOpenOperator)
  27.  
  28. register, unregister = bpy.utils.register_classes_factory(classes)
  29.  
  30. # This allows you to run the script directly from Blender's Text editor
  31. # to test the add-on without having to install it.
  32. if __name__ == "__main__":
  33. register()

After doing these two things you should be able to just reload the add-on with the click of a button.

More reading

Other Blender Add-on related blog posts by me that may be of interest:

No comments:

Post a Comment