Old 01-01-2018, 05:06 PM   #1
J Reverb
Human being with feelings
 
Join Date: Jul 2009
Posts: 1,071
Default Lua example needed

Happy new year !

Would someone be kind enough to give me a quick code example of how to split an item at edit cursor in the form of a Lua script ?

I am trying to learn Lua scripting this year and need a basic place to start.

Many thanks,

J.
J Reverb is offline   Reply With Quote
Old 01-01-2018, 05:48 PM   #2
heda
Human being with feelings
 
heda's Avatar
 
Join Date: Jun 2012
Location: Spain
Posts: 7,239
Default

you can use any action in scripts with the Main_OnCommand function knowing the command ID number shown in the actions window.
It's a good idea to put a lua comment in the line with the action text name like this:
Code:
reaper.Main_OnCommand(40757, 0) -- Item: Split items at edit cursor (no change selection)
heda is offline   Reply With Quote
Old 01-01-2018, 05:56 PM   #3
J Reverb
Human being with feelings
 
Join Date: Jul 2009
Posts: 1,071
Default

Thanks Heda,

I was looking to learn how to get item and cursor position and then execute a split on the selected item using the API.

Is this something that can only be done using Main_OnCommand?

I would like to get a bit deeper into how a few basic functions are constructed using lua.

Thanks for your reply,

J.
J Reverb is offline   Reply With Quote
Old 01-01-2018, 06:05 PM   #4
heda
Human being with feelings
 
heda's Avatar
 
Join Date: Jun 2012
Location: Spain
Posts: 7,239
Default

ah ok...
I think it should be something like:
Code:
local item=reaper.GetSelectedMediaItem(0, 0) -- get first selected item
local time_editcursor=reaper.GetCursorPosition() -- get time of edit cursor
reaper.SplitMediaItem(item, time_editcursor) -- split the item at the time where edit cursor is
heda is offline   Reply With Quote
Old 01-01-2018, 06:12 PM   #5
J Reverb
Human being with feelings
 
Join Date: Jul 2009
Posts: 1,071
Default

Thank you Heda,

This gives me a good start, it's quite a nice example to show the basic mechanics of the API and how it is used.

I am going to try and re create some basic functions based on this.

Thanks again for your help,

J.
J Reverb is offline   Reply With Quote
Old 01-02-2018, 12:41 AM   #6
J Reverb
Human being with feelings
 
Join Date: Jul 2009
Posts: 1,071
Default

Another question for someone who may be able to help...

How to select next/previous item on track, assuming that an item has already been selected. I would imagine you would need 2 separate scripts here.

Thanks,

J.
J Reverb is offline   Reply With Quote
Old 01-02-2018, 04:02 AM   #7
IXix
Human being with feelings
 
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,889
Default

Quote:
Originally Posted by J Reverb View Post
How to select next/previous item on track, assuming that an item has already been selected. I would imagine you would need 2 separate scripts here.
No need for multiple scripts. Think it through...

1. Check if there's an item selected
2. If there is, store its ID and find which track it's on
3. Check all items on the track, from beginning/end until you find the selected item
4. Check if there's another item after/before
5. If there is, update the selection.

It's (mostly) just a matter of working out the necessary steps and then trawling through the (admittedly rather intimidating) API to find the functions that you need.

Next you have to spend some time scratching your head trying to figure out why it's not working how you think it should.

If head scratching doesn't fix it, you come on the forum, ask for help and then some clever sod shows you a way to do it that uses 80% less code. Well, that's my strategy anyway.
IXix is offline   Reply With Quote
Old 01-02-2018, 05:24 AM   #8
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,875
Default

Quote:
Originally Posted by IXix
If head scratching doesn't fix it, you come on the forum
There is also the "look for functions in existing scripts source code". Sharing scripts is not just sharing Actions, but also code snippets to everyone :P Open source !

Script which splits items and scripts which do something with "next selected items on tracks" are common.
X-Raym is offline   Reply With Quote
Old 01-03-2018, 03:02 AM   #9
J Reverb
Human being with feelings
 
Join Date: Jul 2009
Posts: 1,071
Default

Quote:
Originally Posted by IXix View Post
No need for multiple scripts. Think it through...

1. Check if there's an item selected
2. If there is, store its ID and find which track it's on
3. Check all items on the track, from beginning/end until you find the selected item
4. Check if there's another item after/before
5. If there is, update the selection.
Code:
function splitter()
        
        reaper.Main_OnCommand(40759,0) -- split item at edit cursor (select right)
        item=reaper.GetSelectedMediaItem(0, 0)       
        position=reaper.GetMediaItemInfo_Value(item, "D_POSITION") 
        snapto= reaper.BR_GetClosestGridDivision(position)
        reaper.SetMediaItemInfo_Value(item,"D_POSITION",snapto)   
                
        end
splitter()

reaper.UpdateArrange()
This is as far as I have got,
My aim is to have the previous items length snap to the current items start leaving the current item selected.

Definitely some head scratching going on here..
That's the reason why I have used Main_OnCommand but I would really like to know how to do this just using the api functions so as to have a better understanding of how things work.

@xraym
You should ask someone to translate your tutorials they look very good
J Reverb is offline   Reply With Quote
Old 01-03-2018, 03:25 AM   #10
IXix
Human being with feelings
 
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,889
Default

Quote:
Originally Posted by J Reverb View Post
This is as far as I have got,
My aim is to have the previous items length snap to the current items start leaving the current item selected.
Well, I can't debug it for you but you need to figure out what happens when an item is split. Which side keeps the original ID? Are stored item pointers still valid? If the left item keeps the original identity then it's easy. If not then you need to recapture that item somehow.

Printing messages to the console is an invaluable debugging technique.

Quote:
Originally Posted by J Reverb View Post
Definitely some head scratching going on here..
It's good for you (but don't scratch to hard or you may face premature hair loss )
IXix 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 02:43 AM.


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