Copy link to clipboard
Copied
Hello dear scripters.
I'm trying to count the layers within a precomposed layer.
has somebody a solution for my problem?
Thnx a lot.
:F
This is my code:
var curComp = app.project.activeItem; // this is my acive comp
var theSelection = curComp.selectedLayers; // i selected some comps
var compLayer = theSelection[0]; // compLayer is also a comp
// and here is my problem
// this doesn't return the number of layers within my compLayer
var theNumber = parseInt(compLayer.numLayers);
alert(theNumber); // returns undefined but i need an integer
The precomp is the source for the layer. Try this:
var myComp = app.project.activeItem;
var myLayer = myComp.selectedLayers[0];
alert (myLayer.source.numLayers);
Dan
Copy link to clipboard
Copied
The precomp is the source for the layer. Try this:
var myComp = app.project.activeItem;
var myLayer = myComp.selectedLayers[0];
alert (myLayer.source.numLayers);
Dan
Copy link to clipboard
Copied
Great. That works.
Thnx again for your help Dan.
:F
Here is the working code.
It takes a selection of comps in the active comp.
Enables timeremapping.
Adds a null as controller with some sliders.
And adds an expression to the timeremapping
this just works with CompItems.
It is usefull if the layers in the comps are sequenced with one frame length
// written by fabiantheblind
// http://www.the-moron.net
// with a hint by Dan E.
// http://forums.adobe.com/message/3310628#3310628
applyExpression ();
function applyExpression(){
// begin an Undo group
app.beginUndoGroup("apply Expressions");
// check if a comp is active
var curComp = app.project.activeItem;
if (!curComp || !(curComp instanceof CompItem))
{
alert("Please select a Composition.");
return;
}
// get the selection
var theSelection = curComp.selectedLayers;
// you could use this for a null within the workarea
//var theController = curComp.addNull(curComp.workAreaDuration);
// add a null object to the comp for the slider controls
var theController = curComp.layers.addNull();
// make a unique name for the null. dont use the date its just a reminder
theController.name = prompt("Enter an unique name for the controller Null Object", "control_"+ Date().toString());
// loop thru selection
for (var layerId = 0; layerId < theSelection.length; layerId++)
{
var layer = theSelection[layerId];
// get the number of layers within the layer
// -1 because
// layers start at index 1 frames start at 0 Frames
// Thnx Dan for the word ".source."
var theNumber = parseInt(layer.source.numLayers -1);
// add a slider control to the null obj.
var theSliderEffect = theController("ADBE Effect Parade").addProperty("ADBE Slider Control");
// change the name of the slider to something usefull
theSliderEffect.name = layer.name + "_cntrl_" + layerId;
// this is for the timeremapping to make em all hold keyframes
var holdInt = KeyframeInterpolationType.HOLD;
// get the timeremapping property and enable it
var property = layer.property("ADBE Time Remapping");
layer.timeRemapEnabled = true;
// set all keyframes to hold
for (var i = 1; i <= property.numKeys; i++){
property.setInterpolationTypeAtKey(i, holdInt, holdInt);
}
// remove the last keyframe. dont need it
property.removeKey(property.numKeys);
// check if the property can take expressions
// dont need it. but i need more checking
if (!property.canSetExpression) { continue }
// apply the expression with
//the name of the slider
// the name of the control null
// and the number of layers within the comps
property.expression = exprString(theSliderEffect.name, theController.name, theNumber);
}
app.endUndoGroup();
}
// this function builds the expression string
function exprString(inEffName, inControllerName,inNum){
// the name of the effect
var theEffName = inEffName;
// the name of the control null obj
var theCntrlName = inControllerName;
// the number of layers within the timeremapped comps
var theNumber = inNum;
// the expr will be something like this: !!watch out german expression!! (replace "Schieberegler" with "Slider Control"):
// this expression remaps the frame number within the comp to values from the slider
// val = Math.floor(thisComp.layer("control").effect("MovementControl")("Schieberegler"))*thisComp.frameDuration;
//if (Math.floor(thisComp.layer("control").effect("MovementControl")("Schieberegler")) < 0){val =0;
//}if (Math.floor(thisComp.layer("control").effect("MovementControl")("Schieberegler")) > 3){val =3* thisComp.frameDuration;
//}
// build the string with the variables from the selection
var theString = "val = Math.floor(thisComp.layer(\"" + theCntrlName + "\").effect(\"" + theEffName +"\")(\"Schieberegler\"))*thisComp.frameDuration;\n"+
" if (Math.floor(thisComp.layer(\"" + theCntrlName + "\").effect(\""+ theEffName +"\")(\"Schieberegler\")) < 0){val =0;\n"+
" }if (Math.floor(thisComp.layer(\"" + theCntrlName + "\").effect(\""+ theEffName +"\")(\"Schieberegler\")) > "+theNumber+"){val ="+theNumber+"* thisComp.frameDuration;\n }" ;
// and return the string
return theString;
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now