ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Importing unique files only

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Rodrigo Aben
Importing unique files only
on Jul 26, 2020 at 3:13:54 am
Last Edited By Rodrigo Aben on Jul 26, 2020 at 3:18:14 am

Hey guys. I've been struggling with this one for days. I'm getting all imported file's paths, storing then in a array and (trying) to only import any new files that are not in the project. Except it reimports all files everytime and not the unique ones. I'm yet to filter for folders and all that, but I'm sure they are all valid files. Probably missing a simple detail...

for (var i = 0; i &lt; myUniqueFolders.length; i++){

function ImportUniqueFiles(){
var folder = new Folder;
folder = Folder(myUniqueFolders[i]);

var files = folder.getFiles();

var myImportedFiles = [];

for (var k = 1; k &lt;= myProj.numItems; k++) {
var thisFile = app.project.item(k).file;
myImportedFiles.push(thisFile);
alert(myImportedFiles.length);
}

importFiles(files);

function importFiles(files) {
for(var j = 0; j &lt; files.length; j++) {
if(myImportedFiles.indexOf(files[j]) == -1){
alert(files[j]);
app.project.importFile(new ImportOptions(files[j]));
}else{
alert("not unique");
}
}
}
}

}


Return to posts index

Filip Vandueren
Re: Importing unique files only
on Jul 26, 2020 at 8:30:25 am

Hi Rodrigo,

the code you posted is a bit incomplete, for example ImportUniqueFiles() never gets called so it wouldn't import anything.

Nesting functions in functions and in loops isn't my style BTW, but I kept it here.

Anyway; you're creating file() objects in two different arrays.
While these file objects may share the same path, point to the same file on the filesystem, they are not equal to each other on an object-level, that's why indexOf() doesn't find the files of the folder in the array of footage-items.

It would be better to compare their .fsName, . fullName or . absoluteURI, those are string-representations of their Path and indexOf() would function more predictably when it's comparing values of strings


myUniqueFolders = ["~/Desktop/"];
myProj = app.project;

for (var i = 0; i < myUniqueFolders.length; i++){

// function ImportUniqueFiles(){
var folder = new Folder;
folder = Folder(myUniqueFolders[i]);

var files = folder.getFiles();
// convert the list of File-Objects to a list of Strings of their Paths
files = files.map( function (f) { return f.absoluteURI });

myImportedFiles = [];

for (var k = 1; k <= myProj.numItems; k++) {
var thisFile = app.project.item(k).file;
// many compItems don't have a file as source
if (thisFile !== null && thisFile !== undefined) {
myImportedFiles.push(thisFile.absoluteURI);
}
}
alert(myImportedFiles.length);

importFiles(files);

function importFiles(files) {
for(var j = 0; j < files.length; j++) {
$.writeln(myImportedFiles.indexOf(files[j]));
if(myImportedFiles.indexOf(files[j]) == -1){
alert(files[j]);
app.project.importFile(new ImportOptions(files[j]));
}else{
alert("not unique");
}
}
}
//}

}




Return to posts index

Rodrigo Aben
Re: Importing unique files only
on Jul 27, 2020 at 12:03:17 am

Great solution, Filip! Thank you very much. I had no idea they would not be considered equal. Could you elaborate more on that?

BTW I would also like to avoid nesting functions and loops, but since folder.getFiles() only works one folder at a time, running this function seemed to me the simplest way of looping through all folders.

My full code below in case you see any room for improvement (and you probably will).

Thank you very much once again.

var myProj = app.project;

//creates an array of all file's folders

var myAssetspath = [];

for (var i = 1; i &lt;= myProj.numItems; i++) {
var filesPath = app.project.item(i).file.fsName.replace(app.project.item(i).name, "");
myAssetspath.push(filesPath);
}

//this sorts the array

var mySortedFolders = myAssetspath.sort();

//removes duplicate folders

var myUniqueFolders = [];

for (var i = 0; i &lt;= mySortedFolders.length; i++){
if(mySortedFolders[i] != mySortedFolders[i+1]){
myUniqueFolders.push(mySortedFolders[i]);
}
}

// gets files from all unique folders and tests if they're already imported. If not, imports them

for (var i = 0; i &lt; myUniqueFolders.length; i++){

// function ImportUniqueFiles(){
var folder = new Folder;
folder = Folder(myUniqueFolders[i]);

var files = folder.getFiles();
// convert the list of File-Objects to a list of Strings of their Paths
files = files.map( function (f) { return f.absoluteURI });

myImportedFiles = [];

for (var k = 1; k &lt;= myProj.numItems; k++) {
var thisFile = app.project.item(k).file;
// many compItems don't have a file as source
if (thisFile !== null && thisFile !== undefined) {
myImportedFiles.push(thisFile.absoluteURI);
}
}
alert(myImportedFiles.length);

importFiles(files);

function importFiles(files) {
for(var j = 0; j &lt; files.length; j++) {
$.writeln(myImportedFiles.indexOf(files[j]));
if(myImportedFiles.indexOf(files[j]) == -1){
alert(files[j]);
app.project.importFile(new ImportOptions(files[j]));
}else{
alert("not unique");
}
}
}
}


Return to posts index


Rodrigo Aben
Re: Importing unique files only
on Jul 27, 2020 at 12:14:28 am

Sorry for bothering you too much, but I know I'll face a whole new problem in case the "unique" file detected is part of a image sequence. Any insights on how to go about that?


Return to posts index

Filip Vandueren
Re: Importing unique files only
on Jul 27, 2020 at 8:48:40 am
Last Edited By Filip Vandueren on Jul 27, 2020 at 8:59:25 am

Yeah, that's a problem for sure.
There are two methods of the file-object in After-Effects but they are undocumented and for me they don't seem to work:

app.project.item(1).file.isFrameSequence()
-> always returns false for me.

and

app.project.item(1).fileframe.InSequence()
-> always returns the path of the first frame for me.

Bummer…

I'm on a mac, perhaps it works on PC ?
And like I said, I can't find any documentation on these methods, maybe they expect a parameter ?



Return to posts index

Filip Vandueren
Re: Importing unique files only
on Jul 27, 2020 at 8:53:30 am

About the two file Objects not considered equal:

Two objects are never equal, even if they share all of the same methods and properties and values, they occupy two different parts of memory and can be changed independently.


a= {name: "Rodrigo"};
b= {name: "Rodrigo"};

a == b; // -> False !
a.name == b.name // -> True ☺

c = {name: "Filip"};
d = c;

c == d; // -> True! They both point to the same object at the same memory-location


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2020 CreativeCOW.net All Rights Reserved
[TOP]