View Single Post
Old 11-16-2009, 05:17 PM   #4
airon
Human being with feelings
 
airon's Avatar
 
Join Date: Aug 2006
Location: Berlin
Posts: 11,817
Default

Here is the first version without putting notes in to the items as of now, but everything else works nicely.

A downloadable ZIP (~4kB) of both the script and the example spreadsheet.
https://stash.reaper.fm/oldsb/241778/...mpty_Items.zip

This code beneath is had been edited to keep within the 10kB posting limit. Just comment characters mostly.
Code:
#! /usr/bin/perl -w

use constant CURRENT_PROJECT => 0;

# Action command ids

@selected_track = 40939 .. 41037; # all the "Select Track xx" commands

use constant REMOVE_TIME_SELECTION_AND_LOOP_POINT_SELECTION => 40020;
use constant TIME_SELECTION_SET_START_POINT => 40625;
use constant TIME_SELECTION_SET_END_POINT   => 40626;
use constant INSERT_EMPTY_ITEM              => 40142;
use constant UNSELECT_ALL_ITEMS             => 40289;
use constant UNSELECT_ALL_TRACKS            => 40297;


## Section 1 and 2 - Loading the CSV ##

#
## The csv file is now hardcoded.
#  In the next revision we'll ask the user for a filename to enter
#  Perhaps we can pop up a file request at some point as well

$csv_file	=	'g:\Dev\ReaScript_CSV_to_Empty_Items\csv_data.csv';
$csv_delimiter	=	"\t";

$csv_fps = 25; # this will be configurable by checking for the FPS in the current project

$timecode_number_delimiter = ':';
$timecode_format_check =                             '\d\d' .
                         $timecode_number_delimiter . '\d\d' .
                         $timecode_number_delimiter . '\d\d' .
                         $timecode_number_delimiter . '\d\d'; # hh:mm:ss:ff
@csv_data					=	();
$csv_filesize     = ();
$csv_filesize_max	= 2**20 * 40 ; # 40 Megabytes. Hopefully this is an adequate upper limit
$csv_filesize_max_readable	= ();

## Section 3 - Checkups ##
##########################
@csv_file_line= ();
@csv_timecode_check_split = ();
$csv_column_description = 2;
$csv_column_track       = 3;
$csv_column_tcin        = 4;
$csv_column_tcout       = 5;

@timecodeslots 					= ($csv_column_tcin,$csv_column_tcout); # the slots our data lives in on each csv line
$timecode_ok_count = 0;


## Section 4 ##
###############
$csv_selected_track = ();
$reaper_time = ();



# Create an string from the maximum filesize that looks good for reports
if 				($csv_filesize_max < 2**10) {$csv_filesize_max_readable = $csv_filesize_max . " bytes\n";
	} elsif	($csv_filesize_max < 2**20) {$csv_filesize_max_readable = $csv_filesize_max / (2**10) . " kB\n";
	} elsif	($csv_filesize_max < 2**30) {$csv_filesize_max_readable = $csv_filesize_max / (2**20) . " MB\n";
	} elsif	($csv_filesize_max < 2**40) {$csv_filesize_max_readable = $csv_filesize_max / (2**30) . " GB\n";
	} else {
		reaprint ("This max filesize is too damn large at $csv_filesize_max_readable !");exit;
	}
#
##
###### end of variable initialization #####################
	




#### 1. Check CSV filesize and compain if it exceeds a given maximum

# Size check so we don't burn up too much memory with bogus data files
$csv_filesize = (stat($csv_file))[7];
if ($csv_filesize > $csv_filesize_max) {die "\nData file size exceeding maximum of $csv_filesize_max_readable\n\n"};



#### 2. Get the CSV Datafile


reaprint ("\n\nLoading file \"$csv_file\" ...\n");

open (DATAFILE,"$csv_file") || die "\nError: $!\n\n"; # open it safely
@csv_data = <DATAFILE>; # store it
chomp @csv_data; # remove linefeeds
close (DATAFILE) || die "\nError: $!\n\n"; # safely close it


#  [to be done]
#  Checks to make sure the project and data file have the same frame rate
#  Checks to make sure all other data is present, i.e. the data file has all the data cells we need

#### 3. Timecode integrity check in CSV data

##
reaprint ("Checking data integrity . . .\n");

## Checking

$i=1; # we start on the second line of @csv_data

until ($i eq scalar(@csv_data)) {
	# check timecode format
	@csv_file_line = split(/$csv_delimiter/, $csv_data[$i]);	# grab and split a line by the csv delimiter
	foreach $k (@timecodeslots) {
		# here we check for dd:dd:dd:dd for d=digits
		unless ($csv_file_line[$k] =~ /$timecode_format_check/)
				{die "Line " . $i+1 . " of the CSV file contained illegal timecode";} # format of timecode does not match expectations

		# here we check whether any of the numbers in the timecode have illegal values
		# get each of the numbers in the timecode
		@csv_timecode_check_split = split(/$timecode_number_delimiter/, $csv_file_line[$k]);

		# Hours cannot exceed 23, minutes 59, seconds 59 and frames the current frame rate - 1
		unless ($csv_timecode_check_split[0] < 24 ) { die "$k in line " . $i+1 . " contains an illegal HOURS number\n";}
		unless ($csv_timecode_check_split[1] < 60 ) { die "$k in line " . $i+1 . " contains an illegal MINUTES number\n";}
		unless ($csv_timecode_check_split[2] < 60 ) { die "$k in line " . $i+1 . " contains an illegal SECONDS number\n";}
		unless ($csv_timecode_check_split[3] < $csv_fps ) { die "$k in line " . $i+1 . " contains an illegal FRAMES number\n";}
		$timecode_ok_count++; # report back this resulting number
	}
	$i++; # next line
}
##
####### END of Timecode integrity check in CSV data ########

reaprint ("\nTimecodes that checked out ok : $timecode_ok_count\n");



#### 4. Create Empty Items

##
# RPR_Main_OnCommand(actionnumber, 0)
#
# Reaper commands we'll be using :
# Command name                         Cmd #
# ________________________________________________
# Track: Unselect all tracks           40297
# Track: Select track 01-99            40939-41037
# Time Selection: Remove time selection and loop point selection  40020
# Time Selection: Set start point      40625
# Time Selection: Set end  point       40626
# Insert empty item                    40142
# Item: Unselect all items             40289
#
#############################################
# Sequence of events for creating Empty items
#############################################
#
## 1. Clear time selection, loop selection, item selection and track selection(actions)
##
## 2. Select track specified in CSV file(action)
##
## 3. Select time selection start and end point 
##
## 4. Insert Empty item(action)
##
## 5. The item notes are changed (API call to get item info and another to set it)
##
## 6. Go to 1. until CSV list is processed
##
####
#### REMOVE_TIME_SELECTION_AND_LOOP_POINT_SELECTION => 40020;
#### TIME_SELECTION_SET_START_POINT => 40625;
#### TIME_SELECTION_SET_END_POINT   => 40626;
#### INSERT_EMPTY_ITEM              => 40142
#### UNSELECT_ALL_ITEMS             => 40289;
#### UNSELECT_ALL_TRACKS            => 40297;


## Preparation - Getting the data
#
#### 4.0 Setting up the loop and gathering the data

$i=1; # we start on the second line of @csv_data
for ($i=1; $i < scalar(@csv_data); $i++) {
	# Get the line and split it
	@csv_file_line = split(/$csv_delimiter/, $csv_data[$i]);	# grab and split a line by the csv delimiter
	
	
	## 4.1 Clear time selection, loop selection, item selection and track selection(actions)
	#
	RPR_Main_OnCommand(REMOVE_TIME_SELECTION_AND_LOOP_POINT_SELECTION, 0); # Remove time selection and loop point selection
	RPR_Main_OnCommand(UNSELECT_ALL_ITEMS, 0);	# Unselect all items
	RPR_Main_OnCommand(UNSELECT_ALL_TRACKS, 0); # Unselect all tracks

	## 4.2 Select track specified in CSV
	#
	$csv_selected_track = $selected_track[($csv_file_line[$csv_column_track])-1]; # tracks go from 1-99, the list goes from 0-98
	reaprint("Track $csv_file_line[$csv_column_track] selected.\n");
	RPR_Main_OnCommand($csv_selected_track, 0); # select the track chose in the CSV line


	## 4.3 Select time selection start and end point 
	#
	#RPR_SetEditCurPos2(CURRENT_PROJECT, double time, bool moveview, bool seekplay)
	$reaper_time = RPR_parse_timestr_pos($csv_file_line[$csv_column_tcin], 5); # get frame time code and convert to seconds, 5->this is frame-timecode
	RPR_SetEditCurPos2(CURRENT_PROJECT, $reaper_time, 0, 0);                   # set the cursor to the Timcode IN position
	RPR_Main_OnCommand(TIME_SELECTION_SET_START_POINT,0);                      # set the time selection start

	$reaper_time = RPR_parse_timestr_pos($csv_file_line[$csv_column_tcout], 5);# get frame time code and convert to seconds, 5->this is frame-timecode
	RPR_SetEditCurPos2(CURRENT_PROJECT, $reaper_time, 0, 0);                   # set the cursor to the Timcode OUT position
	RPR_Main_OnCommand(TIME_SELECTION_SET_END_POINT,0);                        # set the time selection end
	

	## 4.4 Insert Empty item(action)
	#
	RPR_Main_OnCommand(INSERT_EMPTY_ITEM,0); # insert the empty item

	## 4.5. The item notes are changed (API call to get item info and another to set it)
  #  Nothing there yet.
  #  Put the description in to the item notes
  get_and_set_item_state ($csv_file_line[$csv_column_description]);
  

  ############################1.23456789012345
  # Reaper timecode format :  0.00000000000000

}

# Cleanup
	RPR_Main_OnCommand(REMOVE_TIME_SELECTION_AND_LOOP_POINT_SELECTION, 0); # Remove time selection and loop point selection
	RPR_Main_OnCommand(UNSELECT_ALL_ITEMS, 0);	# Unselect all items
	RPR_Main_OnCommand(UNSELECT_ALL_TRACKS, 0); # Unselect all tracks

exit; # END OF MAIN PROGRAM

##############################################################################

sub reaprint {
	
	#print $_[0];
	RPR_ShowConsoleMsg($_[0]);
};



sub get_and_set_item_state {

  return; # this is not working yet. Skipping for now
}
__________________
Using Latch Preview (Video) - Faderport 16 setup for CSI 1.1 , CSI 3.10
Website
"My ego comes pre-shrunk" - Randy Thom
airon is offline   Reply With Quote