Trying to add Thumbnail Previews for 3D Models to the FileSystem Dock

Godot Version

4.2.1

Question

Hi, I’ve been experimenting with ChatGPT to help generate a plugin that creates thumbnails previews for 3D models in the FileSystem Dock. It’s totally not my area of knowledge, but I thought I’d give it a go for fun. It’s an important feature currently missing for Godot and until the devs implement it I thought it might be useful to to make a quick plugin.

The script that was generated appears to be logical, however I’ve no idea if this is the correct way to generate the thumbnails for 3D models in the FileSystem dock, but anyhu :wink:

I’m getting a few errors though, some I think are to do with the GDScript being for V3 of Godot instead of v4. For example the Yield statement changing to Await. But there’s other issue’s, such as the signal at the start. Plus I’m surprised that the script is so short - so it might just be useless.

I’ve only ever thinkered with ChatGPT for small bits of code and it’s typically hit and miss as I’m sure you’re aware.

Anyway, here’s the plugin script that was generated:

@tool
extends EditorPlugin

func _enter_tree():
	connect("resource_pre_save", self, "_on_resource_pre_save")

func _on_resource_pre_save(resource):
	if resource is MeshInstance:
		var model = resource
		var viewport = Viewport.new()
		var camera = Camera.new()
		var light = OmniLight.new()
		
		viewport.add_child(camera)
		viewport.add_child(light)
		viewport.render_target_v_flip = true
		viewport.size = Vector2(64, 64)
		
		model.add_child(viewport)
		model.show()
		
		yield(get_tree(), "idle_frame")
		yield(get_tree(), "idle_frame")
		
		var image = viewport.get_texture().get_data()
		var thumbnail = ImageTexture.new()
		thumbnail.create_from_image(image)
		
		model.set("thumbnail/icon", thumbnail)
		model.remove_child(viewport)
		viewport.queue_free()


*In the description it says:*
This code connects the "resource_pre_save" signal to a function called "_on_resource_pre_save". When this function is called, it checks if the resource is a MeshInstance. If it is, it creates a Viewport, Camera, and Light, adds them to the Viewport, and sets up the Viewport to render the MeshInstance.

Then, it yields to allow the rendering to happen. Once the rendering is complete, it retrieves the rendered image, creates a thumbnail texture from the image, and sets the thumbnail texture as the icon for the MeshInstance.

    Finally, make sure to add your plugin to the list of enabled plugins in the project settings.

After completing these steps, your plugin should be able to generate thumbnail icons for 3D models in the File System dock in Godot Engine version 4.

Anyone want to see if they can fix it?```

Resource preview generation is available with the EditorResourcePreview singleton and EditorResourcePreviewGenerator class.

Could you elaborate further as I’m not familiar with this script, as I said it was ai generated. I’ve never scripted plugins for Godot before and I’m not sure even if it will work :wink: