e11world

AfterEffects ater react to masks

Nov 12th, 2023
44
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | Software | 0 0
  1. // Make Layers React to a Mask
  2. // Based on knowledge from The Power of Expression Book
  3. // https://aescripts.com/the-power-of-expression/
  4. //
  5. // Requires the new 'Javascript' expressions engine introduced in AE 16.0 (CC2019)
  6. // You can set this in File -> Project Settings: https://drop.aescripts.com/NQurXWmG
  7. // If you replace all "let" to "var" this expression will should work in previous versions
  8. //
  9. // v1 initial version
  10. // v2 added feathering support
  11. // v3 added parenting support
  12.  
  13. let maskToUse = thisComp.layer("MASK").mask("Mask 1").maskPath.points();
  14. let positionLayer = thisLayer.toComp([transform.anchorPoint[0],transform.anchorPoint[1]]);
  15.  
  16. let inside = function(point, path) {
  17. let x = point[0],
  18. y = point[1];
  19. let result = 0;
  20. let inside = false;
  21. for (let i = 0, j = path.length - 1; i < path.length; j = i++) {
  22. let xi = path[i][0],
  23. yi = path[i][1];
  24. let xj = path[j][0],
  25. yj = path[j][1];
  26. let intersect = ((yi > y) != (yj > y)) &&
  27. (x <
  28. (xj - xi) * (y - yi) / (yj - yi) + xi);
  29. if (intersect) inside = !inside;
  30.  
  31. let t1 = length(path[i],point);
  32. let t2 = length(path[result],point);
  33. if (t1 < t2){
  34. result = i;}
  35.  
  36.  
  37. }
  38. return [inside,result];
  39. };
  40.  
  41.  
  42. let lengthToLine = function(a, b, c) {
  43. let line = Math.pow(length(b,c),2);
  44. if (line === 0) return Math.pow(length(a,b),2);
  45. let d = ((a[0] - b[0]) * (c[0] - b[0]) + (a[1] - b[1]) * (c[1] - b[1])) / line;
  46. d = Math.max(0, Math.min(1, d));
  47. let distance = Math.pow(length(a, [ b[0] + d * (c[0] - b[0]), b[1] + d * (c[1] - b[1]) ]),2);
  48.  
  49. return Math.sqrt(distance);
  50. };
  51.  
  52. let r = inside(positionLayer, maskToUse);
  53. let final, distance1, distance2, result2;
  54. if (r[0] == true) {
  55. thisProperty.key(2).value;
  56. } else {
  57. let closest = r[1];
  58. let fallOff = thisComp.layer("MASK").mask("Mask 1").maskFeather[0];
  59. final = thisProperty.key(1).value;
  60.  
  61. if (fallOff!=0){
  62.  
  63. if (closest==0){
  64. distance1 = lengthToLine (positionLayer, maskToUse[closest], maskToUse[maskToUse.length-1]);
  65. distance2 = lengthToLine (positionLayer, maskToUse[closest], maskToUse[closest+1]);
  66. } else if ((closest+1)==maskToUse.length){
  67. distance1 = lengthToLine (positionLayer, maskToUse[closest], maskToUse[closest-1]);
  68. distance2 = lengthToLine (positionLayer, maskToUse[closest], maskToUse[0]);
  69. } else {
  70. distance1 = lengthToLine (positionLayer, maskToUse[closest], maskToUse[closest-1]);
  71. distance2 = lengthToLine (positionLayer, maskToUse[closest], maskToUse[closest+1]);
  72. };
  73.  
  74.  
  75. if (distance1 < distance2){
  76. result2 = distance1;
  77. }else{
  78. result2 = distance2;
  79. };
  80.  
  81.  
  82. final = linear(result2,0,fallOff,thisProperty.key(2).value,thisProperty.key(1).value);
  83. };
  84.  
  85. final;
  86.  
  87. };
Comments
  • e11world
    1 year
    # text 0.29 KB | 0 0
    1. https://aescripts.com/learn/how-to-make-layers-react-to-a-mask-in-after-effects/
    2.  
    3. v3 Expression now works with parenting as well as feather!
    4. IMPORTANT: This expression requires the new 'Javascript' expressions engine introduced in AE 16.0 (CC2019)
    5. You can set this in File -> Project Settings
Add Comment
Please, Sign In to add comment