0
$\begingroup$

I use an external editor for my scripting. When I return to Blender I want the script in the text editor to reload my changes. Blender does indeed detect the changes (red button) which is great but it quickly becomes a nightmare having to confirm my changes over and over.

So, is it's possible to make the "reload from disk" confirmation automatic, via preference, console command, or scripting?

enter image description here


Update 2:

@batFINGER I tried to wrap it into an addon but I'm new to this and cant get it to work properly.

Edit: removed code as it should be posted as a new question instead.

$\endgroup$
  • 1
    $\begingroup$ The addon I shared here makes a menu from a folder of scripts, it checks and reloads changed scripts before running with no prompt. If you don't want the menu, you could make an addon that adds a button that will reload without prompt. $\endgroup$ – sambler Jul 15 '18 at 7:59
  • $\begingroup$ Tnx, I'll take a look at the script and see what I can do with it. Do you know any good place to start learning about addons and blender scripting? $\endgroup$ – JoelArt Jul 15 '18 at 23:18
  • $\begingroup$ The addon intro tutorial and the python API docs. You can also find a lot of python bits here, or ask how. $\endgroup$ – sambler Jul 16 '18 at 16:50
1
$\begingroup$

Using a draw callback

Investigated a few ways to do this for an edit with vim addon I put togeter. In the end didn't go with any,

My first choice would be using the loophole in the draw callback that lets us manipulate properties, which you cannot do from a draw method. The red lifesaver on text editor header is drawn when text and text.is_modified. I have left in the reload method I use, however it doesn't update the text.is_modified. I've used the reload operator instead for this question. (intead I use os to keep tabs on file modified time)

If the test script below is run from blender text editor it will add the draw callback. To use from an addon or such, would import the class and instance it, that way it can be removed, or stopped by calling instance.remove_handle(). An update on a boolean property to turn on and off could be another alternative

The script.

import bpy

def reload(text):
    fp = bpy.path.abspath(text.filepath)
    text.clear()
    with open(fp) as f:
        text.write(f.read())
    return False

class DrawingClass:
    lock = False
    def __init__(self, context, prop):
        self.prop = prop
        self.handle = bpy.types.SpaceTextEditor.draw_handler_add(
                   self.draw_text_callback,(context,),
                   'WINDOW', 'POST_PIXEL')

    def draw_text_callback(self, context):
        if self.lock:
            return
        space = context.space_data
        text = space.text

        if text and text.is_modified:  # if is_modified(text):
            print("reloading text")
            bpy.ops.text.reload()
            '''
            self.lock + True
            self.lock = reload(text)
            '''

    def remove_handle(self):
         bpy.types.SpaceTextEditor.draw_handler_remove(self.handle, 'WINDOW')

if __name__ == "__main__":
    context = bpy.context             
    dc = DrawingClass(context, "Draw This On Screen")

Other approaches worth considering:

  • A modal timer operator. See text editor > templates > python > Operator Modal Timer . The issue here is that while a modal timer is running, you may be turning off auto-save.

  • A scene update handler. (IMO a pita, as it runs toooooo often)

$\endgroup$
  • $\begingroup$ Update 2 in first post: Thank you for the answer. I'm new to python and new to Blender. I've only managed to wrap one of my scripts into a working addon by using a template. I've tried wraping your provided code into an addon but I can't get it to show in the prefs>addon. Not really sure how to register() the class and or functions where to get it started. Also you say you have to use a callback as a "loophole", does this have to do with many contexts being read-only and scripts run as addons can't write to properties? How do one get round this limitations if one can? $\endgroup$ – JoelArt Jul 15 '18 at 23:15
  • $\begingroup$ @JoelArt On the loophole, if you try and alter properties from a draw method (eg in a panel or header), you get an error and warning that you can't do it. At the moment a draw-callback lets you do this, as shown above, if modified: the text is loaded whenever the text editor has focus (in answer to your original question). As for part ii make it a new question. $\endgroup$ – batFINGER Jul 16 '18 at 8:32
0
$\begingroup$

I dunno about automatic, but you could make a keyboard shortcut to do it for you which is a little more slick than accessing it via menu with the mouse.

$\endgroup$
  • $\begingroup$ Tnx, I tried that but it's still not good enough though, there must be a way to just have the text update when refocusing on blender or at least on text editor. $\endgroup$ – JoelArt Jul 15 '18 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.