Topic was automatically imported from the old Question2Answer platform.
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...")
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()