-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpoll.py
59 lines (47 loc) · 2.33 KB
/
poll.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
from typing import Callable
import bpy
import os
POLL_RATE = 0.1
def watch_for_text_changes(
text: bpy.types.Text, callback: Callable, poll_rate: float = POLL_RATE
):
abs_filepath = bpy.path.abspath(text.filepath, library=text.library)
last_string = text.as_string()
last_modified = not text.is_in_memory and os.path.getmtime(abs_filepath) or None
# TODO: @uki-dev can we pull this outside of this function?
def timer():
# Wrap in try/except as sometimes, perhaps due to the fact that we are 'reloading' the text, a `ReferenceError: StructRNA of type Text has been removed` will be thrown
try:
nonlocal last_string, last_modified
# Support filepath of text changing
abs_filepath = bpy.path.abspath(text.filepath, library=text.library)
# TODO: Find a way to avoid:
# - Marking the blend file as having unsaved changes due to hot reloading external file
# - Overwriting legitimate local changes when external file is modified
# Currently, because we want to load this in the background without `bpy.ops.text.reload`,
# we have to read the file from disk natively, and overwrite the texts contents with `from_string`.
# This subsequently marks the text as being dirty and modified.
if not text.is_in_memory:
# If text is external and it has no local changes, check if it has been modified and `revert` to reload from disk
modified = os.path.getmtime(abs_filepath)
if modified > last_modified:
last_modified = modified
reload_text(text)
string = text.as_string()
if string != last_string:
last_string = string
callback()
except:
pass
return poll_rate
bpy.app.timers.register(timer)
def dispose():
if bpy.app.timers.is_registered(timer):
bpy.app.timers.unregister(timer)
return dispose
def reload_text(text: bpy.types.Text):
if text.is_in_memory:
raise ValueError(f"Text object ('{text.name}') is not an external file.")
abs_filepath = bpy.path.abspath(text.filepath, library=text.library)
with open(abs_filepath) as file:
text.from_string(file.read())