Lua in Darktable issue, dt.gui.create_job: Unexpected table argument

Hello everyone,

I’m seeking assistance for an issue I encountered while developing scripts for Darktable. Specifically, I’m facing an error with the darktable.gui.create_job function that I can’t seem to figure out.

I’m using the darktable.gui.create_job function to create and execute a job, passing a simple string as the title for the job. darktable lua documentation - darktable.gui.create_job

This is my example code:

local dt = require "darktable"

local obj = {"Value 1", "Value 2", "Value 3"}

local do_job = function(obj)
    -- Add debugging output to indicate the start of the function
    dt.print_error("Starting do_job function")

    local job = dt.gui.create_job {
        "Do something", -- String
    }

    -- Add debugging output to indicate the start of the job
    dt.print_error("Job created, beginning processing of objects...")

    for _, value in ipairs(obj) do
        -- Add debugging output to indicate the current value
        dt.print_error("Current value:", value)
    end

    -- Add debugging output to indicate the end of the function
    dt.print_error("End of do_job function")

    job = nil
end

-- Test the do_job function
do_job(obj)

--[[ output:
     2.4613 LUA ERROR Starting do_job function
     2.4614 LUA ERROR: dtutils.lua: prequire: 224: Error loading myscripts/dp_lua_test 
     2.4614 LUA ERROR: dtutils.lua: prequire: 225: Error returned is /home/yves/.config/darktable/lua/myscripts/dp_lua_test.lua:9: bad argument #1 to 'create_job' (string expected, got table) 
     2.4615 LUA ERROR: script_manager.lua: activate: 408: Error loading myscripts/dp_lua_test 
     2.4615 LUA ERROR: script_manager.lua: activate: 409: Error message: /home/yves/.config/darktable/lua/myscripts/dp_lua_test.lua:9: bad argument #1 to 'create_job' (string expected, got table) 

]]

The error occurs when calling the darktable.gui.create_job function, indicating that a table is being passed instead of a string. However, I’ve verified that I’m explicitly passing a string.

Interestingly, the code is derived from a working script that also uses the darktable.gui.create_job function:

What affects there? I have no idea. I use Darktable 4.6.1 on Ubuntu 22.04

I would appreciate any assistance or guidance on resolving this issue and successfully running my script.

Thank you in advance for your support!

I think you have to replace the curly brackets with regular brackets. Otherwise you are indeed passing a table as argument.

2 Likes

Oh god, I’m a blind Lua beginner :face_with_peeking_eye:

Thank you very much, I won’t be able to test it until later, but it seems promising :smiley:

You can also use dt.print_log() for printing debugging statements and use dt.print_error() for when errors occur.

I start Darktable using darktable -d all | grep -i 'error' so that I don’t get the whole flood of output. That’s why I got used to it.

I am still practicing with Lua and Darktable :grin:

I have another question about the current job. Since I move images there, is the job saved when Darktable is closed by the user?

I call a destroy function in the cancel_callback an at the end of the job to writes a log and ultimately sets job.valid to false.

You can start darktable with darktable -d lua and just get the Lua messages.

jobs are used to provide user feedback that something is happening. Let’s say that you process 10 images in a loop and the whole process takes .1 sec. To the user it takes no time at all. Now let’s say it’s 1000 images and it takes 10 seconds. The use sits there wondering what is happening. If I wrap that loop in a job and move the slider every 100 images then the user understands that something is happening and they can track the progress.

1 Like

Thank you. So a pure GUI element. Then I will try to make the process secure by watching darktable.control.ending so that my log will be written at the end as well.

Maybe I’m doing something wrong, but for me, the output of darktable -d lua is so long that I can’t find anything without using grep.

Catch the exit event instead

dt.register_event(MODULE, "exit", 
  function(event)
    dt.print_log("got exit signal")
    stop_job()
  end
)

The stop job function sets job.valid to false.

Comment out some of your print statements :smile:

1 Like