How to render viewport content without adding to Scene tree?

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

I am trying to render something into a off screen viewport and take a screenshot of it by doing something like this:

func _ready() -> void:
    var viewport =
    viewport.size = Vector2(1000,1000)
    viewport.usage = Viewport.USAGE_2D
    viewport.render_target_update_mode = Viewport.UPDATE_ONCE

    #add some content
    var poly =
    poly.polygon = [Vector2(20,20), Vector2(100,20), Vector2(60,60)]
    poly.color = Color(1,1,1,1)

    add_child(viewport) #removing this line will result in empty screenshot
    yield(VisualServer, "frame_post_draw")

    var img = viewport.get_texture().get_data()

But when I don’t do add_child(viewport), screenshot is empty. I guess render is not initiated unless added to scene tree. Is there a way to render the viewport without adding it as a child?

:bust_in_silhouette: Reply From: Calinou

I don’t think you can render a viewport to a texture without adding it to the scene tree. However, I think the viewport might be able to render its contents even if you place it outside the camera’s bounds. (If objects disappear when you do this, you may have to extend their visibility/culling margins.)