0

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...")
Victoralm
  • 203
  • 3
  • 12
  • I think i'm looking at the same Godot forum page as you've consulted. In order to get the positions of all points of the mesh, don't you also need to include `for i in range(0, mdt.get_vertex_count()): var position = mdt.get_vertex(i)` to iterate over all of the mesh vertices to get their locations? Then I guess, you could `print("position")` – Christopher Bennett May 01 '19 at 03:58
  • Finally I can get the vertex positions, thanks man! Now I need a way to get its world coordinates/positions... – Victoralm May 01 '19 at 09:06
  • 1
    It couldn't be something as simple as `node.get_global_transform().get_translation()`. and then calculating the coordinates of the vertices relative to that, could it? – Christopher Bennett May 01 '19 at 09:21
  • I don't know but, if it is you're genius sir!! I'll try it asap. – Victoralm May 01 '19 at 11:38

1 Answers1

0

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.

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()
Victoralm
  • 203
  • 3
  • 12