-
Notifications
You must be signed in to change notification settings - Fork 55
/
watch.py
129 lines (101 loc) · 3.5 KB
/
watch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding: utf-8 -*-
# This script is Free software. Please share and reuse.
# ♡2022 Adam Dominec <[email protected]>
# This script reloads your addon every time you save a file.
# It starts when this addon is enabled, so once you specify the script name to be watched,
# your script will be watched for updates even after opening a new blend file.
bl_info = {
"name": "Watch Reload",
"author": "Addam Dominec",
"version": (0, 1),
"blender": (3, 0, 0),
"location": "",
"warning": "",
"description": "Watch for script update and reload automatically",
"doc_url": "",
"category": "Development",
}
import bpy
import sys
import importlib
import types
from dataclasses import dataclass
from pathlib import Path
@dataclass
class Pair:
module: types.ModuleType
stamp: int
def __repr__(self):
return self.module.__name__
__str__ = __repr__
def timestamp(filename):
return Path(filename).stat().st_mtime
def touch(filename):
Path(filename).touch()
watchers = list()
def watcher_factory(script_name, interval=1.0):
modules = list()
init = None
for name, module in sys.modules.items():
filename = getattr(module, "__file__", None)
if not filename:
continue
if any(filename.startswith(f"{path}/addons/{script_name}") for path in bpy.utils.script_paths()):
pair = Pair(module, timestamp(filename))
modules.append(pair)
if filename.endswith("__init__.py"):
init = pair
print("Watching", modules)
def watch_script():
touched = list()
for pair in modules:
now = timestamp(pair.module.__file__)
if now != pair.stamp:
pair.stamp = now
touched.append(pair)
if touched:
if init:
init.module.unregister()
for pair in touched:
print("Auto-reload", pair)
pair.module = importlib.reload(pair.module)
if init:
if init not in touched:
init.module = importlib.reload(init.module)
init.module.register()
return interval
return watch_script
def start():
prefs = bpy.context.preferences.addons[__name__].preferences
script_name = prefs.script_name
if script_name:
watcher = watcher_factory(script_name, interval=prefs.interval or 1.0)
watchers.append(watcher)
bpy.app.timers.register(watcher, first_interval=prefs.interval)
def stop():
for fn in watchers:
try:
bpy.app.timers.unregister(fn)
except ValueError:
pass
def restart(self, context):
stop()
start()
class WatchPreferences(bpy.types.AddonPreferences):
bl_idname = __name__
script_name: bpy.props.StringProperty(
name="Script Name", description="name of the script to be watched",
default="io_export_paper_model", update=restart)
interval: bpy.props.FloatProperty(
name="Interval", description="Number of seconds between subsequent checks for updates",
default=1, soft_min=0.1, soft_max=60, subtype="UNSIGNED", unit="TIME")
def draw(self, context):
self.layout.prop(self, "script_name")
self.layout.prop(self, "interval")
def register():
bpy.utils.register_class(WatchPreferences)
# wait three seconds for all addons to be loaded
bpy.app.timers.register(start, first_interval=3.0)
def unregister():
stop()
bpy.utils.unregister_class(WatchPreferences)