FORUMS: list search recent posts

Are Scripts out there to write the calculated value of an expression to that property?

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
leif falk
Are Scripts out there to write the calculated value of an expression to that property?
on Jan 26, 2013 at 3:34:16 pm

Hi there
I have a ton of layers like 500 and they all use a sampleImage and a random expressions to set their position.
It works but it is slow. Is their a script that sets all the position values to that once calculated expression value and deletes the expression? I don`t want keyframes, just one value for each layer.

And what about other properties like scale, rotation or values in effects and shape layers?
I think the best solution would be a script that applies all expressions in all selected layers to non keyframed values.
(Afterwards you could copy and paste the expressions back again which you didn´t want to remove)

Any ideas?
Thank you a lot

leif


Return to posts index

Xavier Gomez
Re: Are Scripts out there to write the calculated value of an expression to that property?
on Jan 26, 2013 at 7:51:10 pm

What you are asking for is very specific and i don't think there is a script that does it. The main problem is to decide what properties to scout in the composition (only positions? only transform properties ? all including effects, masks, layerStyles, etc... ?) Scouting absolutely everything over 500 layers can be terribly long, and also useless, if in the end you only want 2-3 specific properties.
If you know precisely what properties should be scouted the script itself isnt too difficult to write. Below is an example without any UI nor anything, for just position. If you want the script to treat other properties you'll have to create a "subloop" over the desired properties.
Re-enabling the expressions can be done similarly, however it is more tricky than it may seem and in practice it is not possible, unless you know what were the original pre-expression values of the properties.

Xavier.

function myScript()
{
if (!Object.isValid(app.project) || typeof app.project.activeItem === 'undefined' || !(app.project.activeItem instanceof CompItem)) return;

var comp = app.project.activeItem;
var selectedLayers = comp.selectedLayers;
if (!selectedLayers.length) return;

var numSelectedLayers = selectedLayers.length;
var layer, inPoint, prop, x;
for (var n = 0; n<numSelectedLayers; n++)
{
layer = selectedLayers[n];
inPoint = layer.inPoint;
prop = layer.transform.position;
if (!prop.numKeys && prop.expressionEnabled)
{
x = prop.valueAtTime(inPoint, false);
prop.setValue(x);
prop.expressionEnabled = false;
}
}
return;
}

myScript();


Return to posts index

leif falk
Re: Are Scripts out there to write the calculated value of an expression to that property?
on Jan 26, 2013 at 9:17:24 pm

Thanks a lot for your afford, that is very good and helpful.

I am new to scripts and I tried modifying your line
prop = layer.transform.position;
To
prop = layer.transform.scale;
or even
prop = layer("Effects")("Fast Blur")("Blurriness");
works nice ;)

One additional question:
How do I have to change the line to access for example the following properties of a shape layer:
Contents/Group 1/Ellipse 1/Transform:Ellipse 1/Position
And this in a for-loop for all Ellipses in this Group(Ellipse 1,Ellipse 2,Ellipse 3,etc.)?

Then I think I could find out how to access every property I want.
thanks again.

leif


Return to posts index


Xavier Gomez
Re: Are Scripts out there to write the calculated value of an expression to that property?
on Jan 27, 2013 at 2:45:30 pm

Hello,
i forgot something essential in the code above: before the for() loop you can add this line:
app.beginUndoGroup("expr. to value")
and after the loop (so, just before the enter; instruction) add this other line:
app.endUndoGroup()

In case you feel sorry about applying this script (better think twice since there is no way back, so it's not a very safe script) the script action appears in the history under only one label and you can undo the whole lot in just one undo.

For the shape layers it is quite clumsy since a shape layer property tree can be arbitrarily long (just add a tower of subgroups).
And writing a script that always treats Ellipses in Group1 is not too useful.
To know how to refer properties, download the script rd_GimmePropPath

For instance, for layer = app.project.activeItem.selectedLayers[n], and properties refered by name:

Content : layer.property("Contents")
Group 1 : layer.property("Contents").property("Group 1")
Group 1/Ellipse 1 : layer.property("Contents").property("Group 1").property("Contents").property("Ellipse Path 1")

For loops it is better to refer properties by index though.

Below is a script that scouts all selected layers and, when the layer is a shape layer, scouts all its properties and when the property is the position property of a Shape (rectangle, Ellipse etc) disables the expression and sets the value.
You can change var propToReplace = "Position"; to the property you want to replace, for instance var propToReplace = "Scale".

Xavier

function myOtherScript()
{
function scout_and_replace(propertyBase, stringKey)
{
if(propertyBase.propertyType === PropertyType.PROPERTY)
{
var prop = propertyBase;
if (prop.name.indexOf(stringKey) != -1 && prop.parentProperty.matchName.indexOf("ADBE Vector Shape") != -1)
{
if (!prop.numKeys && prop.expressionEnabled)
{
x = prop.valueAtTime(inPoint, false);
prop.setValue(x);
prop.expressionEnabled = false;
}
}
return;
}
else
{
var propGroup = propertyBase;
var numProps = propGroup.numProperties;
for (var j=1; j<numProps+1; j++)
scout_and_replace(propGroup.property(j), stringKey);
return;
}
}
/* */
if (!Object.isValid(app.project) || typeof app.project.activeItem === 'undefined' || !(app.project.activeItem instanceof CompItem)) return;
var comp = app.project.activeItem; var selectedLayers = comp.selectedLayers;
if (!selectedLayers.length) return;

var numSelectedLayers = selectedLayers.length;
var layer, inPoint, prop, x;
var propToReplace = "Position";

app.beginUndoGroup("script")
for (var n = 0; n<numSelectedLayers; n++)
{
layer = selectedLayers[n];
inPoint = layer.inPoint;
if (Object.isValid(layer.property("Contents"))) scout_and_replace( layer.property("Contents"), propToReplace );
}
app.endUndoGroup()
return;
}

myOtherScript();


Return to posts index

Bryan Woods
Re: Are Scripts out there to write the calculated value of an expression to that property?
on Jan 28, 2013 at 10:52:07 pm

Dan showed me that the easiest way to set an expression, get its result, then remove the expression is very simply: Set expression, put result into a new variable, then set expression to blank (easiest way to remove the expression is to just over write it with nothing!). I've used this for a script with sampleImage where I needed to offset the anchor point based on the alpha of the image. It moves the anchor point, then removes the expression so you're not constantly recalculating.

See this thread:
http://forums.creativecow.net/thread/227/22776


Return to posts index

leif falk
Re: Are Scripts out there to write the calculated value of an expression to that property?
on Jan 30, 2013 at 4:44:19 pm

Thank you both. The script from Xavier worked perfect for my purpose. Seems to be a bit complicated with the shape layers, though. Thanks a lot for figuring it out..

I was just wondering because the script affected the Position value of the shape, in the category where there is only Position and Size, not the "Transform:Ellipse 1 properies" where you can find Scale and all the others. So change the line to "var propToReplace = "Scale"" won´t work I guess.
For me it worked but maybe its for general interest how to refer to these other values..


Return to posts index


leif falk
Re: Are Scripts out there to write the calculated value of an expression to that property?
on Jun 5, 2013 at 6:55:50 pm

@Xavier
Your Script is very good and I guess it was quite a lot of work.
I tried to check out how to modify the script and affect the other values like for example:
contents/Shape 1/Transform: Shape 1/Rotation
Your Script does affect:
contents/Shape 1/Path 1/Position

I think to modify it to access "Anchor Point","Position","Scale", etc. under Transform should be changing only one single line in your script, but I don´t find out where.

Maybe you could help out here once again. It can be very useful, because if you have hundreds of shapes calculated by expressions it slows down your computer a lot.
thanks


Return to posts index

Xavier Gomez
Re: Are Scripts out there to write the calculated value of an expression to that property?
on Jun 5, 2013 at 7:35:44 pm

Hello, i haven' looked into much details, but i think what prevents to modify Transform Group is the line:
if (prop.name.indexOf(stringKey) != -1 && prop.parentProperty.matchName.indexOf("ADBE Vector Shape") != -1) I thought it was what you wanted at that time. To include transform group, replace this line by
if (prop.name.indexOf(stringKey) != -1) this amount to removeing the condition that excludes transform groups.

If you use this thing a lot it might be worth learning a bit of scripting because it can be a lot improved with a UI, for instance make it able do everything a a time instead of editing the script, changing Position to Anchor Point, relaunching etc... (!)

Xavier


Return to posts index

leif falk
Re: Are Scripts out there to write the calculated value of an expression to that property?
on Jun 6, 2013 at 7:50:06 pm

Thanks a lot, Xavier
It works perfect. I hope it will be useful for other people aswell.
Maybe you are right about learning script writing. I hope I will find the time. At the moment I am focusing on learning expressions and there still is a lot to discover.


Return to posts index

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