View Single Post
Old 04-17-2013, 09:51 AM   #86
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,514
Default

Ok, now it creates new tracks if needed.



Code:
from reaper_python import *
from contextlib import contextmanager

@contextmanager
def undoable(message):
    RPR_Undo_BeginBlock2(0)
    try:
        yield
    finally:
        RPR_Undo_EndBlock2(0, message, -1)

with undoable("Spread selected items over tracks - add tracks if needed"):

    def msg(m):
        RPR_ShowConsoleMsg(m)

    selItemIdL = []

    # get first selected item and track index
    firstSelItem =  RPR_GetSelectedMediaItem(0, 0)
    track = RPR_GetMediaItem_Track(firstSelItem)
    trackIndex = int(RPR_GetMediaTrackInfo_Value(track, "IP_TRACKNUMBER"))
    trackItemCount = RPR_CountTrackMediaItems(track)

    # make list from selected items in track
    for i in range(trackItemCount):
        itemId = RPR_GetTrackMediaItem(track, i)
        if int(RPR_GetMediaItemInfo_Value(itemId, "B_UISEL")):
            selItemIdL.append(itemId)

    # check if there are enough tracks for items, create more tracks if needed
    needed = int(RPR_CountTracks(0) - trackIndex + 1 - len(selItemIdL))
    if needed < 0:
        for tr in range(abs(needed)):
            RPR_InsertTrackAtIndex(trackIndex, 1)

    # check if next tracks are empty
    for t in range(len(selItemIdL) - 1):
        # create new track if there are already items in "destination" track
        if RPR_CountTrackMediaItems(RPR_GetTrack(0, trackIndex + t)) > 0:
            RPR_InsertTrackAtIndex(trackIndex, 1)

    # move each item to its own track
    for j, id in enumerate(selItemIdL):
        destTrack = RPR_CSurf_TrackFromID(trackIndex + j, 0)
        RPR_MoveMediaItemToTrack(id, destTrack)

    # update view
    RPR_TrackList_AdjustWindows(False)
    RPR_UpdateArrange()
spk77 is offline   Reply With Quote