Old 10-19-2018, 10:25 PM   #1
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 565
Default Lua: weird problem about MoveMediaItemToTrack function

This is a script to "move selected items to selected track"

Code:
track=reaper.GetSelectedTrack(0,0)

num=reaper.CountSelectedMediaItems(0)


  for i=0,num-1 do

    item=reaper.GetSelectedMediaItem(0,i)

    reaper.MoveMediaItemToTrack(item,track)
  
  end
When I select more than one item, about half of them failed to move



Is ther any problem in my code?
dsyrock is offline   Reply With Quote
Old 10-19-2018, 10:37 PM   #2
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

When it moves each item, you're changing the order of the selected items - what used to be 1 is now 0, etc. - so it ends up missing some of them in the shuffle.

Try looping through all of the selected items first and putting them in a table, then moving all of the items in the table afterward:

Code:
local items = {}
for i = 0, reaper.CountSelectedMediaItems(0) do
  items[i] = reaper.GetSelectedMediaItem(0, i)
end

for i = 0, #items do
  reaper.MoveMediaItemToTrack(items[i],track)
end
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 10-19-2018, 10:38 PM   #3
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 565
Default

Quote:
Originally Posted by Lokasenna View Post
When it moves each item, you're changing the order of the selected items - what used to be 1 is now 0, etc. - so it ends up missing some of them in the shuffle.

Try looping through all of the selected items first and putting the MediaItems in a table, then moving all of the items in the table afterward:

Code:
local items = {}
for i = 0, reaper.CountSelectedMediaItems(0) do
  items[i] = reaper.GetSelectedMediaItem(0, i)
end

for i = 0, #items do
  reaper.MoveMediaItemToTrack(items[i],track)
end
Oh yes. What a silly problem. Thanks Lokasenna! Big help for me
dsyrock 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 05:39 PM.


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