View Single Post
Old 04-18-2013, 11:05 AM   #91
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,514
Default

Here are two more scripts:



Implode items (simple version):
- should work if one item per track is selected
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("Implode selected items"):

    selItemCount = RPR_CountSelectedMediaItems(0)

    for i in range(1, selItemCount + 1):
        if i == 1:
            # get first selected item id/start pos/length and track index
            firstSelItem =  RPR_GetSelectedMediaItem(0, 0)
            firstSelItemPos = RPR_GetMediaItemInfo_Value(firstSelItem, "D_POSITION")
            firstSelItemLength = RPR_GetMediaItemInfo_Value(firstSelItem, "D_LENGTH")
            nextItemStart = firstSelItemPos + firstSelItemLength
            track = RPR_GetMediaItem_Track(firstSelItem)
            trackIndex = int(RPR_GetMediaTrackInfo_Value(track, "IP_TRACKNUMBER"))
        else:
            # move items
            itemId = RPR_GetSelectedMediaItem(0, i - 1)
            destTrack = RPR_CSurf_TrackFromID(trackIndex, 0)
            RPR_MoveMediaItemToTrack(itemId, destTrack)
            RPR_SetMediaItemPosition(itemId, nextItemStart, 0)
            nextItemStart = nextItemStart + RPR_GetMediaItemInfo_Value(itemId, "D_LENGTH")

    # update view
    RPR_TrackList_AdjustWindows(False)
    RPR_UpdateArrange()
Explode items:
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"):

    selItemIdL = []

    # get first selected item id/start pos and track index
    firstSelItem =  RPR_GetSelectedMediaItem(0, 0)
    firstSelItemPos = RPR_GetMediaItemInfo_Value(firstSelItem, "D_POSITION")

    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)):
            # insert new track(s) at the end
            RPR_InsertTrackAtIndex(RPR_CountTracks(0), 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)
        # set start pos to first sel item start
        RPR_SetMediaItemPosition(id, firstSelItemPos, 0)

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