exporting contents of viewport as a png results in a black image

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

im trying to recreate iron lung’s submarine mechanic by take a picture of what a camera sees by exporting the contents of a viewport. my stucture is:

  • kinematic body 3d
  • collision shape
  • node3d
  • a bunch of mesh instances to make the shape of the submarine the player is inside
  • viewport
  • camera (with script attached)

the script is:

extends Camera

func _ready():
    takePhoto()

func takePhoto():
    var vpt = get_viewport()
    var txt = vpt.get_texture()
    var image = txt.get_data()
    image.flip_y()
    image.save_png("break.png")

break.png appears but it is just a black image. i tried attaching the script to the viewport instead but it gave me the same result. sorry if this is a bad question, this is my first time using viewports

:bust_in_silhouette: Reply From: rellekd

Fixed it! just dont put the call inside the ready function and put it inside a procces function that calls takePhoto when a button is pressed