MeshLibrary import with GDScript

Godot Version



I try to create a MeshLibrary with GDScript from a Blender file. So using a EditorScenePostImport script, I create a new MeshLibrary, add the meshes and save it. The script is executed without erros, but unfortunatelly the MeshLibrary seems still be empty, when I add it to a grid.

What is wrong in my script?

@tool # Needed so it runs in editor.
extends EditorScenePostImport

# This sample changes all node names.
# Called right after the scene is imported and gets the root node.
func _post_import(scene):
	var source = get_source_file()
	print_rich("Processing import for scene '" + + "' from '" + source + "'")
	var meshLib =
	var dest = source.replace('.blend', '.tres')
	print("Creating new MeshLibrary '" + dest + "'")
	iterate(scene, meshLib)
	var err =, dest)
	if err != OK:
		printerr("Error saving MeshLibrary: " + str(err))
		print("Saved MeshLibrary: " + dest)
	return scene

func iterate(node, meshLib):
	if node != null:
		if node.get_class() == "MeshInstance3D":
			add_mesh(node, meshLib)
		for child in node.get_children():
			iterate(child, meshLib)

func add_mesh(node, meshLib):
	var index = meshLib.get_last_unused_item_id()
	print_rich("Import Mesh: [b]%s[/b] at %d" % [, index])
	meshLib.set_item_mesh(index, node.get_mesh())