Is it possible to access mesh vertices via script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jaxin

Hi,
does anyone know how to access vertices of a 3D object? I found MeshDataTool class but from the doc it isn’t clear how to use it. I need their position in the 3D scene. Thanks.

:bust_in_silhouette: Reply From: wombatstampede

Meshdatatool is the way to go. You take a MeshInstance. Get the mesh from that (usually an ArrayMesh) and then grab one of the surfaces with create_from_surface.
All coordinates are “local” and have to be xform’ed with the global_transform of the MeshInstance when you want to compare them to other global coords.

MeshDataTool can also edit meshes but commit surfaces are added. So you need to remove the original surface in that case.

var mdt = MeshDataTool.new() 
var nd = get_node("MyMeshInstance")
var m = nd.get_mesh()
#get surface 0 into mesh data tool
mdt.create_from_surface(m, 0)
for vtx in range(mdt.get_vertex_count()):
	var vert=mdt.get_vertex(vtx)
	print("global vertex: "+str(nd.global_transform.xform(vert)))
1 Like