How to delete object from scene through the Blender Python API

To completely remove the object from the scene through the Blender Python API do the following:

Open the “Text Editor” window.

Import the main Blender Python API module.

Removing objects with the operator

To delete objects with the API operators, first, switch to the “Object” mode if the scene is in the “Edit” mode.

The operator removes all the selected objects. To delete only required objects we need to deselect all the scene objects,

and next, select the required object. If we select several objects – all of them will be deleted from the scene.

Now let’s use the delete operator.

After its execution, all the selected objects will be deleted.

Final code:

Removing objects without an operator

We can also delete objects from the scene without using an operator. At first, we need to get the object to remove:

and delete it from the global objects list.

Pay attention to the second “do_unlink” parameter, which we mast set to “True” to proper unlink the object from the scene.

Final code:

After the code execution, by pressing the “Run Script” button, in case of the deleting way all the selected objects will be removed from the scene or only the required object.

Nikita

Author: Nikita

Blender add-ons developer and articles writer.