Can I take a screenshot of only a certain area of my scene?

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

I’m taking a screenshot of the scene using:
get_viewport().get_texture().get_data()

and i want to cut a part of the scene for the final screenshot, or maybe take the screenshot of only an specific node and his children.

This is my scene:

And i want to take an screenshot only of this part of the scene:

Edit: I figured out that i can have multiple ViewPorts in one scene, but i’m struggling to make the second one render the scene that i’m looking.

:bust_in_silhouette: Reply From: Yuminous

Yes!

Viewport nodes are a good option, but you can actually just extend get_viewport().get_texture().get_data() to include .get_rect( -Rect2- ), where “Rect2” is the region that you specify (beforehand).

Rect2(x: float, y: float, width: float, height: float)

So just for example:

var region = Rect2(83, 225, 480, 296)  # change the values around to suit your actual scene
var image = get_viewport().get_texture().get_data().get_rect(region)
image.flip_y()
image.save_png("screenshot.png")

Also in your screenshots you have a little thumbnail of the screenshot and you can do that in various ways, but I have a basic example if you’re looking for inspiration:

# do this in the main (or visible) scene

sprite = Sprite.new()
var texture = ImageTexture.new()
texture.create_from_image(image, 1)
sprite.set_texture(texture)

sprite.position = Vector2(700, 200)
sprite.scale = Vector2(0.3, 0.3)
$Node.add_child(sprite)

Just in order to test all that code I made a quick little scene with your screenshots (the thumbnail was actually generated in the scene):

Screenshot! Cheese~

The flash and fade effects were also extras I added but pretty easy to do as well (animate the modulate property of a white box and the thumbnail).

Your game looks really nice, by the way. I like it!
Good luck!

Damn, that was exactly what i needed, i was trying to create an customized viewport, and duplicating nodes, and all that. And you just came with the solution in my face, Thanks!
:smiley:

RafaelGomes | 2021-07-21 20:17