2
$\begingroup$

I'm trying to create an addon. I'm able to run the script from the text editor but when I to try install it via User Preferences, I get the following error:

bpy.utils.register_class(SmoothMonkey)
ValueError register_class(...) already registered as a subclass 'SmoothMonkey'

enter image description here

bl_info = {
    "name": "3D Simple X",
    "author": "8N Films",
    "version": (1, 0),
    "blender": (2, 92, 100),
    "location": "View3D > Toolbar > 3D Simple X",
    "description": "Very Simple and easy! Add everything you want!",
    "wiki_url": "https://www.youtube.com/channel/UCpHUlOmZ2XWhcRk7ykPbWiQ",
    "category": "8N Films",
}

import bpy

class SimplePanel(bpy.types.Panel):
    bl_label = "3D Simple X"
    bl_idname = "PT_SimplePanel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = '8N Films'
   
    def draw(self, context):
        layout = self.layout
       
        row = layout.row()
        row.label(text= "Add everything you want!")
        
        row = layout.row()
        row.label(text= "Simple & easy!   -8N Films", icon= 'ARMATURE_DATA')
        
        
        
        
class AdderXPanel(bpy.types.Panel):
    bl_label = "Adder X"
    bl_idname = "PT_AdderXPanel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = '8N Films'
    bl_parent_id ='PT_SimplePanel'
    bl_options = {'DEFAULT_CLOSED'}
   
    def draw(self, context):
        layout = self.layout
       
        row = layout.row()
        row.label(text= "Add an object", icon= 'OUTLINER_OB_EMPTY')  
        
        
        row = layout.row()
        row.operator("object.text_add", icon= 'OUTLINER_OB_FONT', text= "Add text")
       
        row = layout.row()
        row.operator("mesh.primitive_cube_add", icon= 'CUBE', text= "Add cube")
        
        row = layout.row()
        row.operator("mesh.primitive_uv_sphere_add", icon= 'SPHERE', text= "Add sphere")
        
        row = layout.row()
        row.operator("mesh.primitive_cone_add", icon= 'CONE', text= "Add cone")
       
        row = layout.row()
        row.operator("mesh.primitive_torus_add", icon= 'MESH_TORUS', text= "Add torus")
        
        row = layout.row()
        row.operator("object.camera_add", icon= 'OUTLINER_OB_CAMERA', text= "Add camera")
        
        row = layout.row()
        row.operator("object.light_add", icon= 'OUTLINER_OB_LIGHT', text= "Add light")
        
        
class ToolPanel(bpy.types.Panel):
    bl_label = "Tools"
    bl_idname = "PT_ToolPanel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = '8N Films'
    bl_parent_id='PT_SimplePanel'
    bl_options = {'DEFAULT_CLOSED'}
   
    def draw(self, context):
        layout = self.layout
       
        row = layout.row()
        row.label(text= "Choose your tools", icon= 'TOOL_SETTINGS') 

        row = layout.row()
        row.label(text= "                     X             Y             Z")
        
        row = layout.row()
        row.prop(context.object, "scale")

        row = layout.row()
        row.operator("object.modifier_add", icon= 'MODIFIER', text= "Modifier")

        
        
        
        
class ScourceXPanel(bpy.types.Panel):
    bl_label = "Scource X"
    bl_idname = "PT_ScourceXPanel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = '8N Films'
    bl_parent_id='PT_SimplePanel'
    bl_options = {'DEFAULT_CLOSED'}
   
    def draw(self, context):
        layout = self.layout
       
        row = layout.row()
        row.label(text= "Add a model", icon= 'FILE_BLEND')
       
        row = layout.row()
        row.operator("mesh.primitive_monkey_add", icon= 'MESH_MONKEY', text= "Add monkey")
        
        row = layout.row()
        row.operator("mesh.smooth_monkey", icon= 'MONKEY', text= "Add smooth monkey")
        

def main(context):
    bpy.ops.mesh.primitive_monkey_add()
    bpy.ops.object.modifier_add(type='SUBSURF')
    bpy.context.object.modifiers["Subdivision"].levels = 4


class SmoothMonkey(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "mesh.smooth_monkey"
    bl_label = "Add Smooth Monkey"


    def execute(self, context):
        main(context)
        return {'FINISHED'}


def register():
    bpy.utils.register_class(SmoothMonkey)
    bpy.utils.register_class(SimplePanel)
    bpy.utils.register_class(AdderXPanel)
    bpy.utils.register_class(ToolPanel)
    bpy.utils.register_class(ScourceXPanel)
       

def unregister():
    bpy.utils.unregister_class(SmoothMonkey)
    bpy.utils.unregister_class(SimplePanel)
    bpy.utils.unregister_class(AdderXPanel)
    bpy.utils.unregister_class(ToolPanel)
    bpy.utils.unregister_class(ScourceXPanel)
   

register()

    # test call
    # bpy.ops.mesh.smooth_monkey()

Q: What am I doing wrong and how can I fix this error?

$\endgroup$
5
  • $\begingroup$ Looks like you are registering the same class twice. $\endgroup$
    – Gorgious
    Commented Mar 4, 2021 at 7:25
  • $\begingroup$ Can I send the py. to you and help me to check? $\endgroup$
    – 8N Films
    Commented Mar 4, 2021 at 8:09
  • 1
    $\begingroup$ Did you quit and restart Blender between the time when you ran it in the script editor and when you tried to install it as an add-on ? $\endgroup$
    – Gorgious
    Commented Mar 4, 2021 at 8:26
  • $\begingroup$ yes. you can see my script: drive.google.com/file/d/1Y3j6zdOrycCuR2XAE2NpFKcsRMP2TzHB/… $\endgroup$
    – 8N Films
    Commented Mar 4, 2021 at 9:07
  • $\begingroup$ Hi and welcome. I took the liberty and edited your question according to the standards of this site, just roll back my edit if you do not like it for whatever reason. BTW: Please take a minute and read about how this site actually works: blender.stackexchange.com/tour, Thanks. $\endgroup$
    – brockmann
    Commented Mar 4, 2021 at 9:56

1 Answer 1

7
$\begingroup$

When installing an add-on as a .py file, Blender automatically looks for and executes the register method if there is one. Also, when disabling the add-on, it will automatically execute the unregister method.

But since you are already calling register in the body of the file, it is executed once when parsing the .py file, and once again by Blender when enabling the add-on. But you can't register the same Panel twice. Thus the error.

Usually what developers do to prevent it is to add an if statement that will not execute if the file is not run directly. You just need to add if __name__ == "__main__": before your register() line. The statement will evaluate to False if run from another source than the main file, and to True when executed in the Script Editor.

Alternatively you can just get rid of this line if you plan on using the script only as an add-on.

More on that if statement

$\endgroup$
3
  • $\begingroup$ Thank you! I did it. I fix the problem now. $\endgroup$
    – 8N Films
    Commented Mar 5, 2021 at 0:00
  • $\begingroup$ If that fixed your issue, please consider to accept the answer @8NFilms What should I do when someone answers my question? $\endgroup$
    – brockmann
    Commented Mar 5, 2021 at 8:26
  • $\begingroup$ @HarryMcKenzie It means a class is registered twice. It might be a name collision from another addon, or something else. Are you using the autoload file from Jacques Lucke ? $\endgroup$
    – Gorgious
    Commented Feb 9 at 9:14

You must log in to answer this question.

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