0
$\begingroup$

I need to call the unregister function before disabling the addon. I can get this information about the addon:

addon_collection = {}
for addon in addon_utils.modules():
    addon_name = addon_utils.module_bl_info(addon)
    addon_file_name = addon.__name__
    # my_name.update({addon_name['name'] : addon_file_name})
    addon_items = {addon_name['name'] : addon_file_name}
    addon_collection.update(addon_items)

print(addon_collection)

I get such a dict with keys.

{'Magic UV': 'magic_uv',
'TexTools': 'TexTools_1_5',
'3D Navigation': 'space_view3d_3d_navigation',
'Stored Views': 'space_view3d_stored_views',
'Cycles Render Engine': 'cycles'}

Also, I can get the path to the file:

for addon in addon_utils.modules(refresh=False):
    print(addon.__file__)
D:\blender\0\3.1\scripts\addons\node_presets.py
D:\blender\0\3.1\scripts\addons\node_wrangler.py
D:\blender\0\3.1\scripts\addons\space_view3d_align_tools.py
D:\blender\0\3.1\scripts\addons\object_boolean_tools.py
D:\blender\0\3.1\scripts\addons\object_carver\__init__.py
D:\blender\0\3.1\scripts\addons\object_fracture_cell\__init__.py
D:\blender\0\3.1\scripts\addons\drop_it\__init__.py

From these lists it turns out that I can refer to the addon

  1. By file name
  2. The name of the addon
  3. The path to the addon file

How to call unregister function? Without knowing the internal classes or other functions that have different names, but are somehow called when the unregister function is triggered. Thank you in advance )

$\endgroup$

1 Answer 1

0
$\begingroup$
import object_fracture_cell
if "unregister" in dir(object_fracture_cell):
    object_fracture_cell.unregister()

If you prefer try/except

import object_fracture_cell
try:
    object_fracture_cell.unregister()
except AttributeError:
    print("No unregister function in object_fracture_cell addon")
except RuntimeError:
    print("object_fracture_cell has already been unregistered")

From my understanding if the addon is a single file, blender will seek a unregister function inside it and nothing else, and if it is a multifile addon, it will seek __init__.py then seek the unregister function and nothing else.

So you can assume that if an addon can be unregistered, importing it then calling my_addon.unregister() will call all the unregister logic.

However I don't think it is mandatory for an addon to have an unregister function so you should add an if or try/except block.

for instance :

import node_presets

print(dir(node_presets))

import object_fracture_cell

print(dir(object_fracture_cell))
['AddonPreferences', 'Menu', 'NODE_MT_template_add', 'NODE_OT_template_add', 'NodeTemplatePrefs', 'Operator', 'StringProperty', '__addon_enabled__', '__addon_persistent__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__time__', 'add_node_button', 'bl_info', 'bpy', 'classes', 'node_center', 'node_search_path', 'node_template_add', 'node_template_cache', 'os', 'register', 'unregister']
['BoolProperty', 'EnumProperty', 'FloatProperty', 'FloatVectorProperty', 'FractureCell', 'IntProperty', 'Operator', 'StringProperty', '__addon_enabled__', '__addon_persistent__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__time__', 'bl_info', 'bpy', 'main', 'main_object', 'menu_func', 'register', 'unregister']
$\endgroup$

You must log in to answer this question.

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