How to fast generate MeshInstance in code(GDScript) in mode tool?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By timur_star

I have a piece of code that creates a MashInstance every time it is called.

func _on_init_block(idx):
	var block=current_layer.get_block(idx)
	#Logger.info(block.get_modelname())
	var mesh_instance=block.get_mesh()
	mesh_instance.set_transform(transform)
	add_child(mesh_instance)
	

This function returns the already created MashInstance and adds to the stage what would be displayed. This function is a slot for some signal that the scene generates. So far, the slot has been called about 5000 times. After a while, the scene starts to freeze at the moment when this slot is called and the processor is very heavily loaded (1 core). I found out that the hang occurs when the add_child method is called. I checked in normal mode after compilation and in tol mode. This behavior occurs only in tool mode when drawing is in the editor itself. Why is this happening and how can I solve my problem?
I evaluated the dependence of the call time of the add_child function on the number of calls and I got an almost exponential dependence. Not tool mode(execute application), the execution time of add_child is constant and has a small value.

You can try using MultiMeshInstance for this. It’s not as straightforward but it can achieve much better performance than thousands of individual MeshInstances

umaruru | 2023-05-29 20:38

MultiMeshInstance allows you to quickly create multiple instances of the same mesh. I have a large number of models of different shapes.

timur_star | 2023-05-30 09:23

Sorry, I thought it was thousands instances of the same mesh. In this case one of the solutions would be bypassing the scene system and using servers directly. Take a read on this article: Optimization using Servers — Godot Engine (4.0) documentation in English

umaruru | 2023-05-30 17:18