简体   繁体   English

Blender Python - 在导入所有类时强制重新加载模块

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

I am developing in Python/Blender, and have two needs here:我在 Python/Blender 中开发,这里有两个需求:

  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 ):目前我正在这样做(在__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".但是reload(MyPlugin)行会导致错误:“MyPlugin is not defined”。

Originally I tried reloading each of the classes instead, but it raised an error that reload expects a module.最初我尝试重新加载每个类,但它引发了一个错误, reload需要一个模块。

Some colleagues helped me with an answer, what ended up working was this in __init__.py :一些同事帮我回答了,最终在__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这在这里有详细说明: https : //blenderartists.org/t/how-to-reload-add-on-code/1202715/2

this works with older Version (tested with 2.79) Restart first!这适用于旧版本(使用 2.79 测试)首先重新启动!

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说 script1, script2, ...

import script1, script2, ...

those scripts are working well but after editing they should be reloaded.这些脚本运行良好,但编辑后应重新加载。 What is the code to do this?执行此操作的代码是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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