I have a script that watches for changes in another script, and re-runs the latter if it's changed. Gist here. It's run like this:
~/blender/blender-2.92.0-linux64/blender --python script_watcher.py -- test_group.py
It worked ok until I used the reduce()
function in the watched script - snippet here (union()
is a function that creates the union of two objects):
def all_union(first, *others, **kwargs):
def _(a, b):
return union(a, b)
r = reduce(_, others, first)
return r
then I get an error message like this:
NameError: name 'reduce' is not defined
I tried replacing the timer script with one that runs only once - like this:
import bpy
import sys
import os
import traceback
prev = ""
curr = open(sys.argv[-1]).read()
if not prev==curr:
print("%s changed" % sys.argv[-1])
try:
exec(curr)
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
print(traceback.format_exc())
and it runs perfectly - no error.
So why is reduce()
not not working under the timer? or is the issue somewhere else?
from functools import reduce
. Also this question is probably offtopic - not about Blender, but about Python. $\endgroup$from functools import reduce
right at the top. $\endgroup$from functools import reduce
inside the function definiton everything works ok. Also, usingfunctools.reduce()
works ok $\endgroup$bpy.app.timers.register
. Given error message the method passed, when run from timer thread, has no idea of the locals. As MB says simply import in the function. Consider looking at when file was last touched, rather than constantly testing content for equality. Recommendpathlib
for statting and reading text files. Also have a look at script stub template. $\endgroup$