How to view SurfaceTool generated mesh in editor correctly?

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

Hello,
I’m wondering if there is a good way to view GDScript’s SurfaceTool generated mesh in the editor for easier visual manipulation/placement of that procedurally generated mesh.

Adding ‘tool’ to the top of the GDScript file that generates the mesh for a MeshInstance node appears to somewhat work by causing the script to be executed in editor each time the scene file is opened. But this appears to cause the generated mesh to be stored in the .tscn file when the scene is saved, which makes the file unnecessarily big.

I can manually clear the MeshInstance’s Mesh field every time I’m saving the scene but feels like there should be a better way that I’m missing…
Thanks.

:bust_in_silhouette: Reply From: Toshio Araki

I think it depends on how you structured your code, are you triggering the mesh generation at func _ready? Then it’s normal.
If not, I’d say paste here some of your code, so that I can help more.
I can tell you that in my case I used properties (export, with onready), and used them to trigger the mesh generation function only when value is changed (using setget), so, for example:

tool
extends Spatial

export(bool) onready var trigger_generation= false setget GenerateMesh


func GenerateMesh(_newval):
	if(_newval != trigger_generation):
		callMeshGenerationFunction()

Thank you for your suggestion.
Yes, I’m generating the mesh in _ready() and it works for the game, but I was hoping to generate it at a point where I can also see the mesh inside the editor. Below is an example of how I’m trying to generate a simple triangle in code. It shows up in the game but inside the editor it just shows up as an empty node without the triangle mesh. Enabling procedurally generated mesh to show up in editor would make it easy to place it correctly relative to non-procedurally generated meshes so that’s what I’m hoping to achieve.
Thanks.

extends MultiMeshInstance

func _ready():
	var st = SurfaceTool.new()
	st.begin(Mesh.PRIMITIVE_TRIANGLE_STRIP)

	var pts = [
			Vector3(0,0,0),
			Vector3(1,-2,0),
			Vector3(-1,-2,0),
		]

	for i in range(pts.size()):
		st.add_vertex(pts[i])

	multimesh.mesh = st.commit()
	multimesh.instance_count = 1
	for i in range(multimesh.instance_count):
		multimesh.set_instance_transform(i, Transform())

dev.monkey | 2020-05-11 18:01