15
$\begingroup$

I'm currently twiddling with an old addon and insert some print statements for debugging. Currently I'm restarting Blender each time to see the new messages. Is there a way to 'refresh' an already loaded Python script?

An API call would be preferred to disable/re-enable the addon in the User Preferences dialog.

$\endgroup$
25
$\begingroup$

Hitting F8 or searching for "Reload scripts" from the spacebar search, it reloads all addons and scripts, running addons unregister and register functions.

$\endgroup$
  • 1
    $\begingroup$ Thanks this works, unfourtunatly it doesn't care about my opened sockets and complains that I tried to open the socket twice (OSError: [WinError 10048]). Is there a 'harder' reload. This should probably another question. I will upvote later (run out of upvotes for today). $\endgroup$ – stacker Sep 3 '13 at 20:39
  • 1
    $\begingroup$ @stacker, you could handle any resources (sockets, open files etc), in the addons unregister function. This should allow you to support cleaning up before reloading $\endgroup$ – ideasman42 Sep 4 '13 at 2:08
  • 1
    $\begingroup$ It is worth noting that this will only reload the __init__.py portion of the addon. Any changes in imported sub-modules will not be registered unless Blender is re-opened or code is included in accordance with this answer blender.stackexchange.com/a/28505/3292 $\endgroup$ – splic May 9 '18 at 21:13
  • $\begingroup$ In Blender 2.8 F8 doesn't work out of the box anymore. Search with F3 for reload scripts or remap it yourself (answer from: devtalk.blender.org/t/reload-button-in-2-8/1708) $\endgroup$ – NumesSanguis Apr 21 at 5:04
  • $\begingroup$ The place to add the shortcut, as seen in the image in the above link, can be found: Preferences -> Keymap -> Screen -> Screen (Global) -> Add New -> collapse, then enter script.reload with your desired shortcut. $\endgroup$ – NumesSanguis Jul 21 at 14:08
4
$\begingroup$

To reload a Blender Addon at the console type:

>>> bpy.ops.wm.addon_enable(module='your_module_name')

Note: under-the-hood Blender uses Python's imp.reload to reload modules.

One of its caveats is that the module dictionary will not be replaced. Since that is where module globals are stored, this can lead to surprises (if expecting a pristine module state after reload).

As such, restarting Blender semi-frequently is still a good idea even when using console reloading during development.

Note that the imp.reload behavior can also be used (with care) as a debug-enabling tool; for example, to "persist" a network socket across Addon reloads:

try:
    socket_that_survives_reloads
except NameError:
    socket_that_survives_reloads = None

if not socket_that_survives_reloads:
    print("creating new socket connection")
    socket_that_survives_reloads = however_you_connect()
else:
    print("re-using existing socket connection")
$\endgroup$
  • $\begingroup$ I have compiled Blender as a Python3 module so I don't have access to the GUI. I am installing an addon and enable it using the function you pointed. I then save the user preferences using bpy.ops.wm.save_userpref() and exit Python.Next time that I do import bpy I don't see the addon API being added to the bpy module.Do you know how I can fix that?If I run the same exact script in Blender terminal (when using the GUI) everything works.(You may answer here) $\endgroup$ – Amir Sep 17 '18 at 18:52
  • $\begingroup$ In Blender 2.80 the correct code shortcut to reload ALL add-ons is: bpy.ops.script.reload() (developer.blender.org/T66924). bpy.ops.wm.addon_enable(module='your_module_name') won't work for multi-file add-ons, as there is a check, which only looks at whether __init__.py has changed. $\endgroup$ – NumesSanguis Jul 21 at 14:10
0
$\begingroup$

To enable and disable via the API use bpy.ops.wm.addon_enable, and bpy.ops.wm.addon_disable respectively.

$\endgroup$
  • $\begingroup$ Not the recommended way for Blender 2.8 according to a Blender developer Sybren: developer.blender.org/T66924 . Instead use bpy.ops.script.reload() $\endgroup$ – NumesSanguis Jul 21 at 14:18

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.