1
$\begingroup$

I have made a new object for the Add > Mesh menu and I would like to unregister it but when I try to with:

if __name__ == "__main__":
   unregister()

I get the following error:

Traceback (most recent call last):
  File "\add_tetrahedron.py", line 63, in <module>
    register()
  File "\add_tetrahedron.py", line 59, in unregister
    bpy.utils.unregister_class(OBJECT_OT_add_object)
RuntimeError: unregister_class(...):, missing bl_rna attribute from 'OrderedMeta' instance (may not be registered)

I've tried looking for this error but, I can't find what I'm doing wrong. I can't find anything about bl_rna anywhere.

I'm using Blender 2.79b

The register functions look like this:

# Registration

def add_object_button(self, context):
    self.layout.operator(
        OBJECT_OT_add_object.bl_idname,
        text="Tetrahedron",
        icon='PLUGIN')

def register():
    bpy.utils.register_class(OBJECT_OT_add_object)
    bpy.types.INFO_MT_mesh_add.append(add_object_button)


def unregister():
    bpy.utils.unregister_class(OBJECT_OT_add_object)
    bpy.types.INFO_MT_mesh_add.remove(add_object_button)

if __name__ == "__main__":
    register()
$\endgroup$
  • $\begingroup$ the register() function works just fine. $\endgroup$ – Whitequill Riclo Mar 15 '19 at 15:35
3
$\begingroup$

Consult python docs for a better explanation

Since there is no main() function in Python, when the command to run a python program is given to the interpreter, the code that is at level 0 indentation is to be executed. However, before doing that, it will define a few special variables. __name__ is one such special variable. If the source file is executed as the main program, the interpreter sets the __name__ variable to have a value “__main__”.

If this file is being imported from another module, __name__ will be set to the module’s name. __name__ is a built-in variable which evaluates to the name of the current module. Thus it can be used to check whether the current script is being run on its own or being imported somewhere else by combining it with if statement, as shown below.

if __name__ == "__main__":
    register()

which when run from text editor in above case registers the class OBJECT_OT_add_object When run again it has "no idea" that it is the same class as registered before. ( Hence the error if you try to unregister)

If however the class is registered when enabled as an addon it will be in that addon modules namespace my_addon.OBJECT_OT_add_object (ie __name__ == "my_addon") and will be the same class when unregistered from the addon.

This is included to test scripts in the text editor. Removing it will have no consequence if installed as an addon.

Have a feeling an issue here is:

When testing such scripts it has the tendency to append and append and append the item to the menu.

A solution to this is bpy.ops.script.reload() which in 2.7x has the shortcut F8 or can be run in the python console. Recommend writing a little script to prepend it to the text editor and console headers for convenience

|improve this answer|||||
$\endgroup$

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.