trying to get vertices of a MeshInstance using MeshDataTool

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ben Nicholl
func _on_add_weight_pressed():
	#below 2 lines will be int values
	var weight_dimension = weight_value.text
	var input_dimension = input_value.text
	
	var surface_tool = SurfaceTool.new()
	surface_tool.begin(Mesh.PRIMITIVE_LINES)
#add vertices, thus creating a line
	surface_tool.add_vertex(Vector3(0,0,0))
	surface_tool.add_vertex(Vector3(weight_dimension,0,1))
	# this creates the mesh with all of the above vertex's and other attributes
	var mesh = Mesh.new()
	surface_tool.commit(mesh)
	# this actually creates an object instance of the above mesh
	var mesh_inst = MeshInstance.new()
	mesh_inst.mesh = mesh

	add_child(mesh_inst)

This code will successfully give me a line. If I try and use MeshDataTool to get those vertices in the same function with the below code.

var tool = MeshDataTool.new()
tool.create_from_surface(mesh_inst, 0)
print(tool.get_vertex_count())
print(tool.get_vertex(0))

the two lines that should print out the vertex count and what the vertices give me output:

0
(0, 0, 0)

I am not sure what I’m doing wrong. Is there a way to get the vertices that have been instantiated in the mesh_inst object? I’m ultimately trying to change those vertices given some input.

:bust_in_silhouette: Reply From: wombatstampede

How about:
tool.create_from_surface(mesh_inst.mesh, 0)

You tried to create to create from the meshinstance but create_from_surface expects an ArrayMesh therefore use its attribute mesh.

I still get the same output

Ben Nicholl | 2020-04-04 14:38

No error messages in the output window?

What is the value of mesh_inst.get_surface_material_count ()

What is the value of mesh_inst.mesh.surface_get_array_len (0)

I am surprised that you get no error when you print get_vertex(0) because that should be out of range when the vertext count is 0.

Do you get an error when you try to print tool.get_vertex(1) ?

wombatstampede | 2020-04-04 15:07

There is no error message in output window.

mesh_inst.get_surface_material_count() = 1

mesh_inst.mesh.surface_get_array_len(0) = 2

tool.get_vertex(1) does not give me an error. it gives me the same output as before, which is (0, 0, 0)

Ben Nicholl | 2020-04-04 18:13

Ah, createfromsurface only works with PRIMITIVETRIANGLE mesh types.
See
MeshDataTool — Godot Engine (stable) documentation in English

wombatstampede | 2020-04-04 19:44