blender_vscode
blender_vscode copied to clipboard
Reloading imported modules in a script
I am working on a multi-file project I am running as a script. Everything works well when I make changes in the main file. I edit the file, run Blender:Run Script and it runs with no problems. However, if I make changes in any of the imported files they aren't seen until I restart Blender. Is there something I can do to force a reload of the imported code?
I use this in my scripts which imports first time it is run and reloads on subsequent runs
from importlib import reload
import blah
reload(blah)
Agreed. If you are not using blender register functions you need to reload them manually. This is exactly what registering does under the hood.
@ChristopherPAndrews btw this is simplified code how we in this addon reload modules. it is not always reliable though. After you delete module you can import it again. but importlib is much better solution though.
bpy.ops.preferences.addon_disable(module=module_name)
for name in list(sys.modules.keys()):
if name == module_name or name.startswith(module_name + "."):
del sys.modules[name]
bpy.ops.preferences.addon_enable(module=module_name)