Any ways to access a mesh data? (to spread objects over it)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Nuno Donato
:warning: Old Version Published before Godot 3 was released.

To long story short, I’m looking into different methods to spread grass over a large terrain in an automatic manner (not me placing them).

I was thinking that if I could get the terrain mesh data, I could somehow go trough all the faces and place new objects at those positions, but cant find a way to do this. Is it possible at all?

Any other suggestions on how to go about this problem? :slight_smile:

I want to do the exact same thing. Did you make your way through this?

rredesigns | 2017-06-02 01:52

:bust_in_silhouette: Reply From: Zylann

I think you can do that with MeshDataTool, a helper class that you build from a mesh to access its attributes (vertices, UVs etc) MeshDataTool — Godot Engine (stable) documentation in English

I never used this and its not well documented, but from what I see, it doesn’t look complicated to use.
This is a guess on how to use it:

func do_stuff_with_mesh(mesh):
	var tool = MeshDataTool.new()

	# Initialize the tool from the mesh and a surface index.
	# Meshes contain surfaces, which in turn contain vertices.
	# I guess you want the first surface, but keep that in mind
	# in case you have multi-material meshes
	tool.create_from_surface(mesh, 0)

	# Then iterate all vertices and whatever
	for i in range(0, tool.get_vertex_count()):
		var position = tool.get_vertex(i)
		# ...

	# Delete the tool once you're done with it, because it only inherits Object
	# (or use .clear() if you want to re-use it for another mesh)
	tool.free()

(not tested)

How did I miss this? I was looking at the surface tool, but it was only for building new stuff, not reading what was already there.

Thanks a lot :slight_smile:

Nuno Donato | 2017-03-08 09:25