The issue with your Lua script arises because the method clip:SetEnd(end_time) does not properly adjust the clip's endpoint to match the marker's end time. In DaVinci Resolve's API, modifying a clip's in and out points on the timeline requires the use of the SetStartTrim and SetEndTrim methods of the TimelineItem object.
Here's why your script isn't working as intended:
-
Incorrect Method Usage: The method SetEnd is not the correct method to adjust a clip's end point on the timeline. Instead, SetEnd is typically used to set the end timecode of a media pool item, not to trim a clip on the timeline.
-
Not Adjusting Start Times: Your script only attempts to adjust the end time of clips that extend beyond the marker's end time. However, clips that start before the marker's start time also need to be adjusted so that the compound clip includes only the content within the marker's duration.
-
Units Mismatch: Ensure that the time units you're using (start_time, end_time, clipStart, clipEnd) are all consistent and in frames, as required by the SetStartTrim and SetEndTrim methods.
Solution:
Modify your script to use SetStartTrim and SetEndTrim to adjust the clips' in and out points to match the marker's start and end times before creating the compound clip. Here's the corrected code:
local resolve = Resolve()
local projectManager = resolve:GetProjectManager()
local project = projectManager:GetCurrentProject()
local timeline = project:GetCurrentTimeline()
local markers = timeline:GetMarkers()
for frameId, marker in pairs(markers) do
local start_time = frameId
local duration = marker.duration
local end_time = start_time + duration
local name = marker.name
local items = {}
local trackType = "video"
local trackCount = timeline:GetTrackCount(trackType)
for trackIndex = 1, trackCount do
local clips = timeline:GetItemListInTrack(trackType, trackIndex)
for _, clip in pairs(clips) do
local clipStart = clip:GetStart()
local clipEnd = clip:GetEnd()
if clipEnd > start_time and clipStart < end_time then
-- Adjust clip to fit within the marker's duration
if clipStart < start_time then
clip:SetStartTrim(start_time)
end
if clipEnd > end_time then
clip:SetEndTrim(end_time)
end
table.insert(items, clip)
end
end
end
if #items > 0 then
local compoundClip = timeline:CreateCompoundClip(items)
compoundClip:SetName(name)
end
end
Explanation of Changes:
-
Used SetStartTrim and SetEndTrim: These methods adjust the in and out points of the clip on the timeline.
clip:SetStartTrim(start_time) trims the clip so that it starts at start_time.
clip:SetEndTrim(end_time) trims the clip so that it ends at end_time.
-
Adjusted Both Start and End Times: Now the script checks if the clip starts before the marker's start time or ends after the marker's end time and trims accordingly.
-
Order of Operations: Moved the table.insert(items, clip) after the clip has been trimmed to ensure that the items added to the compound clip are already adjusted.
Additional Notes:
-
Consistency in Time Units: Make sure that all time values (start_time, end_time, clipStart, clipEnd) are in frames, which is the expected unit for these methods.
-
Non-Destructive Editing: Trimming clips in this way is non-destructive within the timeline context and doesn't affect the source media.
-
Testing: After making these changes, test the script with your timeline to ensure that the compound clips are created accurately according to the markers.
By correctly trimming the clips to match the marker's in and out points before creating the compound clip, your script should now work as intended.
Reference: For more information on the methods used, you can refer to the DaVinci Resolve Developer Documentation, specifically the TimelineItem class methods SetStartTrim and SetEndTrim.