31
$\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$
1

3 Answers 3

43
$\begingroup$

You can go to the Main menu > Blender > System > Reload Scripts. As the name suggests, the operator reloads all addons and scripts, running addons, unregister and register functions.

enter image description here

Another way is hitting F3 and typing "Reload Scripts" in the Operator Search menu and execute the operator. In versions pre Blender 2.8x you can just hit F8 in order to reload.

$\endgroup$
5
  • 2
    $\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
    Commented Sep 3, 2013 at 20:39
  • 2
    $\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
    Commented Sep 4, 2013 at 2:08
  • 5
    $\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
    Commented May 9, 2018 at 21:13
  • 1
    $\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$ Commented Apr 21, 2019 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$ Commented Jul 21, 2019 at 14:08
5
$\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$
2
  • $\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
    Commented Sep 17, 2018 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$ Commented Jul 21, 2019 at 14:10
2
$\begingroup$

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

$\endgroup$
1
  • 2
    $\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$ Commented Jul 21, 2019 at 14:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .