From 2c986f5b1b8bb94675c90a61a76be1b3c84ae14e Mon Sep 17 00:00:00 2001 From: Ben Smith Date: Wed, 21 Sep 2022 11:18:12 +0100 Subject: [PATCH] Bug fix for children of folder tracks --- Routing/Toggle routing and solo to master.lua | 83 +++++++++++++++---- 1 file changed, 69 insertions(+), 14 deletions(-) diff --git a/Routing/Toggle routing and solo to master.lua b/Routing/Toggle routing and solo to master.lua index 67bf2d5..72af1d9 100644 --- a/Routing/Toggle routing and solo to master.lua +++ b/Routing/Toggle routing and solo to master.lua @@ -4,9 +4,9 @@ @link Author https://www.bensmithsound.uk Repository https://github.com/bsmith96/Reaper-Scripts - @version 1.3 + @version 1.4 @changelog - # Added user identifier to provided file names + # Bug fix: successfully routes tracks in folders to master by routing and soloing the folder too @metapackage @provides [main] . > bsmith96_Toggle routing and solo to master.lua @@ -20,12 +20,67 @@ ]] +-- ========================================================= +-- ================= GLOBAL VARIABLES ==================== +-- ========================================================= + +local r = reaper + +-- get the script's name and directory +local scriptName = ({r.get_action_context()})[2]:match("([^/\\_]+)%.lua$") +local scriptDirectory = ({r.get_action_context()})[2]:sub(1, ({r.get_action_context()})[2]:find("\\[^\\]*$")) + + +-- ========================================================= +-- ====================== FUNCTIONS ====================== +-- ========================================================= + +function getParent(track) + parents = {} + + parent = r.GetParentTrack(track) + + while( parent ~= nil ) + do + table.insert(parents, parent) + parent = r.GetParentTrack(parent) + end + return parents +end + +function toggleRouteAndSolo(track) + if r.GetMediaTrackInfo_Value(track, "B_MAINSEND") == 1 then + r.SetMediaTrackInfo_Value(track, "B_MAINSEND", 0) + r.SetMediaTrackInfo_Value(track, "I_SOLO", 0) + else + r.SetMediaTrackInfo_Value(track, "B_MAINSEND", 1) + r.SetMediaTrackInfo_Value(track, "I_SOLO", 1) + end +end + + +-- ========================================================= +-- ====================== UTILITIES ====================== +-- ========================================================= + +-- Deliver messages and add new line in console +function dbg(dbg) + r.ShowConsoleMsg(tostring(dbg) .. "\n") +end + +-- Deliver messages using message box +function msg(msg, title) + local title = title or scriptName + r.MB(tostring(msg), title, 0) +end + + -- ============================================== -- =============== MAIN ROUTINE =============== -- ============================================== -- count selected tracks -selectedCount = reaper.CountSelectedTracks(0) +selectedCount = r.CountSelectedTracks(0) -- create a table for selected tracks tracks = {} @@ -33,22 +88,22 @@ tracks = {} -- get selected tracks for i = selectedCount, 0, -1 do - track = reaper.GetSelectedTrack2(0, i, false) + track = r.GetSelectedTrack2(0, i, false) table.insert(tracks,track) end -reaper.Undo_BeginBlock() - +r.Undo_BeginBlock() -- toggle master routing + solo for _, eachTrack in ipairs(tracks) do - -- toggle both based on the current state of the master send - if reaper.GetMediaTrackInfo_Value(eachTrack, "B_MAINSEND") == 1 then - reaper.SetMediaTrackInfo_Value(eachTrack, "B_MAINSEND", 0) - reaper.SetMediaTrackInfo_Value(eachTrack, "I_SOLO", 0) - else - reaper.SetMediaTrackInfo_Value(eachTrack, "B_MAINSEND", 1) - reaper.SetMediaTrackInfo_Value(eachTrack, "I_SOLO", 1) + eachParents = getParent(eachTrack) + -- toggle based on the current state of the master send + toggleRouteAndSolo(eachTrack) + -- toggle all parent tracks + if eachParents[1] ~= nil then + for __, eachParent in ipairs(eachParents) do + toggleRouteAndSolo(eachParent) + end end end -reaper.Undo_EndBlock("Route tracks to master", -1) \ No newline at end of file +r.Undo_EndBlock(scriptName, -1) \ No newline at end of file