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:
-
- # Check if this add-on is being reloaded
- if "bpy" in locals():
- # reloading .py files
- import importlib
-
- from . import nuu_menu_panel
- importlib.reload(nuu_menu_panel)
- from . import nuu_menu_open_op
- importlib.reload(nuu_menu_open_op)
- # or if this is the first load of this add-on
- else:
- import bpy
- from . import nuu_menu_panel
- from . import nuu_menu_open_op
-
- bl_info = {
- "name": "Nuu Test",
- "author": "Luna",
- "version": (0, 0, 1),
- "blender": (2, 80, 0),
- "category": "Generic",
- "location": "View3D",
- }
-
- classes = (nuu_menu_panel.NUU_PT_menuPanel, nuu_menu_open_op.NUU_OT_menuOpenOperator)
-
- register, unregister = bpy.utils.register_classes_factory(classes)
-
- # This allows you to run the script directly from Blender's Text editor
- # to test the add-on without having to install it.
- if __name__ == "__main__":
- 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