blender_vscode icon indicating copy to clipboard operation
blender_vscode copied to clipboard

Reloading imported modules in a script

Open ChristopherPAndrews opened this issue 1 year ago • 1 comments

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?

ChristopherPAndrews avatar Mar 13 '24 15:03 ChristopherPAndrews

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)

macduff111 avatar Mar 17 '24 22:03 macduff111

Agreed. If you are not using blender register functions you need to reload them manually. This is exactly what registering does under the hood.

Mateusz-Grzelinski avatar Jul 22 '24 19:07 Mateusz-Grzelinski

@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)

Mateusz-Grzelinski avatar Jul 30 '24 16:07 Mateusz-Grzelinski