File watcher addon for Blender. Edit files in an external editor, save the files and see results instantly inside Blender.
-
Install Python module
watchfiles
- on MacOS:
/Applications/Blender.app/Contents/Resources/3.3/python/bin/python3.10 -m pip install watchfiles
- TODO: instructions for other OSs
- TODO: include the pip install inside the addon itself
- on MacOS:
-
Download
external_live_edit.py
-
Add it as addon in Blender
-
Execute "Watch And Run" operator
- in the
Scripting
workspace -> in theText
menu -> click onWatch and Run
menu item - via Search (
F3
shortcut), typeWatch and Run
- in the
-
A file path selector will appear
-
Choose the file with the Python module you want to run. If it has a
main
function it will be called after the module is loaded.
The module will then be reloaded and reexecuted every time any file changes in the file's parent folder (or any of its subfolders).
To disable the watching run the Stop Watching
operator, which you will find in the Text
menu or via Search.
You can use a folder with multiple modules to split your script.
Folder structure:
/script/
├── foo.py
└── another.py
Choose /script/foo.py
which contains:
import another
If you want nested folders for more structure, or if you want to avoid shadowing builtin modules, you can use a package.
/script/
├── __init__.py
├── foo.py
└── another/
├── __init__.py
└── bar.py
And in /script/foo.py
:
import script.another.bar
# Or using relative paths
from .another import bar
- Only modules declared in files inside the the parent folder of the chosen file will be reloaded. Any other modules will not be reloaded. You will have to restart Blender to update them.
Contributions are welcome for any issue or to implement the TODO
s called out above.
There are a few tricks to get this functionality working correctly and efficiently:
watchfiles
is used in a background thread to watch for file changes- The
Watch and Run
operator is a modal operator which gets run every 50ms and checks anasyncio.Event
for whether the chosen module should be run - Running the module entails making sure its directory is in
sys.path
and removing all the modules from the directory so that they get rerun correctly - The chosen path is written to a Blender
Text
so that it is saved in the.blend
file and watch can be automatically restarted when you open the.blend
file - The background thread must be killed before exiting Blender otherwise Blender never quits