Copy link to clipboard
Copied
How do you precompose a layer that has expressions linked to the main comp layers without losing the connection?
I would assume this is a frequent enough of an issue that there would be a solution for it. In fact, in my view it should be built-in to After Effects.
For some reason I can't find even anyone asking the question -- much less answering it -- by googling, so here we go.
Is it an issue that can be solved by scripting (relatively easily)?
Copy link to clipboard
Copied
To clarify, I am looking for a scripting solution that would obviate manual linking. This was moved to the expressions forum, but I'm not asking how to reconnect expressions manually, rather if there is a script, or if such can be made, that you can use to precompose a layer (or set of layers) while retaining expression references to layers outside the precomp.
Copy link to clipboard
Copied
Hi,
You can absolutely use scripting to accomplish what you're trying to do. It's not something that's super simple to explain but I can point you to the important pieces of scripting and let you figure out how the functionality needs to work to fit your wants.
Firstly, to precompose layers within a comp, you'll use the following code:
var comp = app.project.activeItem;
var indices = [/* array of layer indices to be precomposed...pulled via layer.index /*];
comp.layers.precompose("New Name", indices, true);
Now, handling the expressions is a little trickier because you need to disable them, update them, precompose and then enable them to avoid getting an error. That functionality should be relatively straightforward. When you're ready to update the expressions...you'll do it like this. Below is an example for accessing the Scale expression
var layer = comp.layers(1);
var scale = layer.property("ADBE Transform Group").property("ADBE Scale"); //can also do layer.transform.scale
if(scale.expressionEnabled) {
scale.expressionEnabled = false;
var curExpression = scale.expression;
scale.expression = curExpression.replace("old reference", "new reference");
}
}
You can also look into Regular Expressions (RegExp object in Javascript) for more advanced ways of doing a search and replace.
Just make sure your ordering is disable expression, update expression, precompose layers and then in the new comp...iterating over all layers and re-enabling their expressions.
Hope that helps!
Calvin
Copy link to clipboard
Copied
Hi Calvin and thanks for the answer.
It sounds like I'd have to write the code for each and every possible property checking for an expression with that approach -- if that is even possible, for each plug-in has it's own keyframeable properties. Which bring up the question: Is it possible to recursively search for the expressionEnabled property instead of having to explicitly check each property?
Bit new to the scripting side of AE -- literally installed ExtendScript the same day I asked the question -- but I'm at least somewhat familiar with JavaScript from other contexts.
Well, a nice challenge to start AE scripting with...
Copy link to clipboard
Copied
You can actually do it the way you describe since properties are basically indexed lists. It can get a little tricky as you dig down deeper into the layer properties (watch for infinite loops!). I would suggest checking out Property, PropertyBase and PropertyGroup in the Scripting Guide.
function recurseProps(obj) {
for(var k=1; k<=obj.numProperties; k++) {
var curProp = obj.property(k);
if(curProp.numProperties > 0) {
recurseProps(curProp);
}
}
}
I also had a background in programming so I think you'll take to scripting in AE quickly. It's not without its quirks but overall, I've been very happy with what I've been able to accomplish. Plus, the forums are pretty great for finding answers! Hope I helped.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now