Help adding expression and slider to all selected properties

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
thegreenfury
Posts: 3
Joined: April 12th, 2013, 1:18 pm

Howdy,

Long time After Effects user, proficient in expressions and this is my first foray into scripting.

My goal is to create a script that adds an expression linked to slider controls to all selected layer parameters. If a selected parameter doesn't have any keyframes to hold the expression, it should create a key at the current time as well and add the expression to that.

What I have now almost works. It will alert you if you don't have a layer or a layer property selected, it will add keys to properties without any, it will add the sliders and the expression, however inconsistently. The issue seems to be adding it to multiple properties at once. For example, if I select rotation, opacity, and position it will add sliders to all of them but only one of the them gets the expression. And if I select the position of one layer and the opacity of another, it will only run on the first layer and ignore the second one. Ideally I would be able to lasso a bunch of keys and/or properties over multiple layers (contiguous or not) and the script will apply the expression to all properties and sliders for each instance of the expression. Maybe I need a loop construct to keep running the script on all the selected properties? If so, I'm not sure how to approach that.

Here is what I have so far; please forgive any snafus and don't be afraid to talk to me like i'm a child, heh. In this example I'm just using a simple wiggle expression for testing purposes.

var proj = app.project; // The Application Object is at the top level followed by the Project Object.
var comp = proj.activeItem; // Set the comp variable to the active composition.
var layer = comp.selectedLayers[0]; // Set the layer variable to the selected layer of the composition object.

//Determine if a layer is selected.
if (layer == null){
alert ("Select a layer property");
}

var parameter = layer.selectedProperties[0]; //Set the parameter variable to the selected layer property.


//Determine if a property is selected.
if (parameter == null){
alert ("Select a layer property");
}

var numKeys = parameter.numKeys; // Get the number of keyframes on the selected property through the numKeys attribute.

//function to add a slider control and expression to the selected property.
function expressionCreator(){
var myExpr = "wiggle(effect(\"frequency\")(\"Slider\"),effect(\"amplitude\")(\"Slider\"));";
var addSlider = layer.Effects.addProperty("Slider Control");
addSlider.name = 'amplitude';
var addSlider = layer.Effects.addProperty("Slider Control");
addSlider.name = 'frequency';
parameter.expression = myExpr;
}

app.beginUndoGroup("Expression Creator");

//Determine if there are keyframes on the selected property and add one at the current time if there are none present then run the expressionCreator function.
if (numKeys > 0) {
expressionCreator ();
}
else {
var curTime = app.project.activeItem.time; //get the current time.
parameter.addKey(curTime);// add a key to the current time.
expressionCreator ();
}
app.endUndoGroup();


Thanks in advance!

Cory
Dan Ebberts
Posts: 320
Joined: June 26th, 2004, 10:01 am
Location: Folsom, CA
Contact:

My initial advice would be to save the selected layers into a variable and the selected properties for each layer into an array, because creating anything can change the active selections.

Dan
thegreenfury
Posts: 3
Joined: April 12th, 2013, 1:18 pm

Dan,

I'll have to do some more studying to take your suggestion properly, but I will keep that advice in mind as I google furiously. If you have any other ideas don't hesitate to send them my way.

Thanks!

Cory
Post Reply