0 votes

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...")
in Engine by (43 points)
edited by

1 Answer

0 votes
Best answer

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 "createfromsurface()" returns a non zero when an error occours.

Godot Debugger

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()
by (43 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.