A MultiMesh that uses a MeshLibrary

(Godot 4.3+ But might work on 4.1 and 4.2)

A Normal Multimesh

meshMultiMeshMultiMeshInstance3D Node

The mesh is a reference and does not bloat the MultiMesh with its various data arrays; that stuff all stays inside the mesh.

Using a MeshLibrary

mesh, mesh, mesh (Scene → Export As) → MeshLibraryMultiMeshGridMap

That’s the GridMap way, but we can be cheeky and employ the MeshLibrary for other things.

Fusing the Two

One fetches a mesh out of a library by its id, which is just an index from 0 into an array. Something like var mesh = meshlibref.get_item_mesh(id).

So, what we need is a subclass of MultiMesh which has a ref to a MeshLibrary as well as an id.

An aside here. Because the mesh thus fetched is stored in a variable (in RAM) it is divorced from the MeshLibrary where it started. If one where to put it into the MultiMesh.mesh property, the entire mesh would be stored (vertices, normals, materials, all of it) inside your new resource, upon saving.

This is silly because now you have two copies of the same mesh, on in the library and one in your new resource. So, we need to do some voodoo to ensure that the mesh’s data is not stored inside this new resource class we are making.

Because it’s defined in MultiMesh, we have little say over the mesh property, but there’s a neat gdscript command _validate_property which is the voodoo we need! With that in hand, we can tell mesh to never save anything.

There’s a small hack in the _init func to deferr a call to _resource_ready, our own func that does the actual get_item_mesh call. This is because when _init first runs, all the @export properties are not yet available; they are all null, wich is not very helpful. I learned about this hack here:

With all that in hand, the code below let’s you do:

MeshLibraryMultiMeshLibraryMultiMeshInstance3D Node

HTH

The Code

All mistakes are mine.

@tool
class_name MultiMeshLibrary extends MultiMesh

## A subclass of [MultiMesh] to use meshes from a MeshLibrary by index(id)
## It will not save the mesh retrieved within this resource, that remains
## in the MeshLibrary. Should be transparent in use, unless bugs.

# MIT License
# Copyright (c) 2024 Donn Ingle (donn.ingle@gmail.com)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

## The MeshLibrary you want to use.
@export var meshlib : MeshLibrary

## The index into the library to retrieve.
@export var id : int


# This func forces the `mesh` NOT to save!
# NB `mesh` lives in MultiMesh and I can't do much to it,
# but this _validate_property seems to work!
func _validate_property(property: Dictionary):
	if property.name == "mesh":
		property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT | PROPERTY_USAGE_NO_INSTANCE_STATE


# When this runs, the export vars are NOT YET SET
# This is crazy, but hey. So I have to use this trick:
# https://github.com/godotengine/godot-proposals/issues/296#issuecomment-675208185
func _init() -> void:
	call_deferred("_resource_ready")


func _resource_ready():
	# By now the meshlib is loaded into RAM. I hope...
	# So, set the mesh!
	mesh = meshlib.get_item_mesh(id)
2 Likes