FORUMS: list search recent posts

ExtendScript - Add expression to opacity property

COW Forums : Adobe After Effects

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Paul Connors
ExtendScript - Add expression to opacity property
on Nov 1, 2016 at 3:55:04 pm

Hello,

I'm currently writing a script and am stuck on trying to automatically add an expression to a specific composition. Currently my script creates a new comp named: "Precomp1". It also creates another composition within "Precomp1" named: "Item 1". I'm trying to then automatically add an expression to the opacity property for "Item 1." Here is my current code:



var MasterComp = app.project.activeItem;
var Precomp1 = app.project.items.addComp("Precomp1", 1920, 1080, 1, 60, 29.97);
MasterComp.layers.add(Precomp1);

var Item001Comp = app.project.items.addComp("Item 1", 1920, 1080, 1, 60, 29.97);

Precomp1.layers.add(Item001Comp);



Any ideas?

Thanks!


Return to posts index

Walter Soyka
Re: ExtendScript - Add expression to opacity property
on Nov 1, 2016 at 4:56:19 pm

var myOpacityExpression = "value;";

var MasterComp = app.project.activeItem;
var Precomp1 = app.project.items.addComp("Precomp1", 1920, 1080, 1, 60, 29.97);
MasterComp.layers.add(Precomp1);

var Item001Comp = app.project.items.addComp("Item 1", 1920, 1080, 1, 60, 29.97);

Item001Layer = Precomp1.layers.add(Item001Comp);
Item001Layer.property("Transform").property("Opacity").expression = myOpacityExpression;



We're creating an object (Item001Layer) to represent the "Item 1" layer inside the precomp, then adding an expression. to its opacity property. The value we'll use for the expression is stored in the variable "myOpacityExpression" on the first line.

Any questions?

Walter Soyka
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive [twitter]   |   RenderBreak [blog]   |   Profile [LinkedIn]


Return to posts index

Paul Connors
Re: ExtendScript - Add expression to opacity property
on Nov 1, 2016 at 6:16:05 pm

That's fantastic. Thank you very much. I'm able to get it to work with a simple expression like "wiggle(2,50)" but I'm struggling to add a more complex expression to that opacity property. I have an expression from another project I'm trying to incorporate here. That working expression looks like this:



C = comp("Master");
ctrl = C.layer("Controls").effect("Slider Control")("Slider");
L = C.layer(thisComp.name);
val = Math.round(ctrl.valueAtTime(time+L.startTime));
if (val == index) 100 else 0



Do you know how would go about incorporating that to opacity expression help you offered above?

Thanks!


Return to posts index


Walter Soyka
Re: ExtendScript - Add expression to opacity property
on Nov 2, 2016 at 2:08:40 pm

[Paul Connors] "I'm able to get it to work with a simple expression like "wiggle(2,50)" but I'm struggling to add a more complex expression to that opacity property."

You have to escape your single quotes by typing backslash-doublequote instead of just doublequote. You also have to address your line endings. Although there are multiple ways to do this, I think the simplest is to add backslashes to the ends of the lines:

var myOpacityExpression = "C = comp(\"Master\"); \
ctrl = C.layer(\"Controls\").effect(\"Slider Control\")(\"Slider\"); \
L = C.layer(thisComp.name); \
val = Math.round(ctrl.valueAtTime(time+L.startTime)); \
if (val == index) 100 else 0 \
";

var MasterComp = app.project.activeItem;
var Precomp1 = app.project.items.addComp("Precomp1", 1920, 1080, 1, 60, 29.97);
MasterComp.layers.add(Precomp1);

var Item001Comp = app.project.items.addComp("Item 1", 1920, 1080, 1, 60, 29.97);

Item001Layer = Precomp1.layers.add(Item001Comp);
Item001Layer.property("Transform").property("Opacity").expression = myOpacityExpression;


Walter Soyka
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive [twitter]   |   RenderBreak [blog]   |   Profile [LinkedIn]


Return to posts index

Paul Connors
Re: ExtendScript - Add expression to opacity property
on Nov 2, 2016 at 3:52:11 pm

You sir are a wizard! That worked like a charm. Thank you so much. If you're able to help with one more little nuance that would be amazing. I tried to extrapolate the knowledge you just gave me but am missing somewhere. I'm trying to add an expression to a slider control now. I thought I wrote it correctly (it doesn't return any errors when I execute it in After Effects) but the expression doesn't show up on the slider control. I'm sure it's something silly I'm overlooking. Here is the code:



// This creates a new text layer
myComp = app.project.activeItem;
myText = myComp.layers.addText("Slide-Number-Selector");
myText.startTime = 0

// This creates a new null layer with a slider control named "Slider Control"
var comp = app.project.activeItem;
var myNull_1 = comp.layers.addNull();
myNull_1.name = "Controls";
var slider1 = myNull_1.Effects.addProperty("Slider Control");
slider1.name = "Slider Control";

// Here I'm defining a expression for the newly created "Slider Control"
var mySliderExpression = "C = comp(\"Master\"); \
txt = C.layer(\"Slide-Number-Selector\").text.sourceText; \
if (! isNaN(parseFloat(txt))) \
parseFloat(txt) \
else \
value \
";

// Here I'm attempting to apply the newly created expression to the "Slider Control"
myNull_1.effect("Slider Control").expression = mySliderExpression;



I'm not sure where I'm going wrong.

Any help would be greatly appreciated. Thanks!


Return to posts index

Walter Soyka
Re: ExtendScript - Add expression to opacity property
on Nov 3, 2016 at 11:08:28 pm

One little tweak on that last line:

myNull_1.effect("Slider Control")("Slider").expression = mySliderExpression;

You cannot put the expression on the Slider Control, which is an effect. You have to put it on the Slider Control's Slider, which is a property.

Walter Soyka
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive [twitter]   |   RenderBreak [blog]   |   Profile [LinkedIn]


Return to posts index


Paul Connors
Re: ExtendScript - Add expression to opacity property
on Nov 4, 2016 at 12:57:07 pm

A wizard I say! Thanks very much Walter. And thank you for your explanations to these solutions as well. It all makes more sense when I'm actually able to read what the problem was exactly and then why the solution works.


Return to posts index

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