Old 07-29-2019, 04:20 PM   #1
deadbongo
Human being with feelings
 
Join Date: May 2016
Posts: 27
Default [solved]-explode overlapping items in tracks

--Hi there...
--I'm really new to scripting.
--I'm trying to write a lua script to propagate overlapping items in the least
--number of tracks , but I'm stuck on the 1st step...
--
--My idea is:
--1. Create a track
--2. Put all overlapping items to that track (first pass)
--3. Move to that track
--4. Create a new track
--5. Put remaining overlapping items to that track (second pass)
--6. Move to that track
--7. Create a new track... until the new track is empty...
--8. end
--
--To do so, I nested 2 loops and I expected that on the first pass every overlapped
--item is going to the new track, but when there are items that begin after and end
--before something goes wrong in a random fashion...
--In the pictures: I espect green AND red item to the new track, but only the green
--goes there :-(

Overlapping items:

Expected:

Real:


--Here's the script:
------------------------------------------
Code:
item = reaper.GetSelectedMediaItem(0,0)
track = reaper.GetMediaItem_Track(item)
tracknum = reaper.GetMediaTrackInfo_Value(track, "IP_TRACKNUMBER")
reaper.InsertTrackAtIndex(tracknum, false)

 item_count = reaper.CountSelectedMediaItems (0)
 
  for i=0, item_count-1 do
   sel_item0 = reaper.GetSelectedMediaItem (0,i)
   
     for j=i+1, item_count-1 do
   
     sel_item1 = reaper.GetSelectedMediaItem (0,j)
    
      i_pos0 = reaper.GetMediaItemInfo_Value (sel_item0,  "D_POSITION")
      i_lgt0 = reaper.GetMediaItemInfo_Value (sel_item0,  "D_LENGTH")
      i_pos1 = reaper.GetMediaItemInfo_Value (sel_item1,  "D_POSITION")
     
      
       if (((i_pos0+i_lgt0)-i_pos1)>0.05)
       then
       
        dest_track = reaper.GetTrack(0, tracknum)
        reaper.TrackList_AdjustWindows(false) 
        
        reaper.MoveMediaItemToTrack(sel_item1, dest_track)
        
        end
     end   
  end
-------------------------------------------



--Thank you very much!!!
Attached Images
File Type: jpg OverlappedRegions.jpg (56.9 KB, 295 views)
File Type: jpg ExpectedResult.jpg (54.7 KB, 297 views)
File Type: jpg real.jpg (41.7 KB, 294 views)

Last edited by deadbongo; 08-06-2019 at 01:27 AM.
deadbongo is offline   Reply With Quote
Old 08-04-2019, 04:18 AM   #2
deadbongo
Human being with feelings
 
Join Date: May 2016
Posts: 27
Default Found a solution with arrays

Just re-wrote the code from scratch.
Now it works as expected.
Don't know if there is a more elegant way to do that, but I am quite happy with the results...

before:


after:


Here is the code...

Code:
--debug

--[[
function msg(param)
reaper.ShowConsoleMsg(tostring(param))
end
msg ("")
--]]
reaper.Undo_BeginBlock()

item = reaper.GetSelectedMediaItem(0,0)
track = reaper.GetMediaItem_Track(item)
tracknum = reaper.GetMediaTrackInfo_Value(track, "IP_TRACKNUMBER")


--select all items
reaper.Main_OnCommand(40421,0)
item = reaper.GetSelectedMediaItem(0,0)
num_of_items=  reaper.CountSelectedMediaItems( 0 )

--create the matrix
tk = {}           
	for i=1,num_of_items do
		tk[i] = {}     
			for j=1,num_of_items do
			tk[i][j] = 0
			end
	end

	for j=1,num_of_items do
		tk[j][1] = 1
	end

ai = {}    -- new array
	for i=1, num_of_items-1 do
		ai [i]="x"
	end

	for i=1, num_of_items-1 do
		sel_item0 = reaper.GetSelectedMediaItem (0,i)
		ai[i] = sel_item0 
	end


--INIZIALIZE TK 1 WITH ITEM 1
tk_index=1
it_index=1

--fill the tracks
tk[tk_index][it_index]=reaper.GetSelectedMediaItem (0,0)

w=0

::START::

w=w+1
	
	if w >(num_of_items-1) then
		goto THE_END
	end
	
tk_index=1

::CONTROL::

	if tk_index>num_of_items or
		it_index>num_of_items then
		goto THE_END
	end

	---control items in current track 
	for i= 1, num_of_items-1 do

		---...till you find the last one and check for is ending point
		if (tk[tk_index][num_of_items-i])==1  then
			tk[tk_index][num_of_items-i+1]=ai[w]
			tk[tk_index][1]="x"
			goto START
		end

		if (tk[tk_index][num_of_items-i])~=0 and(tk[tk_index][num_of_items-i])~=1 then
			i_pos1 = reaper.GetMediaItemInfo_Value (tk[tk_index][num_of_items-i],  "D_POSITION")
			i_lgt1 = reaper.GetMediaItemInfo_Value (tk[tk_index][num_of_items-i],  "D_LENGTH")
			i_end1 =i_pos1+i_lgt1
			goto COMPARE
		end
	end

::COMPARE::

i_posr = reaper.GetMediaItemInfo_Value (ai[w],  "D_POSITION")

	if i_posr>i_end1 then
		it_index=w
		it_insex=it_index+1
		tk[tk_index][w+1]=ai[w]
		goto START
	end

	if i_posr<i_end1 then
		tk_index=tk_index+1
		goto CONTROL
	end

::THE_END:: 

num_tks=0
	for t=1, num_of_items do
		if (tk[t][1])==1 then
		num_tks=num_tks+1
		end
	end

empty=(num_of_items-num_tks)

	for t=1, num_of_items do
		reaper.InsertTrackAtIndex(tracknum, false)
	end

	for t=1, num_of_items do
		dest_track = reaper.GetTrack(0,t)  
		n =("add_track ".. tostring(t))
		reaper.GetSetMediaTrackInfo_String(dest_track, "P_NAME", n, true)            
	end

	for t=1, num_of_items do
		dest_track = reaper.GetTrack(0,t-1)
		for i=1,num_of_items do
			item =tk[t][i]
				if item ~=0 and item ~=1 and item ~="x" then
				reaper.MoveMediaItemToTrack( item, dest_track )
				end
		end
	end

	for t=0, num_of_items-empty do
		dest_track = reaper.GetTrack(0,num_of_items-t)
		reaper.DeleteTrack( dest_track )
	end

reaper.UpdateArrange()

reaper.Undo_EndBlock( "explode overlapping items in tracks",0 )
Hope this can be useful!
Attached Images
File Type: jpg before.jpg (39.8 KB, 246 views)
File Type: jpg after.jpg (36.8 KB, 230 views)

Last edited by deadbongo; 08-04-2019 at 04:36 AM.
deadbongo is offline   Reply With Quote
Old 04-27-2020, 12:35 PM   #3
Buy One
Human being with feelings
 
Buy One's Avatar
 
Join Date: Sep 2019
Posts: 1,146
Default

There's also Xenakios/SWS: Explode selected items to new tracks (keeping positions)
Buy One 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 09:15 PM.


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