How can I access the mesh data of a MeshInstance?

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

I’m trying, without success, to access the data of a mesh from a MeshInstance node.

I’ve imported a 3d object, opened it as “New Inherited”, turned it as “Unique” and saved it as foo.mesh. Then, on a new scene, I did create a MeshInstance and loaded the foo.mesh as its Mesh.

The Script is attached to the very MeshInstance, kinda like follows:

extends MeshInstance

func _ready():
    var themesh = Mesh
    var mdt = MeshDataTool.new()
    if mdt.create_from_surface(themesh, 0):
 	    print("Ok!!")
 	    print(mdt.get_vertex_count()) # get_vertex_count() returns 0
    else:
 	    print("Failed...")
:bust_in_silhouette: Reply From: Victoralm

It doesn’t work for built in meshes. Needs to be an imported mesh, that I also save as a Godot .mesh.

Reference links: Facebook, Mesh Data Tool, Mesh Class

I was mistaking pointing to Mesh class instead of mesh attribute to get the mesh reference. And the if test needs to check pass, because “create_from_surface()” returns a non zero when an error occours.

extends MeshInstance

func _ready():
    var themesh = mesh  # Same as bellow, points to same object in memory
    var themesh2 = self.get_mesh()  # Same as above, points to same object in memory

    print("Mesh surface count: " + str(themesh.get_surface_count()))

    var mdt = MeshDataTool.new()

    if mdt.create_from_surface(themesh, 0) == OK:  # Check pass
        print("Ok!!")
        print(mdt.get_vertex_count())
    else:
        print("Fail...")

    var aMeshVerts = []

    for i in range(mdt.get_vertex_count()):
        aMeshVerts.append(mdt.get_vertex(i))  # Storing the vertices positions

    mdt.set_vertex(0, Vector3(1, 2, 1))  # Changing a vertice position
    themesh.surface_remove(0)
    mdt.commit_to_surface(themesh)
    mdt.clear()