Replace Footage Randomly

After Effects
April 4, 2025

/*
 Replaces selected layers with randomly ordered selected footage from the project panel,
 ensuring no two adjacent layers use the same footage.
*/

(function() {
   var comp = app.project.activeItem;
   if (!(comp && comp instanceof CompItem)) {
       alert("Please select an active composition.");
       return;
   }

   var selectedLayers = comp.selectedLayers;
   if (selectedLayers.length === 0) {
       alert("Please select at least one layer to replace.");
       return;
   }

   var selectedItems = app.project.selection;
   var footageItems = [];
   for (var i = 0; i < selectedItems.length; i++) {
       var item = selectedItems[i];
       if (item instanceof FootageItem) {
           footageItems.push(item);
       }
   }

   if (footageItems.length === 0) {
       alert("No footage items selected in the project panel.");
       return;
   }

   app.beginUndoGroup("Replace Footage Randomly");

   // Shuffle the footage items
   function shuffleArray(array) {
       for (var i = array.length - 1; i > 0; i--) {
           var j = Math.floor(Math.random() * (i + 1));
           var temp = array[i];
           array[i] = array[j];
           array[j] = temp;
       }
   }

   var footageSequence = [];
   while (footageSequence.length < selectedLayers.length) {
       var shuffledFootage = footageItems.slice();
       shuffleArray(shuffledFootage);
       footageSequence = footageSequence.concat(shuffledFootage);
   }

   // Assign footage to layers
   for (var j = 0; j < selectedLayers.length; j++) {
       var layer = selectedLayers[j];
       var currentFootage = footageSequence[j];

       // Ensure no two adjacent layers have the same footage
       if (j > 0 && currentFootage === footageSequence[j - 1]) {
           // Swap with the next item if possible
           if (j + 1 < footageSequence.length) {
               var temp = footageSequence[j];
               footageSequence[j] = footageSequence[j + 1];
               footageSequence[j + 1] = temp;
               currentFootage = footageSequence[j];
           }
       }

       layer.replaceSource(currentFootage, false);
   }

   app.endUndoGroup();
})();

How to use the script

Always check code before running it on your computer. Even basic scripts like this one. If you’re not a developer, ask an LLM like ChatGPT, Claude, or Gemini to verify it for you.

Option 1: Install to your AE Folder

Download and install the below .jsx file in your After Effects Script folder. You can then run it using the scripts menu, or by using a launcher like Quick Menu 3.

Option 2: Adapt the script yourself

Alternatively copy the code below and develop your own version. If you’re not a developer you can do this easily with an LLM like ChatGPT.

How to use the script

Always check code before running it on your computer. Even basic scripts like this one. If you’re not a developer, ask an LLM like ChatGPT, Claude, or Gemini to verify it for you.

Option 1: Install to your AE Folder

Download and install the below .jsx file in your After Effects Script folder. You can then run it using the scripts menu, or by using a launcher like Quick Menu 3.

Option 2: Adapt the script yourself

Alternatively copy the code below and develop your own version. If you’re not a developer you can do this easily with an LLM like ChatGPT.

Research, interviews, and tools for modern motion designers.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.