5
$\begingroup$

I am working on creating an addon and I have a main __init__.py file and then I have a another file we will call utils.py. __init__ registers all the classes in utils by import utils and then bpy.utils.register_class(utils.CLASS), it has several classes and that file registers them all.

If you run the file once it works fine, but if you run it again it gives the error that the class is already registered as a subclass.

utils contains a bpy.types.Panel and bpy.types.Operator class. It is almost like the bpy.utils.unregister_class(utils.CLASS) isn't working.

$\endgroup$

2 Answers 2

8
$\begingroup$

You can replace all calls to bpy.utils.register_class(...) by a single call to

bpy.utils.register_module(__name__)

and all calls to bpy.utils.unregister_class(...) by a single

bpy.utils.unregister_module(__name__)

You won't have to (un-)register single classes ever again and your error should no longer occur. Do it in __init__.py only and import all module files from here (from . import ...).

Note that the order of class definitions may matter, e.g. if you use a custom property group class in an operator (the code of the property group class needs to be above the class definition of the operator).

$\endgroup$
10
  • $\begingroup$ so my import would say from . import utils in my __init__.py file, or are you saying leave that all out. If I put the import code as I just did I get a Parent Module "" not found, can't import error, if I leave out that line it runs, but doesn't register the classes in the second file, same thing happens if I do from .utils import * $\endgroup$ Commented Oct 23, 2014 at 0:40
  • $\begingroup$ I added a link to an example addon comprised of 3 files to show how to import properly with reload support. $\endgroup$
    – CodeManX
    Commented Oct 23, 2014 at 10:51
  • $\begingroup$ I added what you said, and I am still getting that same error. I attached my files to my original post. $\endgroup$ Commented Oct 23, 2014 at 12:40
  • $\begingroup$ Installed and enabled your addon, also refreshed with F8, and there is no error. Did you restart Blender after the changes? Because you sometimes need to, a refresh/reload is not enough. $\endgroup$
    – CodeManX
    Commented Oct 23, 2014 at 20:08
  • 1
    $\begingroup$ Looks like register_module and unregister_module functions no longer exist. see stackoverflow.com/a/59926247/2981429 $\endgroup$ Commented Sep 29, 2021 at 20:15
0
$\begingroup$

This happens when you load the same add on multiple times. Try removing the add on. close the blender. Relaunch the blender and now try adding the add on. Worked for me.

$\endgroup$

You must log in to answer this question.

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