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_object_buttongets 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 theunregistermethod will take care of removing the button. – Gorgious May 09 '21 at 08:24Blender > System > Reload Scriptswill remove all. https://blender.stackexchange.com/questions/131290/how-to-remove-add-object-menu-entry-from-add-shift-a-mesh-after-running-scr – batFINGER May 09 '21 at 08:44