New addon Reload text if modified

Here is a new addon. It reloads a text datablock if the source file has changed externally. To try the addon:

  • Activate the addon,
  • Create a new text data block
  • Save this as a text file
  • On the header bar, check Reload if Modified
  • Open the text file in another text editor, modify it and save your changes
  • Go back to blender and the changes are updated automatically

wikipage:
http://wiki.blender.org/index.php/Extensions:2.5/Py/Scripts/Text_Editor/Reload_if_Modified

This is very simple but also very useful. I think it is quite stable, but if you encounter any problem, please let me know.

And any feedback is welcome.
Bye!

1 Like

Hi,
The link to download no longer works. Can you reshare this? Is there anything else similar available? I also would like a script that will save all text datablocks to their external file if it exists.
Thanks

based on initial testing, this appears to work:


reload_on = False #reload from external file or save all from internal
import bpy
area = bpy.context.area
for text in bpy.data.texts:
    area.spaces[0].text = text
    if text.filepath != '' and reload_on == True:
        bpy.ops.text.reload()
    if text.filepath != '' and text.is_dirty:
        bpy.ops.text.save()

The link not worked :frowning:
This is a very good addon I want it but the link not working.

Found the addon ,but would need to be updated.

1 Like

Updated the addon as it was doing exactly what I needed. Though the code update is tied to depsgraph update (so now it’s updated only when the viewport was updated). Any ideas how to update it more often?

1 Like

Put it in a timer. When doing say, make sure you read the section of the timers documentation that talks about threading and safely updating scene data.

1 Like

Thank you for the suggestion, totally forgot about the timers option. Updated the script above.

I’ve read the timer documentation, not sure if it applies to my case since I’m going to have just 1 thread with example code bellow (code is simplified though).

Sure, it’s possible to start separate timers for each text and then I’d need to store them in queue and run from the main timer thread and I guess that might be more optimal since there won’t be any checks except check for empty queue if there are no texts marked. But then the timer that checks would need to know the text it’s going to check and since text object reference is unreliable then I’d also need to use text’s names and keep track of their changes…

So I think it should work fine for the most purposes the way it is.

import bpy


def check_texts_every_second():
    for text in bpy.data.texts:
        if text.is_in_memory:
            continue
        
        if text.is_modified and text.reload_if_modified:        
            text.from_string(new_text)

    return 1 # every second

bpy.app.timers.register(check_texts_every_second)
1 Like