Skip to main content
Inspiring
May 6, 2023
Answered

This script replaces all instances of one effect with another. But it doesn't work. Help!

  • May 6, 2023
  • 1 reply
  • 1055 views

Hi,

 

I'm using AE 2022, which is the maximum version I can use on the job I'm working on. I have this script which is intended to find all layers with Gaussian Blur in the active comp, turn off Gaussian Blur, then add Camera Lens Blur. The problem is it doesn't work. I don't get any error messages either. I'm curious to know where I went wrong. 

 

// get the active composition
var comp = app.project.activeItem;

// check if a composition is active
if (comp != null && comp instanceof CompItem) {
  // loop through all layers in the composition
  for (var i = 1; i <= comp.numLayers; i++) {
    // get the layer
    var layer = comp.layer(i);

    // check if the layer has effects
    if (layer.effect) {
      // loop through all effects applied to the layer
      for (var j = 1; j <= layer.effect.numProperties; j++) {
        // get the effect
        var effect = layer.effect(j);

        // check if the effect is Gaussian Blur
        if (effect.displayName == "Gaussian Blur") {

          // disable Gaussian Blur
          effect.enabled = false;

          // create a new Camera Lens Blur effect
          var newEffect = layer.effects.addProperty("Camera Lens Blur");

          // exit the loop since we've modified the layer
          break;
        }
      }
    }
  }
} else {
  alert("No composition is active.");
}

 

Ultimately, I'd like any Gaussian Blur "Blurriness" keyframes to be copied over to the Camera Lens Blur "Blur Radius", but reduced in value by 50% (or if there's no keyframes, then just copy the value over).  I know that the look of each blur is different but I've found that this value gives the Camera Lens Blur a similar blur level. I know I could like up the two blurs with expressions and *0.5 but I'd rather have the keyframes baked in so I can choose to delete the Gaussian Blur later if needed.

This topic has been closed for replies.
Correct answer Dan Ebberts

I think you could get that error if the script encounters a light or a camera. You could try adding this before line 5:

		if (! layer.hasVideo) continue;

1 reply

Dan Ebberts
Community Expert
Community Expert
May 6, 2023

This should get you pretty close. It doesn't copy any keyframe easing, just the values:

var comp = app.project.activeItem;
if (comp != null && comp instanceof CompItem) {
	for (var i = 1; i <= comp.numLayers; i++) {
		var layer = comp.layer(i);
		for (var j = 1; j <= layer.effect.numProperties; j++) {
			var effect = layer.property("Effects").property(j);
			if (effect.matchName == "ADBE Gaussian Blur 2") {
				effect.enabled = false;
				var newEffect = layer.property("Effects").addProperty("ADBE Camera Lens Blur");
				effect = layer.property("Effects").property("ADBE Gaussian Blur 2");
				var bluriness = effect.property("ADBE Gaussian Blur 2-0001");
				var blurRadius = newEffect.property("ADBE Camera Lens Blur-0001");
				if (bluriness.numKeys > 0){
					for (var k = 1; k <= bluriness.numKeys; k++){
						blurRadius.setValueAtTime(bluriness.keyTime(k),bluriness.keyValue(k)/2);
					}
				}else{
					blurRadius.setValue(bluriness.value/2);
				}
				break;
			}
		}
	}
} else {
	alert("No composition is active.");
}
Inspiring
May 7, 2023

That works great Dan. Thank you!