View Single Post
Old 12-29-2012, 04:38 PM   #2
mwe
Human being with feelings
 
mwe's Avatar
 
Join Date: Mar 2012
Location: Kentucky, USA
Posts: 254
Default

This script uses SNM functions from the beta SWS builds. It will not work with the general release build or beta builds earlier than #7. The next build could also break it. I tested using Reaper x64 v4.31 and 4.32pre8.

Please make comments/suggestions/improvements. I'd especially like feedback on the best way to deal with fast string handling.

Code:
from reaper_python import * 
from sws_python import * 
from contextlib import contextmanager

@contextmanager
def undoable(message):
    RPR_Undo_BeginBlock2(0)
    try:
        yield
    finally:
        RPR_Undo_EndBlock2(0,message,-1)
		
def getMarkerName(item_start):
	marker_idx = 0
	marker = (RPR_EnumProjectMarkers2(0,marker_idx,0,0,0,0,0))

	while ((marker[0] != 0) and (marker[4] < item_start)) :
		marker_idx += 1
		marker = (RPR_EnumProjectMarkers2(0,marker_idx,0,0,0,0,0))
	marker_ID = marker[7]
	try:
		fast_Str = SNM_CreateFastString("")
		SNM_GetProjectMarkerName(0, marker_ID, False, fast_Str)
		marker_name = SNM_GetFastString(fast_Str)
	finally:
		SNM_DeleteFastString(fast_Str)	
	return(marker_name)

with undoable('Insert region from selected item using marker name'):	
	for i in range (RPR_CountSelectedMediaItems(0)):	
		selected_item = RPR_GetSelectedMediaItem(0, i)
		item_start = RPR_GetMediaItemInfo_Value(selected_item, 'D_POSITION')
		item_end = item_start + RPR_GetMediaItemInfo_Value(selected_item, 'D_LENGTH')
		marker_name = getMarkerName(item_start)
		track_color = RPR_GetTrackColor(RPR_GetMediaItem_Track(selected_item))

		RPR_AddProjectMarker2(0, True, item_start, item_end, marker_name, 0, track_color)
	RPR_UpdateTimeline()

Last edited by mwe; 12-30-2012 at 01:04 AM. Reason: Cleaned things up a little more
mwe is offline   Reply With Quote