2

I got this code from motionscript and I would like to change it to check a specific layer instead of all the layers for a collision. Maybe within the function checklayers() and then there's the if checklayers() at the last line ??

function getMin(a, b, c, d){
  return Math.min(Math.min(a,b),Math.min(c,d));
}

function getMax(a, b, c, d){
  return Math.max(Math.max(a,b),Math.max(c,d));
}

function getBoundingBox(theLayer){
  bb = [];
  c1 = theLayer.toWorld([0,0]);
  c2 = theLayer.toWorld([theLayer.width,0]);
  c3 = theLayer.toWorld([theLayer.width,theLayer.height]);
  c4 = theLayer.toWorld([0,theLayer.height]);
  bb[0] = getMin(c1[0],c2[0],c3[0],c4[0]);
  bb[1] = getMin(c1[1],c2[1],c3[1],c4[1]);
  bb[2] = getMax(c1[0],c2[0],c3[0],c4[0]);
  bb[3] = getMax(c1[1],c2[1],c3[1],c4[1]);
  return bb;
}

function checkLayers(){
  cUL = [];
  cLR = [];
  for ( idx = 1; idx <= thisComp.numLayers; idx++){
    if (index == idx) continue;
    L = thisComp.layer(idx);
    if (! L.active) continue;
    BB = getBoundingBox(L);
    UL = [BB[0],BB[1]];
    LR = [BB[2],BB[3]];

    if (!(myLR[1] < UL[1] || LR[1] < myUL[1] ||
          myLR[0] < UL[0] || LR[0] < myUL[0])){

      if (myUL[1] < UL[1]) cUL[1]=UL[1] else cUL[1]=myUL[1];
      if (myUL[0] < UL[0]) cUL[0]=UL[0] else cUL[0]=myUL[0];
      if (myLR[1] < LR[1]) cLR[1]=myLR[1] else cLR[1]=LR[1];
      if (myLR[0] < LR[0]) cLR[0]=myLR[0] else cLR[0]=LR[0];

      for(i = cUL[0]; i <= cLR[0]; i++){
        for (j = cUL[1]; j <= cLR[1]; j++){
          if (sampleImage(fromWorld([i,j]))[3] > 0 &&
              L.sampleImage(L.fromWorld([i,j]))[3] > 0){

            return true;
          }
        }
      }
    }
  }
  return false;
}

myBB = getBoundingBox(thisLayer);
myUL = [myBB[0],myBB[1]];
myLR = [myBB[2],myBB[3]];

if (checkLayers()) 1 else 0
3

Yes, checkLayers() is what is performs the collision test, and compares every layer in the Comp against the one running the script.

To check against only one layer, you could re-write that function a little cleaner, but here's a quick solution.

First, remove the lines that start and end the for loop, since you'll only be checking one layer. But don't remove the code inside the for loop, since that code is what does the collision check.

for ( idx = 1; idx <= thisComp.numLayers; idx++){  // delete
    ... // keep this stuff
} // delete

You can also delete these two lines, since you don't need to check every layer in the Comp for whether it's the same as the layer running the script, or whether the layer is active or not:

if (index == idx) continue; // skips if the comparison layer is the same as the current layer
if (! L.active) continue;   // skips if the comparison layer is inactive

Last, modify this line:

L = thisComp.layer(idx);

… to make L the one layer that you want to compare to. You might do that by referencing a layer by index or name, such as:

L = thisComp.layer("Name of Single Layer to Compare");

Check the Adobe documentation on Comp attributes and methods to see other options for referencing a specific layer.

You do need the if (checkLayers()) 1 else 0 line at the end, because that's the “switch” that returns the final value of your expression, depending on whether there was a collision (returns 1) or not (returns 0).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.