Old 05-15-2021, 07:59 PM   #1
cjacko13
Human being with feelings
 
Join Date: Apr 2021
Posts: 4
Default Copy Single Item to Multiple Tracks

Hi Forum,

I did a search, but couldn't find an existing thread that covers this. Is there a way to copy a single item and paste it to multiple tracks? I know I can select the item and cmd+drag it multiple times (or cmd+c, cmd+v), but I'd love to be able to do something like:
- copy item
- select tracks
- paste copied item to selected tracks

Anyone know of anything to accomplish this (or similar)?

Thanks!

~ Chris
cjacko13 is offline   Reply With Quote
Old 05-16-2021, 10:56 PM   #2
daniellumertz
Human being with feelings
 
daniellumertz's Avatar
 
Join Date: Dec 2017
Location: Brazil
Posts: 1,992
Default

I gave it a try

Code:
function print(...)
    for k,v in ipairs({...}) do
        reaper.ShowConsoleMsg(tostring(v))
    end
    reaper.ShowConsoleMsg("\n")
end

function bfut_ResetAllChunkGuids(item_chunk, key) -- (I changed a little but it is from here: https://github.com/bfut/ReaScripts/blob/main/Items%20Editing/bfut_Replace%20item%20under%20mouse%20cursor%20with%20selected%20item.lua
    item_chunk = item_chunk:gsub('%s('..key..')%s+.-[\r]-[%\n]', "\ntemp%1 "..reaper.genGuid("").."\n", 1)
    return item_chunk:gsub('temp'..key, key), true
end

function ChangeChunkPosition(item_chunk, newpos)
    local new_chunk = item_chunk:gsub("POSITION [%d%.]+","POSITION "..tostring(newpos))
    return new_chunk
end

function LoadSelectedItems(list) -- Not used it is here in case you need
    reaper.Main_OnCommand(40289, 0)--Item: Unselect all items
    if #list ~= 0 then 
        for i = 1, #list do 
            local bol = reaper.ValidatePtr( list[i] ,"MediaItem*" )
            if bol then
                reaper.SetMediaItemSelected( list[i], true )
            end
        end 
    end
end

function SaveSelectedItems() -- Not used it is here in case you need
    local list = {}
    local num = reaper.CountSelectedMediaItems(0)
    if num ~= 0 then
        for i= 0, num-1 do
            list[i+1] =  reaper.GetSelectedMediaItem( 0, i )
        end
    end
    --reaper.Main_OnCommand(40289, 0)--Item: Unselect all items
    return list
end

function CopyTakeInfo(take_copy, take_paste) -- Copy every info you can with  reaper.SetMediaItemTakeInfo_Value()
    local names = [[
    D_STARTOFFS
    D_VOL
    D_PAN
    D_PANLAW
    D_PLAYRATE
    D_PITCH
    B_PPITCH
    I_CHANMODE
    I_PITCHMODE
    I_CUSTOMCOLOR
    IP_TAKENUMBER
    ]] -- You can add or remove items from this list to be copied or not. Here is all of them
    local info = {}
    for info in string.gmatch(names,"%S+") do
        local val = reaper.GetMediaItemTakeInfo_Value(take_copy, info)
        reaper.SetMediaItemTakeInfo_Value(take_paste, info, val)
    end
end

function CopyMediaNotMIDI(item, track, position) -- Fork From Xraym https://github.com/ReaTeam/ReaScripts-Templates/blob/master/Items%20Creations/X-Raym_Copy%20media%20item.lua
	local new_item = reaper.AddMediaItemToTrack(track)
	local new_item_guid = reaper.BR_GetMediaItemGUID(new_item)
	local retval, item_chunk =  reaper.GetItemStateChunk(item, '')
    local new_item_chunk = bfut_ResetAllChunkGuids(item_chunk, "IGUID")
    local new_item_chunk = bfut_ResetAllChunkGuids(new_item_chunk, "GUID")
	reaper.SetItemStateChunk(new_item, new_item_chunk)
    -- Set some extra info  PUT THIS BAC
    local vol = reaper.GetMediaItemInfo_Value(item, "D_VOL")
	reaper.SetMediaItemInfo_Value(new_item, "D_VOL", vol)
    local off = reaper.GetMediaItemInfo_Value(item, "D_SNAPOFFSET")
	reaper.SetMediaItemInfo_Value(new_item, "D_SNAPOFFSET", off)
    CopyTakeInfo(reaper.GetMediaItemTake(item, 0), reaper.GetMediaItemTake(new_item, 0)) 
	reaper.SetMediaItemInfo_Value(new_item, "D_POSITION", position)
	return new_item
end

function CopyMIDI(item, track, position)-- Copy an MIDI Item to Track at position. Be awere that it will change the item selection to the new item so use SaveSelectedItems() before calling it and then LoadSelectedItems(items_list)
    local retval, chunk = reaper.GetItemStateChunk( item, "", false )
    local chunk = bfut_ResetAllChunkGuids(chunk, "IGUID")
    local chunk = bfut_ResetAllChunkGuids(chunk, "GUID")
    local chunk = bfut_ResetAllChunkGuids(chunk, "POOLEDEVTS")
    local chunk = ChangeChunkPosition(chunk, position)

    local new_item = reaper.CreateNewMIDIItemInProj( track, 3, 0.1 )
    reaper.SetItemStateChunk( new_item, chunk, false )
    return new_item
end

function CopyMediaItem(item, track, position, selectcopy) -- It will change your item selection to the new item if you don't want this use You can put the SaveSelectedItems() and LoadSelectedItems().
    local selectcopy = selectcopy or false -- when selectcopy is true the copied item will be selected
    local ismidi = reaper.TakeIsMIDI(reaper.GetMediaItemTake(item, 0))
    local new_item = nil
    if ismidi == true then
        new_item = CopyMIDI(item, track, position) 
    else 
        new_item = CopyMediaNotMIDI(item, track, position)
    end
    reaper.UpdateItemInProject(new_item)
    if selectcopy then 
        reaper.Main_OnCommand(40289, 0)--Item: Unselect all items
        reaper.SetMediaItemSelected( new_item, true )
    end
    return new_item
end

--Exemple Copy Items to Selected Track  
local count_sel_items = reaper.CountSelectedMediaItems(0)
local count_sel_tracks = reaper.CountSelectedTracks(0)
if count_sel_tracks > 0 and count_sel_items > 0 then
    --Saves some info
    reaper.Undo_BeginBlock()
    reaper.PreventUIRefresh( 1 )
    --local items_list = SaveSelectedItems() 
    --The actual thing
    for items_i = 0,count_sel_items-1 do
        local item = reaper.GetSelectedMediaItem(0, items_i)
        local pos = reaper.GetMediaItemInfo_Value(item, "D_POSITION")
        for i = 0, count_sel_tracks-1 do
            local track_loop = reaper.GetSelectedTrack(0, i)
            CopyMediaItem(item, track_loop, pos)
            reaper.UpdateArrange()
        end
        --LoadSelectedItems(items_list)
    end
    --Load things up
    reaper.PreventUIRefresh( -1 )
    reaper.Undo_EndBlock('Copy Items to Selected Tracks', -1)
end
thanks to bfut and xraym I used some little parts credited in the script


Edit: just removed a bug but is working for me now. Any problems just say

Last edited by daniellumertz; 05-17-2021 at 12:53 AM.
daniellumertz is offline   Reply With Quote
Old 05-18-2021, 06:17 PM   #3
MusoBob
Human being with feelings
 
MusoBob's Avatar
 
Join Date: Sep 2014
Posts: 2,643
Default

This one select the item then ctrl select the tracks you want it copied to, then run.

Code:
 item_count = reaper.CountMediaItems( 0 )
 for i = 0, item_count-1 do
   item = reaper.GetMediaItem( 0, i )
   if reaper.IsMediaItemSelected( item ) then
     item_track = reaper.GetMediaItemTrack( item )
     reaper.SetTrackSelected( item_track, 1 )
     item_track_number = reaper.GetMediaTrackInfo_Value( item_track, "IP_TRACKNUMBER")
   end
 end 
 sel_track_table_number = {}
 track_count = reaper.CountTracks( 0 )
 sel_track_count = reaper.CountSelectedTracks2( 0, 0 )
 for i = 0, track_count -1 do
   track = reaper.GetTrack( 0, i )
   if reaper.IsTrackSelected( track ) then
     track_number = reaper.GetMediaTrackInfo_Value( track, "IP_TRACKNUMBER")
     if track_number ~= item_track_number then
       table.insert(sel_track_table_number, i+1)
     end
   end  
 end 
 
 
 if  reaper.CountSelectedMediaItems( 0 ) ==1 then
   
   for i = 1, sel_track_count -1 do
     sel_item = reaper.GetSelectedMediaItem( 0, 0 )
     d_pos = reaper.GetMediaItemInfo_Value(item, "D_POSITION")
     item_snap = reaper.GetMediaItemInfo_Value(item, "D_SNAPOFFSET")
     item_pos = d_pos + item_snap
     reaper.SetEditCurPos(item_pos, 0, 0)
     reaper.Main_OnCommand( 40698, 0 ) -- Edit: Copy items
     reaper.Main_OnCommand( 42398, 0 ) -- Item: Paste items/tracks
     sel_item = reaper.GetSelectedMediaItem( 0, 0 )
     track = reaper.GetTrack( 0, sel_track_table_number[i]-1)
     reaper.MoveMediaItemToTrack( sel_item, track )
     
   end
 end  
 
 reaper.UpdateArrange()
__________________
ReaTrakStudio Chord Track for Reaper forum
www.reatrak.com
STASH Downloads https://stash.reaper.fm/u/ReaTrak

Last edited by MusoBob; 05-19-2021 at 05:41 AM.
MusoBob is offline   Reply With Quote
Old 05-18-2021, 07:01 PM   #4
daniellumertz
Human being with feelings
 
daniellumertz's Avatar
 
Join Date: Dec 2017
Location: Brazil
Posts: 1,992
Default

Quote:
Originally Posted by MusoBob View Post
This one select the item then ctrl select the tracks you want it copied to, then run.

Code:
 item_count = reaper.CountMediaItems( 0 )
 for i = 0, item_count-1 do
   item = reaper.GetMediaItem( 0, i )
   if reaper.IsMediaItemSelected( item ) then
     item_track = reaper.GetMediaItemTrack( item )
     reaper.SetTrackSelected( item_track, 1 )
     item_track_number = reaper.GetMediaTrackInfo_Value( item_track, "IP_TRACKNUMBER")
   end
 end 
 sel_track_table_number = {}
 track_count = reaper.CountTracks( 0 )
 sel_track_count = reaper.CountSelectedTracks2( 0, 0 )
 for i = 0, track_count -1 do
   track = reaper.GetTrack( 0, i )
   if reaper.IsTrackSelected( track ) then
     track_number = reaper.GetMediaTrackInfo_Value( track, "IP_TRACKNUMBER")
     if track_number ~= item_track_number then
       table.insert(sel_track_table_number, i+1)
     end
   end  
 end 
 
 
 if  reaper.CountSelectedMediaItems( 0 ) ==1 then
   
   for i = 1, sel_track_count -1 do
     sel_item = reaper.GetSelectedMediaItem( 0, 0 )
     item_pos = reaper.GetMediaItemInfo_Value(sel_item, "D_POSITION")
     reaper.SetEditCurPos(item_pos, 0, 0)
     reaper.Main_OnCommand( 40698, 0 ) -- Edit: Copy items
     reaper.Main_OnCommand( 42398, 0 ) -- Item: Paste items/tracks
     sel_item = reaper.GetSelectedMediaItem( 0, 0 )
     track = reaper.GetTrack( 0, sel_track_table_number[i]-1)
     reaper.MoveMediaItemToTrack( sel_item, track )
     
   end
 end  
 
 reaper.UpdateArrange()
Just need to be careful to have the ripple edit off but this will copy the automation if automation follow items, which for this case it seems better
daniellumertz is offline   Reply With Quote
Old 05-18-2021, 09:13 PM   #5
cjacko13
Human being with feelings
 
Join Date: Apr 2021
Posts: 4
Default

Awesome sauce! I will give these a go. Thanks!
cjacko13 is offline   Reply With Quote
Old 05-19-2021, 05:42 AM   #6
MusoBob
Human being with feelings
 
MusoBob's Avatar
 
Join Date: Sep 2014
Posts: 2,643
Default

I added a snap offset for the above script:
Code:
     d_pos = reaper.GetMediaItemInfo_Value(item, "D_POSITION")
     item_snap = reaper.GetMediaItemInfo_Value(item, "D_SNAPOFFSET")
     item_pos = d_pos + item_snap
__________________
ReaTrakStudio Chord Track for Reaper forum
www.reatrak.com
STASH Downloads https://stash.reaper.fm/u/ReaTrak
MusoBob is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 11:04 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.