I’d like to work with the vertices and faces of an existing BoxMesh, so I’m using MeshDataTool. I read in the mesh with MeshDataTool.create_from_mesh, but after doing so, the MDT says it does not have any vertices. I’ve read through the tutorial and related documentation as much as I can, but I can’t see where I’m going wrong.
Example
Here’s a reproducible example. Create a new scene with Node3D as the root, add a MeshInstance3D, and to that mesh instance add a BoxMesh. Attach the following script to the root node:
extends Node3D
@onready var mesh_in = $MeshInstance3D
func _ready() -> void:
var mesh: Mesh = mesh_in.mesh
print(mesh.get_surface_count()) # prints 1
print(len(mesh.get_faces())) # prints 36
var mdt = MeshDataTool.new()
mdt.create_from_surface(mesh, 0)
print(mdt.get_vertex_count()) # prints 0
Here’s a screenshot of the setup (with terminal output) and the script.
You’re right, that was the issue after all. I hadn’t seen the type that create_from_surface wanted, just assumed it was any Mesh.
I added an intermediate step to convert any Mesh to an ArrayMesh. The result from this can then be read with MeshDataTool.create_from_mesh. Not sure if this is necessary for base meshes, but it certainly works for PrimitiveMesh.
func convert_to_array_mesh(mesh: Mesh) -> ArrayMesh:
var surface_array = mesh.surface_get_arrays(0)
var array_mesh := ArrayMesh.new()
array_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, surface_array)
return array_mesh