Old 11-24-2014, 09:45 AM   #1
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 77
Default Iterate function on each selected track

Hi there.

First attempts with Python ReaScript.

I wrote a script that performs some actions for each item on a track.
I actually do not consider the track at all in my function. But the "select and move to next item" (which I use to parse the items) works on the selected track by default (or the first of the selected tracks).

To my surprise I do not manage to expand my code to do the same thing for each selected track in a row.

Any advice please?
I suppose it is a simple thing, but I c'an't seem to figure it out.

thanks a lot,

C.
Cedrik0s is offline   Reply With Quote
Old 11-24-2014, 11:31 AM   #2
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

Hi,

Looping example:
Code:
from reaper_python import *

def msg(m):
    RPR_ShowConsoleMsg(m)
    RPR_ShowConsoleMsg("\n")

def iterate():
    # outer loop - for selected tracks
    for i in range(RPR_CountSelectedTracks(0)): # loop through selected tracks
        current_track = RPR_GetSelectedTrack(0, i)  # get track pointer -> store to "current_track" var
        msg(current_track) # show track pointer

        # inner loop - for each items on current track (in outer loop)
        for j in range(RPR_CountTrackMediaItems(current_track)): # loop through items on current track
            current_item = RPR_GetTrackMediaItem(current_track, j)  # get item pointer -> store to "current_item" var
            msg("    " + current_item)  # show item pointer

# call the "iterate" function:
iterate()
spk77 is offline   Reply With Quote
Old 11-25-2014, 04:02 AM   #3
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 77
Default

Thanks for your answer spk77.

Your code is clearly cleaner than mine.
I'll rewrite the parsing according to your suggestion.

It should solve my problems.

Thanks again.

C
Cedrik0s is offline   Reply With Quote
Old 11-25-2014, 04:29 AM   #4
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

If you are using (native)actions from the action list - you may have to do something like this:

Code:
from reaper_python import *

def msg(m):
    RPR_ShowConsoleMsg(m)
    RPR_ShowConsoleMsg("\n")

def iterate():
    RPR_Main_OnCommand(40289, 0)    # Unselect all items
    # outer loop - for selected tracks
    for i in range(RPR_CountSelectedTracks(0)): # loop through selected track
        current_track = RPR_GetSelectedTrack(0, i)  # get track pointer -> store to "current_track" var

        # inner loop - for each items on current track (in outer loop)
        for j in range(RPR_CountTrackMediaItems(current_track)): # loop through items on current track
            current_item = RPR_GetTrackMediaItem(current_track, j)  # get item pointer -> store to "current_item" var
            RPR_SetMediaItemSelected(current_item, True)    # Set current item selected
            # Add your actions here:
            # RPR_Main_OnCommand(Action id here, 0);
            # RPR_Main_OnCommand(Another action id here, 0);
            RPR_SetMediaItemSelected(current_item, False)    # Set current item UNselected

# call the "iterate" function:
iterate()
spk77 is offline   Reply With Quote
Old 11-26-2014, 01:38 AM   #5
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 77
Default

Thanks again, spk77.

I managed to tidy my code, and have it working on many selected tracks.

I need to discover user input now... and maybe a basic GUI. I'll probably come back with some questions

Thanks,

Cheers
Cedrik0s is offline   Reply With Quote
Old 11-27-2014, 03:21 PM   #6
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

Quote:
Originally Posted by Cedrik0s View Post
Thanks again, spk77.

I managed to tidy my code, and have it working on many selected tracks.

I need to discover user input now... and maybe a basic GUI. I'll probably come back with some questions

Thanks,

Cheers
Glad you got it working
spk77 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 03:26 PM.


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