Reloading your code (with the click of a button)

Hi folks,

I wrote a blog post about reloading your own Python code with the click of a button: http://stuvel.eu/blog/203/reloading-your-code-in-blender
Blender lets you script more or less everything. You can have your code inside Blender (and the .blend file) itself, but I prefer to store it outside the .blend file. This makes it easier to handle changes, and allows you to use any editor you want. The downside is that reloading your code becomes a bit of a hassle. This is a little snippet I wrote to reload my code in Blender with the click of a button.

Visit http://stuvel.eu/blog/203/reloading-your-code-in-blender to read the code :slight_smile:

Let me know what you think!

Best,
Sybren

It doesn’t work, it fails at two lines:


            print('Reloading %s from %s' % (module, module.__file__))
            imp.reload(module)

module has no attribute file (probably just for some), you should do something like:

        attr = getattr(module, '__file__', '')
        if my_path in attr:
            print('Reloading %s from %s' % (module, attr))


imp.reload(module) reload fails with error “has no attribute loader

Hi CoDEmanX,

Thanks for your feedback, I appreciate it.

Since I already use getattr() to get the file attribute, and “my_path in getattr(module, ‘file’, ‘’)” is true, as far as I can see this can only mean that my_path is empty. In this case every loaded module matches the test, which shouldn’t happen. Your fix doesn’t seem to address that, so we need a better solution.

Can you tell me a bit more about your test case? What OS are you running on? Which Blender version? And have you saved your .blend file before testing this? Can you give me the path, and the value of my_path? All that info will help a lot in making this more robust.

EDIT: I’ve updated the code on my blog, to re-evaluate my_path every time a reload is attempted. This should help when a .blend file is saved in a different directory than when the code was first executed.

Windows 7 64bit, Blender 2.69 official. I did NOT save the blend file, is it really required?

It fails here: MODULE <module ‘_warnings’ (built-in)>

If i test for hasattr(module, ‘file’), it will reload a lot of modules, but not this:

  File "C:\Program Files\Blender Foundation\Blender\2.69\python\lib\imp.py", line 252, in reload
    return module.__loader__.load_module(name)
AttributeError: 'module' object has no attribute '__loader__'

Ah yes, I suspected as much. Yes, this is required as the code only reloads Python files that are stored in the same directory (or subdirectory) as the .blend file. These files are considered “your development stuff”, and only those are reloaded. Without a saved blend file, this is impossible to tell, hence it all breaks down. I should add something in my blog to clarify this.

UPDATE: I have added a clarification in my blog.

It seems to reload a lot of stuff with a non-saved blend, and no operators can be called after that…

Yeah, I’ll add some checks so that you get a nice message telling you to save the blend file, rather than trying to reload everything.

It should be more robust now, reloading only pure-Python scripts (no binary modules, as mine didn’t like reloading) and only working when the Blend file is saved.