• 3D
  • How can I get image data from a script?

I have an ImageTexture variable, and am trying to get the color from it at a certain position. I found I can get the Image with:

heightmap.get_data()

and there's a function to get a pixel:

heightmap.get_data().get_pixel(64,64)

but if I try it, I get this error:

Image must be locked with 'lock()' before using get_pixel().

so I tried putting this line before get_pixel():

heightmap.get_data().lock()

but nothing changed. Do you have any idea why?

Try this.

var image := heightmap.get_data()
image.lock()
image.get_pixel(some, numbers)

Note that Godot 4.0 won't require locking/unlocking images anymore.