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.

4.4 7 votes
Article Rating
Subscribe
Notify of
guest

2 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
DeimonSpikelet777
DeimonSpikelet777
25 days ago

1. It will be rightly to sometimes use “print(bpy)”, “print(list(bpy))” or “print(dir(bpy))”.

2. What’s wrong with “bpy.ops.object.add(type=’MESH’)”?

Last edited 25 days ago by DeimonSpikelet777
Nikita
25 days ago

1. You can use the “print” instruction at any time you need to see the required statements.
2. Nothing wrong but operators are sometimes unstable and can require to override the context to be properly executed. If you can, it is better to make your code without system operators.