Page 1 of 1

Get file name of img seq except frame padding?

Posted: October 10th, 2011, 3:01 am
by Simma
Is there a way to get the file name of a image sequence, minus the frame padding? For example, the name of a file could be any of the following (and more):

temp/shot01_####.dpx
temp/shot01_#######.dpx
temp/shot01.#.dpx

How would I do to find just "shot01"? It would be hard to search the string and just ignore those characters, as there are so many possible naming conventions.

Also, is there a way to find the number of the first frame? For example, a image sequence could be imported as shot01_[0030-0050].dpx into AE, how would I know could I tell AE that the first frame is frame 30? Same problem here, as there are a number of possible naming conventions, it could just as well be shot01_[0000030-0000050].dpx.

Thank you.

Re: Get file name of img seq except frame padding?

Posted: October 14th, 2011, 3:05 pm
by zold
Simma wrote: temp/shot01_####.dpx
temp/shot01_#######.dpx
temp/shot01.#.dpx

How would I do to find just "shot01"? It would be hard to search the string and just ignore those characters, as there are so many possible naming conventions.
For the first problem, you could do something like the following:
Two step process:
first, you isolate the file name:

Code: Select all

fName = fullPath.replace(/\\/g,'/').replace( /.*\//, '' );
This gives you the file name, with the extension. Then,

Code: Select all

baseName = fName.split('.')[0];
gives you the basename w/o extension.
Of course, this has no error correction, if the file does not have a file extension.
Also, is there a way to find the number of the first frame? For example, a image sequence could be imported as shot01_[0030-0050].dpx into AE, how would I know could I tell AE that the first frame is frame 30? Same problem here, as there are a number of possible naming conventions, it could just as well be shot01_[0000030-0000050].dpx.
.
Let me make sure I understand. You want to find the beginning number of an imported file sequence?
If you have already have the sequence in a variable like "thisFileSeq":

Code: Select all

firstFileName = thisFileSeq.file.name;
This gives you the file name of the first file of the sequence. Then, if you need the number, you can do:

Code: Select all

firstFileName.split('.')[0].split("_").pop();
This will ONLY work if there is an underscore in the name before the number.
Looking for the number without the underscore gets quite a bit more complicated.

-crg

Re: Get file name of img seq except frame padding?

Posted: October 15th, 2011, 7:52 am
by Simma
Thanks for your reply zold. I played some with your suggestion but didn't get it to work. I might have done something wrong though, as it gave me the full file name.

I think I got it to work with this code in most cases at least:

Code: Select all

var fileName = "shot01_01_007";

// Put every character in an array.
var myArray = new Array();
    for (var i = 0; i <= fileName.length - 1; i++){
    myArray[myArray.length] = fileName.charAt(i -1 );
    }

// Loop through the array (backwards) until a a character that isn't a number is found.
    for(var i = myArray.length - 1; i>=0; i--){
        var character = myArray[i].toString();
        var noNumber = isNaN(character);
        if(noNumber == true || character == "."){
        var theNoNumber = i;
        break;
        }
    }

// Subtract the frame padding from the filename.
var subtractNumber = myArray.length - theNoNumber + 1;
var myFileName = fileName.substring(0,fileName.length-subtractNumber);

alert("The filename without framepadding is: " + myFileName);

var firstFrame = parseFloat(fileName.slice(theNoNumber));

alert("The firsframe of the sequence is: " + firstFrame);



Re: Get file name of img seq except frame padding?

Posted: October 18th, 2011, 8:34 am
by zold
[UPDATE: Okay I just realized that using parseInt() is a very bad idea in this case, as using "padded" zeros is interpreted as octal (see http://www.w3schools.com/jsref/jsref_parseInt.asp -- "If the string begins with "0", the radix is 8 (octal). This feature is deprecated" [so even though it is deprecated, it still does this])
SORRY ABOUT THAT.

Simma wrote:Thanks for your reply zold. I played some with your suggestion but didn't get it to work. I might have done something wrong though, as it gave me the full file name.
"it" refers to what? Which suggestion didn't work? Running this in AE works for me, but maybe I'm missing something:

Code: Select all

n = "shot01_01_007".split('.')[0].split("_").pop();
//returns "007"
//then, to 'strip' the leading zeros:
//makes "007" into an integer, then back to string:
[UPDATE: DO NOT USE THIS TECHNIQUE; SEE ABOVE!]

Code: Select all

n = (parseInt(n).toString());
alert(n);//should show "7"
[2nd UPDATE: THIS IS WHAT YOU SHOULD USE. PARSEFLOAT, NOT PARSEINT:]

Code: Select all

n = (parseFloat(n).toString());
alert(n);//should show "7"
Cheers!

Re: Get file name of img seq except frame padding?

Posted: October 18th, 2011, 5:49 pm
by zold
and you could also use the regular expression solution (but if you want a number, parseInt it):

Code: Select all

n = "000100";
nAsString = (n.replace(/^[0]+/g,""));
//100 as string ("100")
nAsInteger = parseInt(nAsString);
// 100 as integer

Re: Get file name of img seq except frame padding?

Posted: October 24th, 2011, 2:07 pm
by lloydalvarez
parseInt has an optional radix parameter (http://www.w3schools.com/jsref/jsref_parseInt.asp) so you would simply tell it you want it to parse the integer on a base of 10 like so:

parseInt("00007",10); //result: 7


I think having leading zero's is an undocumented way of defining an octal base which is probably why you are getting the unusual output. I would say it is good practice to always define the base when using parseInt. Also, if what you want is an integer use parseInt instead of parseFloat.

-Lloyd

Re: Get file name of img seq except frame padding?

Posted: January 18th, 2012, 5:28 am
by Alan Eddie
Hi there, before I found this post I was working with some of these problems.
This is some code I had generated, I just said I would post it here for reference.



var testRegExp = new RegExp ("[0-9]+\.[A-Za-z]{1,3}$");
//will return 0023.tga for example...

var testString = app.project.item(i).mainSource.file.displayName;
//DO WE HAVE NUMBERS IN THE FILE THAT LOOK LIKE A SEQUENCE..?
var x =testRegExp.test(app.project.item(i).mainSource.file.displayName);

// IF WE DO , REPLACE THE MATCH WITH NOTHING...+ (seq)
if ( x == true){
//CURRENT STRING FOR ITEM
var curName =app.project.item(i).name;
//THE NEW STRING FOR ITEM
var newName = curName.replace(testRegExp," (Seq)");
//ASSIGN THE NEW STRING TO THE ITEM.
app.project.item(i).name = newName;
}//CLOSE IF.