Is it impossible to read images from an editor script?

I have tried with both the built-in image.load and with with GitHub - britzl/defold-png: Defold native extension to load and save PNG images, but both yield an error about trying to index nil - either image isn’t defined or png isn’t defined, so their various methods aren’t available.

I guess theoretically I could use a separate tool, like a go binary or something I could build and embed in the project to read png files, but that seems a bit superfluous to do in a system that obviously reads png files as resources internally.

Example:

local function read_tile_image(file_path)
    local file = io.open(file_path, "rb")
    if not file then
        error("can't load tilemap image:", file_path)
    end
    local bytes = file:read("*a")
    -- @TODO: apparently editor scripts can't do this?
    -- local img = image.load_buffer(bytes)
    -- if not img then
    --     error("can't load tilemap image:", file_path)
    -- end
    -- return img
    -- @TODO: this also doesn't work in an editor script:
    -- local buffer, width, height = png.decode_rgba(bytes, true)
    -- local pixels = buffer.get_stream(buffer, hash("pixels"))
    -- return { pixels, width, height }
end

Editor scripts do not have access to any of the runtime APIs of Defold. This is why image.load() is not working. You will only have access to the standard Lua APIs and the Editor Lua API (API reference (Editor)).

Thanks, that’s pretty much what I assumed. But reading through the editor API, I wasn’t able to find anything that looked like it could read image data.

I’ll open a github issue w/ this as a feature request.

1 Like