Hello! im trying to do mesh generation by code by reading data from a binary file. The problem is im getting weird errors that are not part of my code
servers/rendering_server.cpp:892 - Method/function failed. Returning: ERR_INVALID_DATA
scene/resources/mesh.cpp:1771 - Condition "err != OK" is true.
scene/3d/mesh_instance_3d.cpp:347 - Index p_surface = 0 is out of bounds (surface_override_materials.size() = 0).
to
scene/3d/mesh_instance_3d.cpp:347 - Index p_surface = 10 is out of bounds (surface_override_materials.size() = 0).

Any idea why these error could be apearing?

The mesh generation is done by using ArrayMesh, i simply assingn it Vertices, Indices, Normals, UV1, UV2 and Tangents. Sometimes UV2 and Tangent could be empty arrays, would that cause an issue? would it be better to leave those as null?

Here's how i generate the surfaces of the mesh

static func create_mesh(model_chunk: ModelChunk, skeleton: Skeleton3D, data_for_animations: Array[Array]) -> ArrayMesh:
	var array_mesh = ArrayMesh.new()
	print(model_chunk.texture_data.textures.size())
	for texture in model_chunk.texture_data.textures:
		var array = []
		array.resize(Mesh.ARRAY_MAX)
		
		
		var surface_indeces :Array[int] = get_surface_indices(texture, model_chunk.mesh_data)
		
		var indices :Array[int] = []
		var vertices :Array[Vector3] = []
		var normals :Array[Vector3] = []
		var uv1 :Array[Vector2] = []
		var uv2 :Array[Vector2] = []
		var tangents :Array[Vector3] = []
		
		var bones = []
		var weights = []
		
		get_mesh_arrays(
			surface_indeces,texture,model_chunk.mesh_data,
			indices,vertices,normals,uv1,uv2,tangents,
			model_chunk.texture_data.extra_uv == 1)
		
		array[Mesh.ARRAY_VERTEX] = vertices
		array[Mesh.ARRAY_INDEX] = indices
		array[Mesh.ARRAY_NORMAL] = normals
		array[Mesh.ARRAY_TEX_UV] = uv1
		array[Mesh.ARRAY_TEX_UV2] = uv2
		array[Mesh.ARRAY_TANGENT] = tangents
		
		var surface_data = []
		surface_data.append(vertices)
		surface_data.append(indices)
		surface_data.append(uv1)
		data_for_animations.append(surface_data)
		
		var use_8_bones = false
		if model_chunk.weights.size() > 0:
			use_8_bones = set_bone_weights(model_chunk,bones,weights, surface_indeces, skeleton)
			array[Mesh.ARRAY_BONES] = bones
			array[Mesh.ARRAY_WEIGHTS] = weights
		
		array_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES,array)
	return array_mesh

"get_mesh_arrays" simply takes the data from the forma of the binary file to the format that godot wants.

  • It's failing at

    switch (p_arrays[i].get_type()) {
    	case Variant::PACKED_VECTOR2_ARRAY: {
    		Vector<Vector2> v2 = p_arrays[i];
    		array_len = v2.size();
    		format |= ARRAY_FLAG_USE_2D_VERTICES;
    	} break;
    	case Variant::PACKED_VECTOR3_ARRAY: {
    		ERR_FAIL_COND_V(p_compress_format & ARRAY_FLAG_USE_2D_VERTICES, ERR_INVALID_PARAMETER);
    		Vector<Vector3> v3 = p_arrays[i];
    		array_len = v3.size();
    	} break;
    	default: {
    		ERR_FAIL_V(ERR_INVALID_DATA);
    	} break;
    }

    in the engine.
    Looks like you're giving it Array[Vector3] when it expects a PackedVector3Array. Likewise with Vector2s

The MeshInstance3D is generated in the scene with the array mesh, but no visual model. Adding a material doesnt do anything either

IceBergyn Sadly the error doesnt seem to be too related, im not assigning any textures nor materials by code to the object yet. Which makes those particular error even weirder that they apear

It's failing at

switch (p_arrays[i].get_type()) {
	case Variant::PACKED_VECTOR2_ARRAY: {
		Vector<Vector2> v2 = p_arrays[i];
		array_len = v2.size();
		format |= ARRAY_FLAG_USE_2D_VERTICES;
	} break;
	case Variant::PACKED_VECTOR3_ARRAY: {
		ERR_FAIL_COND_V(p_compress_format & ARRAY_FLAG_USE_2D_VERTICES, ERR_INVALID_PARAMETER);
		Vector<Vector3> v3 = p_arrays[i];
		array_len = v3.size();
	} break;
	default: {
		ERR_FAIL_V(ERR_INVALID_DATA);
	} break;
}

in the engine.
Looks like you're giving it Array[Vector3] when it expects a PackedVector3Array. Likewise with Vector2s

    award That worked wonders! Thank you very much!