Create and fill a mesh by script

Godot Version

v4.2.1.stable.official [b09f793f5]


I want tp process and show data stored in a database (SQLite).
Something like when a button is pressed the data will be read and processed. After all there are some meshes in the scene, where I can “fly over” with my camera.

I need something like:

  • creates a mesh and put it to the scene
  • stores the vertexes/triangles to the mesh
  • stores a material/color anything like this to the mesh or to the triangles - whatever seems to be useful

Can anybody show me, how to do or where to read?
(My problem is not programming at all, I do this for many years. But I do not understand the concept completely, how to create objects by code and to assign it to a scene)

I don’t know what you need exactly. But basically you need the same steps to create Nodes by gdscript. Imagine you have a Node2D/3D that is called “main” with a script “” attached. As a child of this “main” you have the button. Select the button in the right side of the editor, switch to “node” tab, doubleclick “on_pressed” and connect it to “”.
In this function you can create new nodes with:

func on_connect… # the slot to your pressed signal
	var bd =
	var cs =
	var mi =
	mi.set_meta("SearchString", 1.0) # save any custom information you need
	add_child(bd) # adds child to the script owner → "main"

Does this help you?

See: Procedural geometry — Godot Engine (stable) documentation in English

  • create a MeshInstance3D (and add it to the scene using the add_child function), this node will render your mesh
  • create a Mesh and assign it to the MeshInstance3D (for example in form of an ArrayMesh), this object will hold the actual mesh data the MeshInstance3D will render
  • ???
  • profit

The SurfaceTool is useful if you want to construct a mesh at runtime.

If you already have your indices, vertices, uvs and normals, just make sure they have the correct format (Godot uses clockwise winding) and put them into your ArrayMesh. You can find a more detailed explanation here. ← seriously, read this

Blockquote realityvsdreams
Thanks. It is a nice beginning. Is there a way to add “bd” to a special node, f.e. the Root node of a scene? And if so, is it possible to delete this body with all elements from another Button?
If I understand this code crrect bd, cs and mi are local vars (probably sth like pointers), that are connected and by add_child(bd) the complex is given to another node (in this case the parent node). Right?

Blockquote Armynator
I try this out, thank you for this.

Sure. You can do everything by script. I don’t know how your scene hierarchy is, but you always can use get_parent() or get_node() (or the abbreviations with $Name or %Name) to get the desired object and then use .add_child(bd) to add it.