Highlighted

apply script to more than 1 selected layers issue

Community Beginner ,
Oct 07, 2020

Copy link to clipboard

Copied

so i have script that applies slider and effect preset file to selected layer also I added some expression inside effect property as below :

 

app.beginUndoGroup("HL Text");
if(MyComp && MyComp.selectedLayers.length > 0){
    for(var i = 0 ; i < MyComp.selectedLayers.length; i++){
        var MyComp = app.project.activeItem;
        var pfile = Folder(Folder.appPackage.parent.absoluteURI + "/Support Files/Presets/Transitions - Wipes/Linear Wipe.ffx");
        if (pfile.exists != true) {
            alert('Preset file does not exist');
            //ereturn false;
        };
        var currentLayer = MyComp.selectedLayers[i];
        var slider = currentLayer.Effects.addProperty("Slider Control");
        slider.name = "brp frame?";
        currentLayer.effect("brp frame?")("Slider").setValue(20);
        
        currentLayer.applyPreset(pfile);
        var transComp = currentLayer.effect("Linear Wipe").property("Transition Completion");
        transComp.expression = "0";
    };  
app.endUndoGroup();
};
else{
alert("please select the layer first");
}; 

 

the problem is when I selected 2 layers on AE and run the script, the output applied transComp.expression = "0" to 1 layer only like this :

 

Capture.PNG

also the applied linear wipe effect and slider have different order.

any idea how to solve this? any help would be appreciated 🙂

So yes there are a couple of oddities here. Learning to deal with AE's scripting oddities is all part of the fun!

 

Firstly, "layer.applyPreset()" appears to apply to all selected layers. It probably shouldn't, it's probably a result of it matching the manual behaviour, who knows? You do often need to be careful when using a selection as some actions can change it, so it's often a good idea to record all the selected layer indexes at the start, then use that rather than relying on selectedLayers throughout. So the reason for the different order seems to be that it was applying the preset (Linear Wipe) to both layers on the first loop iteration, hence it being the first effect on the second layer which was getting the Slider added on the second loop. 

 

And then the expression not applying....I couldn't figure out why that's happening either, but it does work if you apply all the expressions right at the end.

 

So here's a version that takes advantage of the oddity of applyPresets behaviour and relies on selectedLayers throughout:

var MyComp = app.project.activeItem;
if (MyComp != null && MyComp instanceof CompItem) {
	
	var pfile = Folder(Folder.appPackage.parent.absoluteURI + "/Support Files/Presets/Transitions - Wipes/Linear Wipe.ffx");
	if (pfile.exists != true) {
		alert('Preset file does not exist');
	} else {
		app.beginUndoGroup("HL Text");
		var currentLayer, slider, transComp;
		for(var i = 0 ; i < MyComp.selectedLayers.length; i++){	// apply sliders
			currentLayer = MyComp.selectedLayers[i];
			slider = currentLayer.Effects.addProperty("Slider Control");
			slider.name = "brp frame?";
		}

		currentLayer.applyPreset(pfile);	// apply preset to all selected layers (????)

		for(var i = 0 ; i < MyComp.selectedLayers.length; i++){ // apply expressions
			currentLayer = MyComp.selectedLayers[i];
			transComp = currentLayer.effect(2).property("Transition Completion");
			transComp.expression = "0";
		};

		app.endUndoGroup();
	}
} else {
	alert("please select the layer first");
}; 

 

Then here's a version that does things in perhaps a more robust way, recording the selection, selecting each layer in turn before applying the presets etc (I still had to apply the expressions in a separate loop though), then restoring the layer selection at the end:

var MyComp = app.project.activeItem;
if (MyComp != null && MyComp instanceof CompItem) {
	
	var pfile = Folder(Folder.appPackage.parent.absoluteURI + "/Support Files/Presets/Transitions - Wipes/Linear Wipe.ffx");
	if (pfile.exists != true) {
		alert('Preset file does not exist');
	} else {
		app.beginUndoGroup("HL Text");
		var selectedArray = new Array();
		var currentLayer, slider, transComp;
		for(var i = 0 ; i < MyComp.selectedLayers.length; i++){	// record indexes of all selected layers
			selectedArray.push(MyComp.selectedLayers[i].index);
		}

		for(i = 0 ; i < selectedArray.length; i++){	// deselect all layers
			MyComp.layer(selectedArray[i]).selected = false;
		}

		for(i = 0 ; i < selectedArray.length; i++){	// apply slider and presets
			currentLayer = MyComp.layer(selectedArray[i]);
			currentLayer.selected = true;	// make the current layer selected
			slider = currentLayer.Effects.addProperty("Slider Control");
			slider.name = "brp frame?";
			currentLayer.applyPreset(pfile);

		};
	
		for(i = 0 ; i < selectedArray.length; i++){ // apply expressions
			currentLayer = MyComp.layer(selectedArray[i]);
			transComp = currentLayer.effect(2).property("Transition Completion");
			transComp.expression = "0";
		};
	
		for(i = 0 ; i < selectedArray.length; i++){	// reselect all layers
			MyComp.layer(selectedArray[i]).selected = true;
		}
		app.endUndoGroup();
	}
} else {
	alert("please select the layer first");
}; 

 

TOPICS
Expressions, Scripting

Views

68

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

apply script to more than 1 selected layers issue

Community Beginner ,
Oct 07, 2020

Copy link to clipboard

Copied

so i have script that applies slider and effect preset file to selected layer also I added some expression inside effect property as below :

 

app.beginUndoGroup("HL Text");
if(MyComp && MyComp.selectedLayers.length > 0){
    for(var i = 0 ; i < MyComp.selectedLayers.length; i++){
        var MyComp = app.project.activeItem;
        var pfile = Folder(Folder.appPackage.parent.absoluteURI + "/Support Files/Presets/Transitions - Wipes/Linear Wipe.ffx");
        if (pfile.exists != true) {
            alert('Preset file does not exist');
            //ereturn false;
        };
        var currentLayer = MyComp.selectedLayers[i];
        var slider = currentLayer.Effects.addProperty("Slider Control");
        slider.name = "brp frame?";
        currentLayer.effect("brp frame?")("Slider").setValue(20);
        
        currentLayer.applyPreset(pfile);
        var transComp = currentLayer.effect("Linear Wipe").property("Transition Completion");
        transComp.expression = "0";
    };  
app.endUndoGroup();
};
else{
alert("please select the layer first");
}; 

 

the problem is when I selected 2 layers on AE and run the script, the output applied transComp.expression = "0" to 1 layer only like this :

 

Capture.PNG

also the applied linear wipe effect and slider have different order.

any idea how to solve this? any help would be appreciated 🙂

So yes there are a couple of oddities here. Learning to deal with AE's scripting oddities is all part of the fun!

 

Firstly, "layer.applyPreset()" appears to apply to all selected layers. It probably shouldn't, it's probably a result of it matching the manual behaviour, who knows? You do often need to be careful when using a selection as some actions can change it, so it's often a good idea to record all the selected layer indexes at the start, then use that rather than relying on selectedLayers throughout. So the reason for the different order seems to be that it was applying the preset (Linear Wipe) to both layers on the first loop iteration, hence it being the first effect on the second layer which was getting the Slider added on the second loop. 

 

And then the expression not applying....I couldn't figure out why that's happening either, but it does work if you apply all the expressions right at the end.

 

So here's a version that takes advantage of the oddity of applyPresets behaviour and relies on selectedLayers throughout:

var MyComp = app.project.activeItem;
if (MyComp != null && MyComp instanceof CompItem) {
	
	var pfile = Folder(Folder.appPackage.parent.absoluteURI + "/Support Files/Presets/Transitions - Wipes/Linear Wipe.ffx");
	if (pfile.exists != true) {
		alert('Preset file does not exist');
	} else {
		app.beginUndoGroup("HL Text");
		var currentLayer, slider, transComp;
		for(var i = 0 ; i < MyComp.selectedLayers.length; i++){	// apply sliders
			currentLayer = MyComp.selectedLayers[i];
			slider = currentLayer.Effects.addProperty("Slider Control");
			slider.name = "brp frame?";
		}

		currentLayer.applyPreset(pfile);	// apply preset to all selected layers (????)

		for(var i = 0 ; i < MyComp.selectedLayers.length; i++){ // apply expressions
			currentLayer = MyComp.selectedLayers[i];
			transComp = currentLayer.effect(2).property("Transition Completion");
			transComp.expression = "0";
		};

		app.endUndoGroup();
	}
} else {
	alert("please select the layer first");
}; 

 

Then here's a version that does things in perhaps a more robust way, recording the selection, selecting each layer in turn before applying the presets etc (I still had to apply the expressions in a separate loop though), then restoring the layer selection at the end:

var MyComp = app.project.activeItem;
if (MyComp != null && MyComp instanceof CompItem) {
	
	var pfile = Folder(Folder.appPackage.parent.absoluteURI + "/Support Files/Presets/Transitions - Wipes/Linear Wipe.ffx");
	if (pfile.exists != true) {
		alert('Preset file does not exist');
	} else {
		app.beginUndoGroup("HL Text");
		var selectedArray = new Array();
		var currentLayer, slider, transComp;
		for(var i = 0 ; i < MyComp.selectedLayers.length; i++){	// record indexes of all selected layers
			selectedArray.push(MyComp.selectedLayers[i].index);
		}

		for(i = 0 ; i < selectedArray.length; i++){	// deselect all layers
			MyComp.layer(selectedArray[i]).selected = false;
		}

		for(i = 0 ; i < selectedArray.length; i++){	// apply slider and presets
			currentLayer = MyComp.layer(selectedArray[i]);
			currentLayer.selected = true;	// make the current layer selected
			slider = currentLayer.Effects.addProperty("Slider Control");
			slider.name = "brp frame?";
			currentLayer.applyPreset(pfile);

		};
	
		for(i = 0 ; i < selectedArray.length; i++){ // apply expressions
			currentLayer = MyComp.layer(selectedArray[i]);
			transComp = currentLayer.effect(2).property("Transition Completion");
			transComp.expression = "0";
		};
	
		for(i = 0 ; i < selectedArray.length; i++){	// reselect all layers
			MyComp.layer(selectedArray[i]).selected = true;
		}
		app.endUndoGroup();
	}
} else {
	alert("please select the layer first");
}; 

 

TOPICS
Expressions, Scripting

Views

69

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 07, 2020 1
Most Valuable Participant ,
Oct 07, 2020

Copy link to clipboard

Copied

You probably si9mply need to structure your code better and create a sub-loop/ conditional for applying the values. Currently it's only a flat list of steps which doesn't enforce nor account for a specific oder of operations, especailly if there may be other properties/ effects already on the layer.

 

Mylenium

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 07, 2020 0
Engaged ,
Oct 07, 2020

Copy link to clipboard

Copied

So yes there are a couple of oddities here. Learning to deal with AE's scripting oddities is all part of the fun!

 

Firstly, "layer.applyPreset()" appears to apply to all selected layers. It probably shouldn't, it's probably a result of it matching the manual behaviour, who knows? You do often need to be careful when using a selection as some actions can change it, so it's often a good idea to record all the selected layer indexes at the start, then use that rather than relying on selectedLayers throughout. So the reason for the different order seems to be that it was applying the preset (Linear Wipe) to both layers on the first loop iteration, hence it being the first effect on the second layer which was getting the Slider added on the second loop. 

 

And then the expression not applying....I couldn't figure out why that's happening either, but it does work if you apply all the expressions right at the end.

 

So here's a version that takes advantage of the oddity of applyPresets behaviour and relies on selectedLayers throughout:

var MyComp = app.project.activeItem;
if (MyComp != null && MyComp instanceof CompItem) {
	
	var pfile = Folder(Folder.appPackage.parent.absoluteURI + "/Support Files/Presets/Transitions - Wipes/Linear Wipe.ffx");
	if (pfile.exists != true) {
		alert('Preset file does not exist');
	} else {
		app.beginUndoGroup("HL Text");
		var currentLayer, slider, transComp;
		for(var i = 0 ; i < MyComp.selectedLayers.length; i++){	// apply sliders
			currentLayer = MyComp.selectedLayers[i];
			slider = currentLayer.Effects.addProperty("Slider Control");
			slider.name = "brp frame?";
		}

		currentLayer.applyPreset(pfile);	// apply preset to all selected layers (????)

		for(var i = 0 ; i < MyComp.selectedLayers.length; i++){ // apply expressions
			currentLayer = MyComp.selectedLayers[i];
			transComp = currentLayer.effect(2).property("Transition Completion");
			transComp.expression = "0";
		};

		app.endUndoGroup();
	}
} else {
	alert("please select the layer first");
}; 

 

Then here's a version that does things in perhaps a more robust way, recording the selection, selecting each layer in turn before applying the presets etc (I still had to apply the expressions in a separate loop though), then restoring the layer selection at the end:

var MyComp = app.project.activeItem;
if (MyComp != null && MyComp instanceof CompItem) {
	
	var pfile = Folder(Folder.appPackage.parent.absoluteURI + "/Support Files/Presets/Transitions - Wipes/Linear Wipe.ffx");
	if (pfile.exists != true) {
		alert('Preset file does not exist');
	} else {
		app.beginUndoGroup("HL Text");
		var selectedArray = new Array();
		var currentLayer, slider, transComp;
		for(var i = 0 ; i < MyComp.selectedLayers.length; i++){	// record indexes of all selected layers
			selectedArray.push(MyComp.selectedLayers[i].index);
		}

		for(i = 0 ; i < selectedArray.length; i++){	// deselect all layers
			MyComp.layer(selectedArray[i]).selected = false;
		}

		for(i = 0 ; i < selectedArray.length; i++){	// apply slider and presets
			currentLayer = MyComp.layer(selectedArray[i]);
			currentLayer.selected = true;	// make the current layer selected
			slider = currentLayer.Effects.addProperty("Slider Control");
			slider.name = "brp frame?";
			currentLayer.applyPreset(pfile);

		};
	
		for(i = 0 ; i < selectedArray.length; i++){ // apply expressions
			currentLayer = MyComp.layer(selectedArray[i]);
			transComp = currentLayer.effect(2).property("Transition Completion");
			transComp.expression = "0";
		};
	
		for(i = 0 ; i < selectedArray.length; i++){	// reselect all layers
			MyComp.layer(selectedArray[i]).selected = true;
		}
		app.endUndoGroup();
	}
} else {
	alert("please select the layer first");
}; 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 07, 2020 1
Community Beginner ,
Oct 07, 2020

Copy link to clipboard

Copied

Thank you so much Paul. Your answer is very helpful! actually i started creating scripts yesterday and turns out that scripts have more access than expressions i usually use. I have much to learn 😂

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 07, 2020 0