+1 vote

I'm taking a screenshot of the scene using:
getviewport().gettexture().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:
https://prnt.sc/1dmg2mz
And i want to take an screenshot only of this part of the scene:
https://prnt.sc/1dmgdsx

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.

Godot version v3.3.2 stable
in Engine by (41 points)
edited by

1 Answer

+3 votes
Best answer

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!

by (992 points)
selected by

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!
:D

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.