|
|
|
08-04-2023, 04:28 AM
|
#1
|
Human being with feelings
Join Date: Apr 2018
Posts: 123
|
LUA missing argument
Hi,
I decided to get into lua scripting, and my first attempt has gone wrong, probably due to my misunderstanding a concept somewhere.
It isn't long, so I'll post the complete script:
<Beginning code>
-- Function to split the selected track into items of random length
function splitSelectedTrack()
local track = reaper.GetSelectedTrack(0, 0) -- Get the first selected track
if not track then return end
local itemCount = reaper.CountTrackMediaItems(track) -- Get the number of items on the track
if itemCount == 0 then return end
reaper.PreventUIRefresh(1)
reaper.Undo_BeginBlock()
local newTrack = reaper.InsertTrackAtIndex(reaper.GetNumTracks(), true) -- Insert a new track
for i = 0, itemCount - 1 do
local item = reaper.GetTrackMediaItem(track, i) -- Get the item
local position = reaper.GetMediaItemInfo_Value(item, "D_POSITION") -- Get the item position
local length = reaper.GetMediaItemInfo_Value(item, "D_LENGTH") -- Get the item length
-- Generate a random length between 0.5 to 5 seconds (adjust as needed)
local randomLength = math.random(5, 50)/10
reaper.SetMediaItemInfo_Value(item, "D_LENGTH", randomLength)
local newItem = reaper.SplitMediaItem(item, position + randomLength) -- Split the item at the random length
*** reaper.MoveMediaItemToTrack(newItem, newTrack) -- Move the new item to the new track
end
reaper.Undo_EndBlock("Split and Randomize Items", -1)
reaper.PreventUIRefresh(-1)
reaper.UpdateArrange()
end
splitSelectedTrack() -- Call the function to split and randomize the items
I've marked the line which is being complained about, and the message implies that argument 1 is wrong.
Any help very much appreciated.
|
|
|
08-04-2023, 05:03 AM
|
#2
|
Human being with feelings
Join Date: Jun 2009
Location: Croatia
Posts: 4,597
|
Code:
function splitSelectedTrack()
local track = reaper.GetSelectedTrack(0, 0) -- Get the first selected track
if not track then return end
local itemCount = reaper.CountTrackMediaItems(track) -- Get the number of items on the track
if itemCount == 0 then return end
reaper.PreventUIRefresh(1)
reaper.Undo_BeginBlock()
reaper.InsertTrackAtIndex(reaper.GetNumTracks(), true) -- Insert a new track
newTrack = reaper.GetTrack(0,reaper.GetNumTracks()-1) -- GET NEW TRACK
for i = itemCount, 1, -1 do
local item = reaper.GetTrackMediaItem(track, i-1) -- Get the item
local position = reaper.GetMediaItemInfo_Value(item, "D_POSITION") -- Get the item position
local length = reaper.GetMediaItemInfo_Value(item, "D_LENGTH") -- Get the item length
-- Generate a random length between 0.5 to 5 seconds (adjust as needed)
local randomLength = math.random(5, 50)/10
reaper.SetMediaItemInfo_Value(item, "D_LENGTH", randomLength)
local newItem = reaper.SplitMediaItem(item, position + randomLength) -- Split the item at the random length
if not newItem then
newItem = item -- split takes right item after split (if not splittet then use original item
end
reaper.MoveMediaItemToTrack(newItem, newTrack) -- Move the new item to the new track
end
reaper.Undo_EndBlock("Split and Randomize Items", -1)
reaper.PreventUIRefresh(-1)
reaper.UpdateArrange()
end
splitSelectedTrack() -- Call the function to split and randomize the items
|
|
|
08-04-2023, 01:29 PM
|
#3
|
Human being with feelings
Join Date: Apr 2018
Posts: 123
|
Thanks I'll give it a try.
|
|
|
08-05-2023, 04:24 AM
|
#4
|
Human being with feelings
Join Date: Apr 2018
Posts: 123
|
Sexan,
This works now, but I added a loop which was intended to split the track into random length items. It isn't working for some reason:
<Code>
function splitSelectedTrack()
local track = reaper.GetSelectedTrack(0, 0) -- Get the first selected track
if not track then return end
local itemCount = reaper.CountTrackMediaItems(track) -- Get the number of items on the track
if itemCount == 0 then return end
reaper.PreventUIRefresh(1)
reaper.Undo_BeginBlock()
reaper.InsertTrackAtIndex(reaper.GetNumTracks(), true) -- Insert a new track
newTrack = reaper.GetTrack(0,reaper.GetNumTracks()-1) -- GET NEW TRACK
for i = itemCount, 1, -1 do
local item = reaper.GetTrackMediaItem(track, i-1) -- Get the item
local position = reaper.GetMediaItemInfo_Value(item, "D_POSITION") -- Get the item position
local length = reaper.GetMediaItemInfo_Value(item, "D_LENGTH") -- Get the item length
-- Generate a random length between 0.5 to 5 seconds (adjust as needed)
local randomLength = math.random(5, 50)/10
reaper.SetMediaItemInfo_Value(item, "D_LENGTH", randomLength)
local newItem = reaper.SplitMediaItem(item, position + randomLength) -- Split the item at the random length
if not newItem then
newItem = item -- split takes right item after split (if not split then use original item
end
reaper.MoveMediaItemToTrack(newItem, newTrack) -- Move the new item to the new track
end
reaper.Undo_EndBlock("Split and Randomize Items", -1)
reaper.PreventUIRefresh(-1)
reaper.UpdateArrange()
end
while reaper.CountTrackMediaItems(track) > 0 do
splitSelectedTrack()
end
<End Code>
I suspect I need to declare track globally to make this work properly?
|
|
|
08-05-2023, 04:49 AM
|
#5
|
Human being with feelings
Join Date: Jun 2009
Location: Croatia
Posts: 4,597
|
wrap your code inside code block like this so we can read it better
Let me see...
Yeah you need to declare track as global:
Code:
track = reaper.GetSelectedTrack(0,0)
if reaper.CountTrackMediaItems(track) > 0 then
splitSelectedTrack()
end
BTW looking at the code,why are you setting the item length to random before splitting and then splitting at that random length? There is no cut there since item shrinked thats why you had that original bug
Should your code do this?
Code:
local track = reaper.GetSelectedTrack(0, 0) -- Get the first selected track
if not track then return end
function splitSelectedTrack()
local itemCount = reaper.CountTrackMediaItems(track) -- Get the number of items on the track
if itemCount == 0 then return end
reaper.PreventUIRefresh(1)
reaper.Undo_BeginBlock()
reaper.InsertTrackAtIndex(reaper.GetNumTracks(), true) -- Insert a new track
local newTrack = reaper.GetTrack(0,reaper.GetNumTracks()-1) -- GET NEW TRACK
for i = itemCount, 1, -1 do
local item = reaper.GetTrackMediaItem(track, i-1) -- Get the item
local position = reaper.GetMediaItemInfo_Value(item, "D_POSITION") -- Get the item position
local length = reaper.GetMediaItemInfo_Value(item, "D_LENGTH") -- Get the item length
-- Generate a random length between 0.5 to 5 seconds (adjust as needed)
local randomLength = math.random(5, 50)/10
local newItem = reaper.SplitMediaItem(item, position + randomLength) -- Split the item at the random length
if newItem then
reaper.MoveMediaItemToTrack(newItem, newTrack) -- Move the new item to the new track
end
end
reaper.Undo_EndBlock("Split and Randomize Items", -1)
reaper.PreventUIRefresh(-1)
reaper.UpdateArrange()
end
if reaper.CountTrackMediaItems(track) > 0 then
splitSelectedTrack()
end
Your original code is just shrinking items and move them to new track there is no splitting thats why there was this check
Code:
if not newItem then
newItem = item -- split takes right item after split (if not split then use original item)
end
which is now unnecessary since its splitting
Last edited by Sexan; 08-05-2023 at 05:12 AM.
|
|
|
08-05-2023, 08:10 AM
|
#6
|
Human being with feelings
Join Date: Apr 2018
Posts: 123
|
Sexan,
The aim of the script is to split the items into random lengths, and eventually place them randomly in a new track. Originally, I did something similar with tape, i.e. cut the tape up, throw it all around, then splice it back together again. The whole thing makes no sense, but depending on the media used, some sections can be quite interesting for looping etc.
|
|
|
08-05-2023, 08:46 AM
|
#7
|
Human being with feelings
Join Date: Jun 2009
Location: Croatia
Posts: 4,597
|
But original script (post1) did not cut anything?
So basically it should do what new code does (my mod) but keep original item as is?
|
|
|
08-05-2023, 11:41 AM
|
#8
|
Human being with feelings
Join Date: Apr 2018
Posts: 123
|
Sexan,
Yes, exactly. I'll work on it later.
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 05:29 PM.
|