1
$\begingroup$

Wanting to learn how to create meshes using Python, I followed along with this tutorial:

https://www.youtube.com/watch?v=8wd_EAov6ZE

However, a new "Add Object" is getting added to my Add Mesh menu every time I run the Python code. I've searched all over the internet and haven't been able to figure out how to delete them. It would be even better if, every time I ran my Python code, it would replace the previous "Add Object" rather than adding a new one.

Here's the code

bl_info = {
    "name": "Bilal's Object",
    "author": "Bilal",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "View3D > Add > Mesh > New Object",
    "description": "Adds a new Mesh Object",
    "warning": "",
    "doc_url": "",
    "category": "Add Mesh",
}


import bpy
from bpy.types import Operator

from bpy_extras.object_utils import AddObjectHelper, object_data_add
from mathutils import Vector


def add_object(self, context):

    verts = [
        Vector( ( -0.291521, -0.036571, 0.272471 ) ),
        Vector( ( -0.143110, -0.036571, 0.272471 ) ),
        Vector( ( -0.143110, -0.526643, 0.272471 ) ),
        Vector( ( -0.291521, -0.526643, 0.272471 ) ),
        Vector( ( -0.291521, -0.036571, 0.144022 ) ),
        Vector( ( -0.143110, -0.036571, 0.144022 ) ),
        Vector( ( -0.143110, -0.526643, 0.144022 ) ),
        Vector( ( -0.291521, -0.526643, 0.144022 ) )
    ]
    
    edges = [ ]
    faces = [ [ 0, 1, 2, 3 ], [ 4, 5, 6, 7 ],
              [ 0, 4, 7, 3 ], [ 1, 5, 6, 2 ],
              [ 0, 1, 5, 4 ], [ 3, 2, 6, 7 ] ]

    mesh = bpy.data.meshes.new(name="New Object Mesh")
    mesh.from_pydata(verts, edges, faces)
    # useful for development when the mesh may be invalid.
    # mesh.validate(verbose=True)
    object_data_add(context, mesh, operator=self)


class OBJECT_OT_add_object(Operator, AddObjectHelper):
    """Create a new Mesh Object"""
    bl_idname = "mesh.add_object"
    bl_label = "Add Mesh Object"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):

        add_object(self, context)

        return {'FINISHED'}


# Registration

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


# This allows you to right click on a button and link to documentation
def add_object_manual_map():
    url_manual_prefix = "https://docs.blender.org/manual/en/latest/"
    url_manual_mapping = (
        ("bpy.ops.mesh.add_object", "scene_layout/object/types.html"),
    )
    return url_manual_prefix, url_manual_mapping


def register():
    bpy.utils.register_class(OBJECT_OT_add_object)
    bpy.utils.register_manual_map(add_object_manual_map)
    bpy.types.VIEW3D_MT_mesh_add.append(add_object_button)


def unregister():
    bpy.utils.unregister_class(OBJECT_OT_add_object)
    bpy.utils.unregister_manual_map(add_object_manual_map)
    bpy.types.VIEW3D_MT_mesh_add.remove(add_object_button)


if __name__ == "__main__":
    register()

And here's a screenshot of the Add Mesh menu:

Add Mesh menu infested with "Add Object" items

$\endgroup$
2
  • $\begingroup$ I think it would be troublesome to do it that way, every time you run the script the add_object_button gets assigned a new value in memory and you can't easily access previously added instances of it to unregister it. It may be a hassle if you're developping your script still, but consider running it as an add-on rather than in the text editor, that way the unregister method will take care of removing the button. $\endgroup$
    – Gorgious
    May 9 at 8:24
  • $\begingroup$ Blender > System > Reload Scriptswill remove all. blender.stackexchange.com/questions/131290/… $\endgroup$
    – batFINGER
    May 9 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.