Search code examples
godotgdscript

How to force reimport of texture in godot?


I have a sample.png file which is being changed outside godot
and after it's modified, godot recives a signal and when that signal is received
I want that specific sample.png file to be reimported

I tried this answer but I want to reimport in my script itself not create a plugin for it
(atleast that's what I'm assuming it does)

I also tried this from the documents but I'm not sure how to use it exactly

EditorFileSystem.update_file("res://Assets/sample.png")

so how do I achieve the desired result?


Solution

  • The class EditorFileSystem is intended for plugins.

    You would call get_editor_interface() from an EditorPlugin (which would be where you would be writing code if you were making a plugin). And that gives you an EditorInterface object, on which you can call get_resource_filesystem() which gives you an EditorFileSystem object.

    So the intended use is something like this:

    extends EditorPlugin
    
    func example() -> void:
        var editor_file_system := get_editor_interface().get_resource_filesystem()
        editor_file_system.scan_sources()
        # editor_file_system.update_file("res://icon.png")
    

    By the way, EditorInterface also has a filesystem_changed signal. Although I don't know how reliable it is.

    Usually you don't have to do that. When you restore the Godot window, it will scan for changes in the project folder. So you might minimize Godot while you are working on something else and when you bring the Godot window back it will pick on the changes.

    In practice, the only situations when I had to use scan or scan_sources was when I had a tool script that write a resource file which should be imported, and I wanted it to reflect right away.


    Instead of making a custom plugin, I'll remind you that form a tool script (as long as it is running in the editor) you can simply create an EditorPlugin object. For example:

    var ep = EditorPlugin.new()
    ep.get_editor_interface().get_resource_filesystem().scan()
    ep.free()
    

    I had also shared this example in another answer I wrote for you a while back here, it is under the title "About saving resources from tool scripts".