-- Add current timeline to render queue
-- Usage: Run this script from the Scripts menu in DaVinci Resolve

-- Get the Resolve instance
resolve = Resolve()

-- Get the project manager
projectManager = resolve:GetProjectManager()
if not projectManager then
    print("Failed to get project manager")
    return
end

-- Get the current project
project = projectManager:GetCurrentProject()
if not project then
    print("No project is currently open")
    return
end

-- Get the current timeline
local timeline = project:GetCurrentTimeline()
if not timeline then
    print("No timeline is currently open")
    return
end

-- Get export directory from environment variable
local exportFolder = os.getenv("DAVINCI_EXPORT_DIRECTORY")
if not exportFolder then
    print("DAVINCI_EXPORT_DIRECTORY environment variable is not set")
    return
end

-- Get timeline name
local timelineName = timeline:GetName()
print("Adding timeline to render queue: " .. timelineName)

-- Set render settings for MP4
local renderSettings = {
    ["TargetDir"] = exportFolder,
    ["CustomName"] = timelineName,
}

-- Set the render settings
if not project:SetRenderSettings(renderSettings) then
    print("Failed to set render settings")
    return
end

-- Add render job
local jobId = project:AddRenderJob()
if not jobId then
    print("Failed to add render job")
    return
end

print("Successfully added timeline to render queue")
print("You can start rendering from the Deliver page")

-- Open the Cut page
if not resolve:OpenPage("cut") then
    print("Failed to open Cut page")
    return
end 