Import Image or A Folder Of Images Becomes A Comp

What type of scripts do you need?

Moderator: byronnash

Post Reply
User avatar
Atomic
Posts: 157
Joined: April 30th, 2007, 5:55 am
Location: United States, Ohio

Hi All,

I have searched and searched this site, but I can not find the answer to my simple question.

I need a script that will import a folder of images and make a comp out of it with each image in the folder becoming a layer in the newly created comp.

Thanks :wink:
User avatar
Atomic
Posts: 157
Joined: April 30th, 2007, 5:55 am
Location: United States, Ohio

Hi All,

I guess I have found an answer to my own question in this topic here:

viewtopic.php?t=102

I also updated this script to do a little more. I call it "Folder Of Titled Images." Not only does it import a folder of images into your existing comp, it also adds a companion layer for each image in the folder that is an animated text object that moves from left to right with a 4 second sustain so you can read the text. Currently it uses the name of the image for the text it creates, but it could be modified to do more.

There is a gotcha that is inherited from the original script (read comment "just to clairify") so beware of the naming of your comp before you execute this script or it will fail after importing the files and not create your layers.

Code: Select all

 //Byron Nash 10/2004
 //Revised 7/17/2007 Atom
 
 // Import functions borrowed from AE scripting examples.

 /* 
 
 This script imports a folder of images and sequences them in a predestined comp.
 It also sets ALL comps in the project to be the length of the new sequenced comp.
 This is useful if you are versioning a bunch of logos into the same graphic bed.
 
 Additions By Atom:
 It will now create a companion text layer that is the name of the image you are viewing. 
 It also auto animates the text from left to right. 
 Often, I need text for every image so I thought I'd update this script to do this automatically.
 
 Instructions:
 1. Make sure you have a project with a comp at the very top. Put a "1" in front of the name if need be to keep it the first object in the list.
 2. Run script
 3. Choose a folder of files when prompted
 
  Just to clarify, this script runs in passes, so it will import a series of image, then create layers from that import. 
  Because this script operates on an existing comp, it does no know the name of the comp, so it references the first object in the project window as the comp.
  Consider this, you create default comp called "Comp 1" then you import a series of images froma folder. The folder contains an image called "blueprint.jpg". 
  Now blueprint.jpg is above Comp 1 in the project list and the script will try to operate on the JPG as if it were a comp. 
  This is what causes the error if you do not put a 1 in front of the comp name. You might want to put a zero. 
  If an image comes in with a 0 instead of a 1 as it's first character you could also error out.
  It's not perfect, but it took me a while to figure this out so I thought I'd mention why it will error out and how to avoid this.
  
  Limitation/ Flaws:
	I already mentioned the naming of the comp flaw in this script.
	I have hard coded the comp size and height to 720x480.
	The duration of each imported image is given 5 seconds, you can adjust this by changing the variable, below.
	The frame rate is calculated at 29.97 fps so if you are using another rate please change this value as well.
 */
 {
 // create undo group

app.beginUndoGroup("Import and Sequence");



//Ask the user for a folder whose contents are to be imported

var targetFolder = folderGetDialog("Import Items from Folder..."); //returns a folder or null
if (targetFolder) {

	// if no project open, create a new project to toss the files into. [23839]
	if (!app.project) {
		app.newProject();
	}

	function processFile (theFile) {
		try {
			var importOptions = new ImportOptions (theFile); //create a variable containing ImportOptions
			importSafeWithError (importOptions);
		} catch( error ) {
			// eat errors.
		}
	}
	
	function testForSequence (files){
		var searcher = new RegExp ("[0-9]+");
		var movieFileSearcher = new RegExp ("(mov|avi|mpg)$", "i");
		var parseResults = new Array;
		
		for (x = 0; (x < files.length) & x < 10; x++) { //test that we have a sequence, stop parsing after 10 files
			
			var movieFileResult = movieFileSearcher.exec(files[x].name);
			if (! movieFileResult) {
			
				var currentResult = searcher.exec(files[x].name);
				//regular expressions return null if no match was found
				//otherwise they return an array with the following information:
				//array[0] = the matched string
				//array[1..n] = the matched capturing parentheses
			
				if (currentResult) { //we have a match - the string contains numbers
					//the match of those numbers is stored in the array[1]
					//take that number and save it into parseResults
					parseResults[parseResults.length] = currentResult[0];
				}
				else {
					parseResults[parseResults.length] = null;
				}
			}
			else {
				parseResults[parseResults.length] = null;
			}
		}
		
		//if all the files we just went through have a number in their file names, 
		//assume they are part of a sequence & return the first file
		
		var result = null;
		for (i = 0; i < parseResults.length; ++i) {
			if (parseResults[i]) {
				if (! result) {
					result = files[i];		
				}	
			} else {
				//case in which a file name did not contain a number
				result = null;
				break;
			}
		}
		return result;
	}

	function importSafeWithError (importOptions) {
		try { 
			app.project.importFile (importOptions);
		} catch (error) {
			alert(error.toString() + importOptions.file.fsName);
		}
	}

	function processFolder(theFolder) {
		var files = theFolder.getFiles(); //Get an array of files in the target folder
			
		//test whether theFolder contains a sequence
		var sequenceStartFile = testForSequence(files);
		
		//if it does contain a sequence, import the sequence
		if (sequenceStartFile) {
			try {
				var importOptions = new ImportOptions (sequenceStartFile); //create a variable containing ImportOptions
				
				importOptions.sequence = true;
				//importOptions.forceAlphabetical = true; //un-comment this if you want to force alpha order by default
				importSafeWithError (importOptions);
			} catch (error) {
			
			}
		}
		
		//otherwise, import the files and recurse
		
		for (index in files) { //Go through the array and set each element to singleFile, then run the following
			if (files[index] instanceof File) {
				if (! sequenceStartFile) { //if file is already part of a sequence, don't import it individually
					processFile (files[index]); //calls the processFile function above
				}
			}
			if (files[index] instanceof Folder) {
				processFolder (files[index]); // recursion
			}
		}
	}
	
	processFolder(targetFolder);
}

//Recursively examine that folder
var logoSel = app.project.selection;				//set selected import items to an array
var logoComp = app.project.item(1);				//select the logo comp

//Begin Atom changes. 7/17/07.
var compW = 720; 							// comp width
var compH = 480; 							// comp height
var perImageDuration= 150;						//Frames divided by frame rate equals seconds.
logoComp.duration = perImageDuration/29.97 			//set comp length to 5 seconds

for (i=0; i < logoSel.length; i++){			//loop through and add layers to comp
  
	//Add to logo comp
	if(logoSel[i] instanceof FootageItem){	//test to be sure it's not a comp or folder
	var lcoll = logoComp.layers;			//variable for collection of layer objects in logoComp
	lcoll.add(logoSel[i]);				//add layer
	//alert (logoSel[i].name);
        thisTextLayer = lcoll.addText(logoSel[i].name);
	//thisTextLayer.startTime = myCount;//set in point 
        //thisTextLayer.outPoint= myCount + myLength;
	}	
}
//Sequence Layers out every 1 frame and turn on Collapse transformations
var curIn = 0;						//initialize in point variable

var i = 1;
//Loop through layers
do
{
	//Now do the text layer.
	var curLayer = lcoll[i];
        curLayer.startTime = curIn;			//set in point 
        var curOut = curLayer.outPoint;		//set out
	
	//Auto animate the text from left to right. (stays on screen for 4 seconds transitions in and out for 1 second).
	myPosition = curLayer.property("position"); 
	myPosition.setValueAtTime(curIn,[-compW,compH/2]); 
	myPosition.setValueAtTime(curIn + 0.5,[compW/2,compH/2]); 
	myPosition.setValueAtTime(curOut - 0.5,[compW/2,compH/2]);	//Effectively creates a hold frame, can cause problems on interlace footage.
	myPosition.setValueAtTime(curOut,[compW,compH/2]); 
	
	//Now do the image layer.
	i++;
	var curLayer = lcoll[i];
        curLayer.startTime = curIn;			//set in point 
        var curOut = curLayer.outPoint;		//set out


	curIn = curOut 					//increment in point
	if(curLayer.canSetCollapseTransformation == true)
	{//check to see if layer can be collapsed 
		curLayer.collapseTransformation = true//collapse layer
	}
	i++;
}while(i<lcoll.length);
//End Atom Changes 7/17/07.
        
// Set Final Comp to correct length.
logoComp.duration = lcoll[lcoll.length].outPoint;	//set comp to end at last layer out 
compDur = logoComp.duration;			//set variable for all comps
var stuff = app.project.items;			//get collection of project items
for (i=1; i <= stuff.length; i++)
{//loop through project
	if(stuff[i] instanceof CompItem) 
	{//check for comp
		stuff[i].duration = compDur;		//set comp length
	}
} 
alert("Finished Importing and Sequencing of Images. Be sure to save the project as a new name.")
app.endUndoGroup();
}	
Post Reply