How to create mesh through the Blender Python API

To add custom mesh to the scene through the Blender Python API we need to do the following:

Open the “Text Editor” window.

Import the main Blender Python API module.

Any mesh consists of vertices, edges, and faces. Let’s make data blocks for them.

Our simple mesh will consist only of a single vertex. So let’s fill only the vertices data block, setting the vertex coordinates.

Next, make the mesh structure with the “new_mesh” name,

and fill it from the data blocks.

We created the mesh, but it couldn’t be added to the scene as raw. Only objects could be added to the scene. Let’s make an object with the “new_object” name and link it with the created mesh.

We created the object. But there is more to do. We need a collection in which we will add the created object. Let’s make a new collection with the “new_collection” name and place it into the master scene collection.

Now we can add our object to the scene, placing it into our collection.

The final code:

After this code execution, by pressing the “Run Script” button, we will add a mesh with a single vertex to the scene.


Author: Nikita

Blender add-ons developer and articles writer.