Modify scenes only on export and keep them original in project

Godot Version

4.5

Question

Hello everyone!
For my game for levels building I use @tool gdscript, which simplifiesthe creation of the levels. This tool script is connected to a bunch of nodes on each level.

The goal is to keep scripts at their nodes in editor, and export levels (scenes) and it’s nodes without scripts. So I need export script, which takes level on export, modifies it and exports without saving project files, but I stucked with it.

For now I wrote a plugin, which extends EditorExportPlugin:

@tool
extends EditorExportPlugin
var c : int = 0

func _export_begin(features: PackedStringArray, is_debug: bool, path: String, flags: int) -> void:  
	pass 

func _begin_customize_scenes(EEP : EditorExportPlatform, S : PackedStringArray) -> bool:
	return true

func _get_name() -> String:
	return "ExportCleanUpPlugin"

func _export_file(path: String, type: String, features: PackedStringArray) -> void:  
	if type == "PackedScene" and path.contains("res://Scenes/Levels/"):  
		var scene: PackedScene = load(path)  
		if scene:  
			var root: Node = scene.instantiate()  
			if root:  
				_strip_scripts(root)
				var modified_scene = PackedScene.new()
				modified_scene.pack(root)
				ResourceSaver.save(modified_scene, path)
				root.queue_free()
				print("Export: %s -> Deleted scripts: %d" % [path, c])
				c = 0

				
func _strip_scripts(node: Node) -> void:  
	if node:  
		var script = node.get_script()  
		if script and script.is_tool() and "BlockGenerator.gd" in script.resource_path:
			node.set("script", null)
			c += 1 
		for child in node.get_children():  
			_strip_scripts(child)

But it rewrites my project files (levels), and seems like doesn’t work (as I see error messages in console about this script).

I also tested _customize_scene (with _begin_customize_scenes returning true and implemented _get_customization_configuration_hash) but this method is called only if scene was changed since last export, and also doesn’t delete scripts for me. Am I missing something?

hello there, i think your trying to make a tool that removes your scripts that generate levels and removes them from the final game. if that is your goal, here is some possible problems i found:
The main issue is that you’re calling:

ResourceSaver.save(modified_scene, path)

Since path points over to res:/…, its saving the modified scene back into your project, which ends up over writing the original .tscn file.

you should not be saving the scene yourself, because the export customization API is intended for modifying the scene that’s being exported, and then letting Godot serialize that modified version into the export. but, saving it with ResourceSaver writes into the editor project, not the actual exported game.

i also reccomend using node.set_script(null) instead of node.set("script", null), since the first one seems to work better from my tests.

good luck with this!