< Return to Video

Lua Scripting with REAPER 5 - Part. 7: Loop in Items on Selected Tracks

  • 0:00 - 0:05
    HI, this is X-Raym, for the 7th part
    of our ReaScript Course.
  • 0:05 - 0:10
    In this episode, will wee learn how to get an item,
    according to its position on its parent track.
  • 0:10 - 0:17
    Imagine that I want to restrict our script
    on items on selected tracks
  • 0:17 - 0:19
    but still in time selection.
  • 0:19 - 0:21
    I will have to get my items
    in a different way.
  • 0:21 - 0:29
    But before going further, I will clean the
    script a bit by create a new functions.
  • 0:33 - 0:36
    I will call it Action.
  • 0:37 - 0:43
    I delete my warning messages,
    we don't need it anymore.
  • 0:47 - 0:51
    So Main here will check if our action
    can be executed,
  • 0:51 - 0:56
    And we will create a new function
    called Action.
  • 1:13 - 1:17
    I check if the script still works.
  • 1:20 - 1:24
    It seems to work fine, I can
    continue from here.
  • 1:24 - 1:27
    I want to consider only
    selected tracks.
  • 1:27 - 1:32
    I will have to make a loop
    from selected tracks.
  • 1:34 - 1:37
    Count... sel... tracks...
  • 1:37 - 1:39
    I search for the function...
  • 1:49 - 1:54
    I paste it here.
    The project is 0.
  • 1:55 - 2:00
    For every selected tracks...
  • 2:04 - 2:07
    count_sel_tracks - 1
  • 2:07 - 2:09
    do
  • 2:09 - 2:11
    I add end
  • 2:12 - 2:15
    For every track selected,
  • 2:15 - 2:20
    we will get the track,
  • 2:21 - 2:24
    "get sel track"
  • 2:28 - 2:33
    This function to return a track
  • 2:33 - 2:37
    according to it's index in selection.
  • 2:37 - 2:41
    We already saw the equivalent function
    for selected items.
  • 2:41 - 2:43
    I put j
  • 2:43 - 2:46
    The loop look for selected track.
  • 2:46 - 2:49
    I will count the number of items they have.
  • 2:52 - 2:55
    Count item
  • 2:55 - 3:02
    CountTrackMedialtems which allows
    to count the number of items on the track.
  • 3:02 - 3:08
    I will create the variable
    count_items_on_tracks
  • 3:10 - 3:13
    Here, I have to put a track,
  • 3:13 - 3:17
    and this track came from
    my variable named track
  • 3:21 - 3:26
    For every item on the track
  • 3:35 - 3:37
    do the following thing,
  • 3:40 - 3:47
    Get the item according to
    it's index on the track.
  • 3:51 - 3:52
    Here is the function.
  • 3:54 - 4:02
    This track return an item according to
    a track and a index on that track.
  • 4:02 - 4:05
    The track still came
    from the variable track,
  • 4:05 - 4:09
    and the item index is defined by the loop
    which starts here.
  • 4:09 - 4:11
    So, I put i.
  • 4:13 - 4:15
    I don't need this anymore.
  • 4:15 - 4:19
    I can delete this.
  • 4:19 - 4:23
    And all this is the same thing...
  • 4:39 - 4:46
    Recap: the Action function will,
    for every selected tracks,
  • 4:46 - 4:53
    check all their items, and if an
    item is inside the time selection
  • 4:53 - 4:56
    then a new fade-out
    length value will be set.
  • 4:56 - 5:02
    This value came from the
    item under mouse.
  • 5:02 - 5:04
    I run the script to see.
  • 5:06 - 5:09
    I set a fade here.
  • 5:10 - 5:12
    And I check.
  • 5:12 - 5:17
    We see that it does nothing.
  • 5:18 - 5:23
    If I select this track,
    the fade-out length value get propagated
  • 5:23 - 5:26
    on items on selected tracks
    which are in time selection.
  • 5:26 - 5:28
    If I select this 3 tracks...
  • 5:30 - 5:32
    We see that it works as expected.
  • 5:32 - 5:34
    The loop could be a bit more optimized.
  • 5:34 - 5:40
    Indeed, we don't need to check the items
    that are after the time selection.
  • 5:40 - 5:43
    I can do the following thing:
  • 5:45 - 5:47
    if
  • 5:47 - 5:50
    If item_pos
  • 5:50 - 5:56
    is after the end of the time selection,
  • 5:59 - 6:01
    then
  • 6:01 - 6:04
    break, in other words
    "Break the loop"
  • 6:04 - 6:06
    else
  • 6:08 - 6:11
    modify the items as desired.
  • 6:11 - 6:17
    Break allows to stop on loop
    so that it doesn't get executed unecessary.
  • 6:18 - 6:21
    I will display i
  • 6:23 - 6:26
    I will display i and j in the console.
  • 6:26 - 6:31
    I duplicated some items,
    and I run the script.
  • 6:33 - 6:38
    As you can see, this doesn't consider the
    other items (aka, beyond time selection).
  • 6:38 - 6:41
    If I delete break,
  • 6:48 - 6:53
    You can see that all items on selected
    tracks have been unecessary checked.
  • 6:53 - 6:59
    Break allows to save a bit of performance when
    it is not necessary to calculate certain things.
  • 6:59 - 7:03
    Our function check the first item,
  • 7:03 - 7:08
    then the next item, and if its position is
    after the end of time selection
  • 7:08 - 7:11
    which means that every items after
    this one will be in the same case,
  • 7:11 - 7:18
    So no need to check their position and their
    end to see if they are in time selection.
  • 7:18 - 7:23
    In the next items, we will see how to use
    a REAPER native function inside a script.
Title:
Lua Scripting with REAPER 5 - Part. 7: Loop in Items on Selected Tracks
Description:

Source Article and Exercice:
http://extremraym.com/en/reascript-video-items-on-tracks

Please consider making a donation if you liked it:
http//extremraym.com/en/donation

Cheers !

more » « less
Video Language:
French
Duration:
07:23

English, British subtitles

Revisions