MeshDataTool: Getting the vertex ID from position.

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

I was wondering how you would get the Vertex ID from a position relative to the mesh’s position. Basically the opposite of get_vertex(id), which allows you to get the position of a vertex from it’s ID.

Thank you.

:bust_in_silhouette: Reply From: jgodfrey

I’m not familiar with MeshDataTool, but I don’t see any bulit-in way of getting what you want. You could just iterate on the vertex collection to find what you’re after. Maybe something like this will work?

func get_vertex_id(mdt: MeshDataTool, target_vertex: Vector3)-> int:
	for i in range(mdt.get_vertex_count()):
		var vertex = mdt.get_vertex(i)
		if vertex.is_equal_approx(target_vertex):
			return i
	return -1

I’m decently familiar with MeshDataTool and yes, unless I’ve missed something glaringly obvious, looping as you described is the only way to pull the id from a position argument.

It’s expensive but honestly, if you’ve got an MDT you’re almost certainly looping the verts at some point (that’s what it’s for after all) so you can fit this in within an existing loop at negligible extra cost.

DaddyMonster | 2022-07-22 22:35