3
$\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. File

$\endgroup$
6
$\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).

Example addon to show the file structure and imports: blender-addon.zip

$\endgroup$
  • $\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$ – BlendingJake Oct 23 '14 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 Oct 23 '14 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$ – BlendingJake Oct 23 '14 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 Oct 23 '14 at 20:08
  • $\begingroup$ I believe I did, does it work for you from the text editor as well? $\endgroup$ – BlendingJake Oct 24 '14 at 0:07

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.