Plugin to Automate Import Config Causing Error

Godot Version

v4.5.1

Issue

Hello devs, I’m currently trying to automate my animation import process. I want animations Saved to File and Keep Custom Tracks enabled by default, as locating each new animation to enable keep custom tracks every single time I want to add a new set of animation(s) kinda sucks. To do this, I have created a simple plugin that loads a script, following the solution to this post → Automatically set “keep_custom_tracks” on imported animations

The imported file has the plugin’s main script path given under Import Script; however, when I reimport the file, it throws the following error:

Script is not a subtype of EditorScenePostImport

Changing the loaded script extends to EditorScenePostImport breaks it, so I don’t know where to go. The main script is import.gd:

@tool
extends EditorPlugin

var process_plugin

func _enter_tree() -> void:
	process_plugin = preload("res://addons/anim_importer/post_import.gd").new()
	add_scene_post_import_plugin(process_plugin)

func _exit_tree() -> void:
	remove_scene_post_import_plugin(process_plugin)
	process_plugin = null

And the loaded script, post_import.gd is as follows:

@tool
extends EditorScenePostImportPlugin

func _get_import_options(path: String) -> void:
	add_import_option_advanced(TYPE_BOOL,"animation/save_animations_to_file", false, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED)
	add_import_option_advanced(TYPE_STRING, "animation/animations_dir", "", PROPERTY_HINT_DIR)
	add_import_option("animation/keep_custom_tracks", false)


func _get_option_visibility(path: String, for_animation: bool, option: String) -> Variant:
	var save_animations = get_option_value("animation/save_animations_to_file")
	match option:
		"animation/animations_dir":
			return save_animations
		"animation/keep_custom_tracks":
			return save_animations

	return true


func _pre_process(scene: Node) -> void:
	var original_out  = get_option_value("_subresources")
	var subresources = {}
	iterate(scene, subresources)

	var animations = subresources.get("animations", [])
	if not animations.is_empty():
		var new_out = {} 
		new_out["animations"] = {}
		for animation in animations:
			new_out["animations"][animation] = {
				"save_to_file/enabled": true,
				"save_to_file/path": original_out["animations"][animation]["save_to_file/path"],
				"save_to_file/keep_custom_tracks": true}
		original_out = new_out


func iterate(node:Node, subresources:Dictionary) -> void:
	if node == null: return

	if node is AnimationPlayer:
		if not subresources.has("animations"):
			subresources["animations"] = []
		var animations = subresources["animations"]
		animations.append_array(node.get_animation_list())

	for child in node.get_children():
		iterate(child, subresources)

As mentioned, this is near identical to the solution in the linked topic.

What do I do next? Any help is appreciated.

Did you enable the plugin you created?

Yes. I have also tried disabling and re-enabling, I have tried restarting godot, and I still have the same error.

I tried this recently too, couldn’t get it to work and gave up and went back to import scripts attached to objects. Might be a bug. If no one comes up with a solution here, I recommend logging it as a bug.

It works fine on my end. This is a plugin so you don’t have to put anything in the Import Script field.

When it’s enabled and you select a 3D model you should have an option Save Animations to File under the Animation category in the Import dock. If you enable it you should see two extra options for the directory and the keep custom tracks.

1 Like

Finally got back to this after a couple days. The custom tracks were saved for awhile, until the glb file changed, and it got rid of the custom tracks again.

I’ve changed the options in the import dock, and I’ll try again. I’ll share my results here.

Edit: I should note that during all of this, the animation tab at the bottom has the ‘imported scene’ warning, even when it was saving the tracks.

Video showcase of what exactly is going on. I don’t know if I’m missing something crucial or if this is a bug, as mentioned above.

I’m not sure, it may be a bug but I can’t reproduce it. It’s working fine for me.

Are you modifying the animations after reimporting the asset with the keep custom tracks setting enabled? If you modify them before that value is set then the custom tracks won’t be saved.

Unless the import settings change or revert on reimport, which would be quite irritating, no, I’ve enabled it and kept it on before and after each import, and it still removes the tracks.