ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Extendscript: Changing Effect Values via Expressions

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Michael Müller
Extendscript: Changing Effect Values via Expressions
on Oct 11, 2018 at 12:02:38 pm
Last Edited By Michael Müller on Oct 11, 2018 at 12:06:46 pm

Hey there,

basically I want to know if that is possible or not.
For instance: I want to be able to link the array information of an Effect Point Control (for example the Offset Value in the turbulent displace effect) to two separate expression sliders. I wrote a perfectly working script that lets you do that with any 2D or 3D Values from anywhere else. Except with Point Controls of effects. I searched along the topic and found that "spatial values", as they are called, might not be able to receive expressions by Script. I wonder why it would be that way because regular expressions can be applied to it.
Just wanted to check if that is true and if even Dan Ebberts doesn't know a workaround.

Thanks ahead!

Michael


Return to posts index

Dan Ebberts
Re: Extendscript: Changing Effect Values via Expressions
on Oct 11, 2018 at 1:16:00 pm

Are you talking about something like this:

x = effect("Slider Control")("Slider");
y = effect("Slider Control 2")("Slider");
[x,y]


Dan



Return to posts index

Michael Müller
Re: Extendscript: Changing Effect Values via Expressions
on Oct 11, 2018 at 1:30:32 pm

This is what I want the outcome to be like, yes. Only that this is triggered by a js file. I added the code I was able to get so far to make it more clear.
The code generates as many sliders as the array length says, and adds information to what it's linked to in the slider name.
This works for normal properties like Position and Anchor Point, and also for scale (which was the main reason I started the entire thing)
However, it would be nice to be able to separate the dimensions of Effect Point Controls too. This doesn't seem to work for some reason. Only one slider is generated and no expression is applied to the property! Which made me think that these kind of properties might fall under the !.canSetExpression exception.

I hope this makes the issue more clear.

Michael

function completeSeparation() {
var differentSlidersForProperties = true;
var composition = app.project.activeItem;
if (!composition || !(composition instanceof CompItem))
return alert("Please select composition first");

app.beginUndoGroup("Add Controllers");
forSelectedProperties(separateDimentions);
app.endUndoGroup();

function forSelectedProperties(callback) {
var selectedProperties = composition.selectedProperties;
for (var i = 0, il = selectedProperties.length; i &lt; il; i++) {
callback(selectedProperties[i]);
}
}

function separateDimentions(property) {

// Skip if property cannot have expressions
if (!property.canSetExpression) return;

function getPropChain(prop) {
var ret = [];
if (prop.parentProperty !== null) { //when this prop's parent is Layer, parentProperty is null
ret = getPropChain(prop.parentProperty)
}
ret.push(prop.name); //prop.name add last
return ret;
}

var curComp = app.project.activeItem;
var curLayer = curComp.selectedLayers[0];
var curPropChain = getPropChain(curLayer.selectedProperties[0]);

var JOINT = " > ";
var mastersuffix = curPropChain.slice(1, -1).join(JOINT).toString().replace(new RegExp("Contents >","g"), "");




// Start constructing effect name suffix
var effectNameSuffix = '';
if (differentSlidersForProperties) {
effectNameSuffix = ' (' + property.name + '), [' + mastersuffix + '] ';
}

// Get a layer from current property
var layer = getLayerFromProperty(property);

// Get layers Effect property
var effectsProperty = layer.property("ADBE Effect Parade");

// Get properties value as an array
var value = getPropertyValue(property);

var prefixes = ['X', 'Y', 'Z'];

var controller;
var controllers = [];
var exprArray = [];


for (var i = 0, il = value.length; i &lt; il; i++) {
var sliderName = prefixes[i] + effectNameSuffix;


if (!hasSlider(sliderName)) {

// Add a slider with properties value
addSlider(sliderName, value[i]);

// Remember controllers name
controller = 'ctrl' + (i + 1);
controllers.push(controller);

// Construct expression and push it into array
exprArray.push(controller + ' = effect("' + sliderName + '")("Slider");');
}

addExpression(property);
}

function addExpression(property) {
// Make a String out of array
var expression = exprArray.join('\r');

// Create executable line, as in [ctrl1, ctrl2] etc
expression = expression + '\r' + '[' + controllers.join(', ') + '];';

// Apply expression
property.expression = expression;
}

function hasSlider(sliderName) {
for (var i = 1, il = effectsProperty.numProperties; i &lt;= il; i++) {
if (effectsProperty.property(i).name === sliderName) {
return true;
}
}
return false;
}

function addSlider(sliderName, value) {
var slider = effectsProperty.addProperty('ADBE Slider Control');
slider.name = sliderName;
slider.property(1).setValue(value);
}

function getLayerFromProperty(property) {
while (property.parentProperty) {
property = property.parentProperty;
}
return property;
}

function getPropertyValue(property) {
// Get properties value at timr 0
var value = property.valueAtTime(0, false);

// Make sure it's an array
if (!value.length) {
value = [value];
}

// If it's a 2D layer, trim the 3rd value
if (!layer.threeDLayer && value.length === 3) {
value.length = 2;
}

return value;
}
}
};


Return to posts index


Dan Ebberts
Re: Extendscript: Changing Effect Values via Expressions
on Oct 11, 2018 at 4:03:58 pm

I don't think there's an issue setting an expression for a property like that. This works:

var myProp = app.project.activeItem.layer("Layer 1").property("Effects").property("Turbulent Displace").property("Offset (Turbulence)");
myProp.expression = "value";


Dan



Return to posts index

Michael Müller
Re: Extendscript: Changing Effect Values via Expressions
on Oct 11, 2018 at 5:45:52 pm

Alright, that is already great news.
I still don't understand why my script won't apply the expression to the Point Controls.
I am too much of a beginner to understand everything that is in there. I basically patched it together from different pieces I found, asked for or did a little research to complete it.

Now what is interesting is that the function won't react in any way to Point Controls at all if I delete Line 71, which says
"if(!property.canSetExpression) return;"
If I keep the line in, the slider for "X" appears and has the [0] value. However, no expression is applied and the slider doesn't do anything, as opposed to using the script on a "regular" property, where all necessary sliders appear and work the way they should.

I'd kiss your feet Dan if you could tell me what is wrong in my code here and how I can fix it. I'd just love to have this damn script finished. It already is quite useful as it is but it would be the bomb if there wasn't this bug with Point Controls. I know I should have started with the basics of scripting instead of begging for help with stuff I have no clue of, but now I got already this far and the goal is near.

Thanks a lot

Michael


Return to posts index

Dan Ebberts
Re: Extendscript: Changing Effect Values via Expressions
on Oct 11, 2018 at 6:15:36 pm

One thing you need to take into account is that if you select the Offset (Turbulence) property, the Turbulent Displace effect itself also becomes a selected property and it will be selectedProperties[0], so it looks like your code may be trying to process that instead of the Offset.

When you're debugging script code, $.writeln() is your friend. I sprinkle those throughout my code to print out intermediate results on the JavaScript Console to quickly spot where things are getting off track.


Dan



Return to posts index


Michael Müller
Re: Extendscript: Changing Effect Values via Expressions
on Oct 11, 2018 at 6:49:53 pm

It seems like it is grabbing the value from the offset property, not the turbulent displace effect (correct me if I'm wrong!), so that is probably not the issue here.
The name is also shown correctly.

I tried to use $.writeIn(value) in both cases:

first time I applied the script to a normal Transform Position Property -> Spit out the values in the consoles
second time I applied the script to the Point control -> Slider appears as in image below - Console doesn't spit out anything!

Now I know that this somehow has got to do something with the error, but I don't know what it is or how to approach the problem from now.
Is there anything you can think of using this information?

Thanks a lot!

Michael



Return to posts index

Dan Ebberts
Re: Extendscript: Changing Effect Values via Expressions
on Oct 11, 2018 at 7:24:42 pm

I think the problem may be that adding an effect (the first slider) invalidates any previous references to effects. Are you running the script from the ESTK? If so you should see the "Object is invalid" error.


Dan



Return to posts index

Michael Müller
Re: Extendscript: Changing Effect Values via Expressions
on Oct 12, 2018 at 7:46:44 am

Yes, I am running it from ESTK. I don't get an error with the first Slider. When I run the script again, actually a second slider with the value of Y appears, not working either and coming with an expression error:
"Undefined value used in expression (could be an our of range array subscript?)

Now the effect does have the following, disabled expression in it: [];

I guess this equals "exprArray" as this is a variable with exactly that in it. "Controllers" is that too.

var controllers = [];
var exprArray = [];

I checked with $.writeIn(value.length) if the properties of the turbulent displace effect show the correct array length and they dont!. In fact, they don't seem to have any length at all. Neither do they seem to have any value. As I mentioned, if I check for that, the console doesn't show anything either. Not even an error.

I created a testscript to see if or how I can read the values of the offset property:

alert (app.project.activeItem.selectedLayers[0].selectedProperties[0,1].value);
This gave me the value array of the effect values but refused to give me values of normal transform properties


alert (app.project.activeItem.selectedLayers[0].selectedProperties[0].value);
With this line it's the exact opposite. Can show transform values. Can't show effect values.

I thought maybe with something like this except the alert the problem could be fixed. I don't know how I can change .selectedProperties[0] to .selectedProperties[0,1] though, nor can I read from the code where this Information is coming from. Can you help me out with this one?

if (curLayer.selectedProperties[0,1].isSpatial == true) {
alert("Case of Point Control");
}


"Object is invalid" doesn't appear in any of the bugged actions.


Michael


Return to posts index


Dan Ebberts
Re: Extendscript: Changing Effect Values via Expressions
on Oct 12, 2018 at 8:52:50 pm

I'm not sure what selectedProperties[0,1] is supposed to do.

Dan



Return to posts index

Michael Müller
Re: Extendscript: Changing Effect Values via Expressions
on Oct 13, 2018 at 2:39:55 pm

I don't know, I just thought that might help but apparently it does not.

Something else I found out is that:
app.project.activeItem.selectedLayers[0].selectedProperties.length
equals 2 when you run it on the offset property or any effect property,
and equals 1 when you run it on a transform property.

Does that help in any way?

Michael


Return to posts index

Dan Ebberts
Re: Extendscript: Changing Effect Values via Expressions
on Oct 13, 2018 at 4:19:44 pm

That's what I was saying before, the effect itself is also selected, so you need to skip those as you loop through the selected properties.

Dan



Return to posts index


Michael Müller
Re: Extendscript: Changing Effect Values via Expressions
on Oct 16, 2018 at 9:51:14 am

I just needed a while to wrap my head around it. You helped me a lot with that answer! The problem is now solved. Thank you very much, Dan.

Michael


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]