How to crop screenshots around a node

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

Hi,

I am having some trouble cropping images around a node (colo[u]rRect) for a PBR material painting tool (just trying to implement drawing on a flat canvas right now).

I have looked at the gdpaint demo, tested it, and it worked, so I used similar code, however it gave me a segment of the colo[u]rRect, as well as a grey strip (the bg) on the side/top, depending on the scale of the window.

my code is:

onready var IMAGE_SIZE = Vector2(500, 500) # the size of the colourRect

func _save_img(path):
    yield(VisualServer, "frame_post_draw")
    var img = get_viewport().get_texture().get_data()
    img = img.get_rect(Rect2(canvas.get_global_rect().position, IMAGE_SIZE))
    img.flip_y()
    img.save_png(path)

All I need to work out is how to crop the screenshot to a ColourRect.

:bust_in_silhouette: Reply From: Snail0259

Okay, so I found the answer. Here it is in case someone needs it. You can get it by saving a viewport with the nodes you want inside it to an imagetexture, and then save it to disk.