Expressions/Scripts/Presets

Moderator: Paul Tuersley

 
SpencerT
Topic Author
Posts: 8
Joined: Mon Mar 29, 2010 5:08 pm

Image sequence array concatenation - need optimization help!

Tue Aug 30, 2016 11:40 am

Help needed!

I have a script I've been using/developing for some time now which greatly simplifies importing multiple image sequences (mainly for CG renders with render passes). Everything is working great but there's one specific area that runs really slowly when you start getting into thousands of images and I'm trying to figure out how to speed it up.

What I am trying to optimize is going through an array of potentially thousands of image files, and concatenating it down to just the first image in the sequence. For example my array might be [img.001.png, img.002.png, img.003.png, img.004.png, img.005.png] and what I want to do is remove every instance of "img.###.png" except for the first one. So after my function I would have just: [img.001.png].

Here's how I do it right now:
1) I take array item A, regex out the number sequence
2) then array item B, regex out the number sequence
3) and compare the two to see if they are identical. If they are I splice out item B

This works 100% of the time which is awesome, but is quite slow when you get above a thousand or two images... What I'm trying to do is find a way to do this "pruning" step more quickly. A friend of mine told me regex is pretty slow so maybe I need to do it without regex? I also found several native JavaScript array methods that might help such as forEach() every() and filter() but I don't see how these are going to be faster because I still need to do 2 regex evaluations, and I still need to compare the items to each other.

Any help would be immensely appreciated! And if anyone wants my script just ask, I'm definitely not planning on charging for it.

- Spencer


Here's a snippet of my code:
currentFolder = new Folder ("//12.34.5.67/my folder on the network/")
var folderChildren = currentFolder.getFiles().sort();

var searcher = new RegExp("\\d{3,5}[.]");
for (var i = 0; i < folderChildren.length; i++) {
    // Go through the array and strip out all elements that are the same once their numbers have been removed with a regex
    if (i > 0) {
        currentResult = searcher.exec(folderChildren[i].name); //check if a sequence
        if (currentResult) { // it is a sequence
            // first parse out the comparison strings - current item and item before
            var testNameBefore = folderChildren[i-1].name;

            //if we have a sequence before our current item, we need to delete the numbers.
            var beforeNum = searcher.exec(testNameBefore);
            if (beforeNum) {
                testNameBefore = testNameBefore.substring(0, testNameBefore.length-8);
            }

            var testNameCurrent = folderChildren[i].name;
            testNameCurrent = folderChildren[i].name.substring(0, testNameCurrent.length-8);

            //compare to the element before it and delete if the same!!
            if (testNameBefore == testNameCurrent) {
                folderChildren.splice(i, 1);
                i--;
            }
        }
    }
}

Who is online

Users browsing this forum: No registered users and 1 guest