简体   繁体   中英

Blender Python - Force reload of module while importing all its classes

I am developing in Python/Blender, and have two needs here:

  1. Import all the individual classes from my module (because they must each be registered with blender)
  2. Reload the module itself each time the script is executed (to prevent caching while I'm developing the plugin and press "reload scripts")

Currently I am doing this (in __init__.py ):

from importlib import reload
from .MyPlugin import *

reload(MyPlugin)

classes = [ClassA, ClassB, ClassC]
# register each class, not shown here

However the reload(MyPlugin) line causes an error: "MyPlugin is not defined".

Originally I tried reloading each of the classes instead, but it raised an error that reload expects a module.

Some colleagues helped me with an answer, what ended up working was this in __init__.py :

from importlib import reload

if "MyModule" in locals():
    reload(MyModule)
else:
   import MyModule

from .MyModule import *

This is detailed here: https://blenderartists.org/t/how-to-reload-add-on-code/1202715/2

this works with older Version (tested with 2.79) Restart first!

del sys.modules['foo']    
from foo import *

Sorry, the answers are not clear to me. I have some scripts I like to edit externally. Say script1, script2, ... With

import script1, script2, ...

those scripts are working well but after editing they should be reloaded. What is the code to do this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM