Last active
February 7, 2018 01:38
-
-
Save iluvcapra/098fda75a6102dfe12d42e35b00bf791 to your computer and use it in GitHub Desktop.
A REAPER script for automatically editing backgrounds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
I, Jamie Hardt, the author, hereby release this work to the public domain. | |
This script reads regions created by the "Import Video Cuts from EDL.lua" script and | |
attempts to intelligently loop regions to fill them. | |
]] | |
function eachMarker(proj) | |
local i = 0 | |
local retval, markerCount, regionCount = reaper.CountProjectMarkers(proj) | |
return function () | |
i = i + 1 | |
if i < markerCount + regionCount then | |
local rval, isregion, startTime, endTime, name, regionIndexNum = reaper.EnumProjectMarkers(i,proj) | |
return { isRegion = isregion, startTime = startTime, endTime = endTime, name = name, num = regionIndexNum, globalIndex = i } | |
end | |
end | |
end | |
function eachRegion(proj) | |
local markerIter = eachMarker(proj) | |
return function () | |
while true do | |
local m = markerIter() | |
if m then | |
if m.isRegion then return m end | |
else | |
break | |
end | |
end | |
end | |
end | |
function sceneRegionsByTime(proj) | |
local sceneRegions = {} | |
for region in eachRegion(0) do | |
if region.name and sceneForRegionName(region.name) and region.num > 1000 then | |
table.insert(sceneRegions,region) | |
end | |
end | |
local timeSort = function (a,b) | |
return a.startTime < b.startTime | |
end | |
table.sort(sceneRegions, timeSort) | |
return sceneRegions | |
end | |
function sceneForRegionName(regionName) | |
return regionName:match("V?(%a*%d+)") | |
end | |
function eachScene(proj) | |
local sceneRegions = sceneRegionsByTime(proj) | |
if #sceneRegions == 0 then | |
return nil | |
end | |
local i = 1 | |
local lastStart = sceneRegions[1].startTime | |
local lastEnd = sceneRegions[1].endTime | |
local lastSceneName = sceneForRegionName(sceneRegions[1].name) | |
return function () | |
local retScene, retStart, retEnd = nil, nil, nil | |
while i < #sceneRegions do | |
i = i + 1 | |
local thisRegion = sceneRegions[i] | |
if thisRegion then | |
local thisSceneName = sceneForRegionName(thisRegion.name) | |
if thisSceneName ~= lastSceneName then | |
retScene, retStart, retEnd = lastSceneName, lastStart, lastEnd | |
lastStart = thisRegion.startTime | |
lastEnd = thisRegion.endTime | |
lastSceneName = thisSceneName | |
break | |
else | |
lastEnd = thisRegion.endTime | |
end | |
else | |
return retScene, retStart, retEnd | |
end | |
end | |
return retScene, retStart, retEnd | |
end | |
end | |
function selectedMediaItems(proj) | |
local retval = {} | |
for i = 0, reaper.CountSelectedMediaItems(proj) do | |
local thisItem = reaper.GetSelectedMediaItem(proj,i) | |
table.insert(retval, thisItem) | |
end | |
return retval | |
end | |
function sceneForRegionName(regionName) | |
return regionName:match("V?(%a*%d+)") | |
end | |
function getRegionAtTime(project, time) | |
local lastMarker , lastRegion = reaper.GetLastMarkerAndCurRegion(project, time) | |
local rval, isRegion, startTime, endTime, name, regionIndex = reaper.EnumProjectMarkers(lastRegion) | |
return startTime, endTime, name, regionIndex | |
end | |
function getRegionAtCursorPosition() | |
return getRegionAtTime(0, reaper.GetCursorPosition() ) | |
end | |
function getSceneAtCursorPosition() | |
local startTime, endTime, name, regionIndex = getRegionAtCursorPosition() | |
return sceneForRegionName(name) | |
end | |
sceneToFill = getSceneAtCursorPosition() | |
itemsToUse = selectedMediaItems(0) | |
if #itemsToUse == 0 then | |
reaper.ShowMessageBox("You must selected media items to fill the scene.","Select Media Items",0) | |
return | |
end | |
if #sceneToFill == nil then | |
reaper.ShowMessageBox("The selected area does not have a scene defined in the region list.",0) | |
return | |
end | |
for scene, startTime, endTime in eachScene(0) do | |
if scene == sceneToFill then | |
-- reaper.ShowConsoleMsg("Will set") | |
for k, item in pairs(itemsToUse) do | |
reaper.SetMediaItemPosition(item,startTime, false) | |
reaper.SetMediaItemLength(item,endTime - startTime,false) | |
end | |
end | |
reaper.UpdateArrange() | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment