Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Scale selected comps, overwrite selected comps

  • Scale selected comps, overwrite selected comps

    Posted by Bryan Woods on March 7, 2016 at 1:19 am

    Found the code I need here: https://www.aenhancers.com/viewtopic.php?t=701

    But I just want it to overwrite the selected comps, not create new ones called “resize”. I figured I could just add a section that says mySelection[i].remove(); but this deletes the layers within the new comp and I can’t figure out why. Code is below:

    var percent = prompt("Resize factor (percentage):","50");
    var mySelection = app.project.selection;
    app.beginUndoGroup("scaleSelectedComps.jsx");
    for (var i = 0; i < mySelection.length; i++)
    {
    if (mySelection[i] instanceof CompItem)
    {
    var compName = mySelection[i].name;
    var W = Math.round(mySelection[i].width * percent/100);
    var H = Math.round(mySelection[i].height* percent/100);
    var pixAsp = mySelection[i].pixelAspect;
    var Dur = mySelection[i].duration;
    var frRate = mySelection[i].frameRate;
    var resizedComp = app.project.items.addComp(compName,W,H,pixAsp,Dur,frRate);
    var myLayer = resizedComp.layers.add(mySelection[i]);
    myLayer.scale.setValue([percent,percent]);
    mySelection[i].remove(); //this should just remove the current selected comp, but instead deletes the layers within that comp, and the new comp created from var ResizedComp is created empty.
    }
    }
    app.endUndoGroup();

    Bryan Woods replied 10 years, 4 months ago 2 Members · 5 Replies
  • 5 Replies
  • Dan Ebberts

    March 7, 2016 at 1:53 am

    Your script creates a resized comp, then adds the original comp as a layer and scales it. So when you then delete the original comp, you’re deleting the source for the only layer in your new comp.

    Dan

  • Bryan Woods

    March 7, 2016 at 10:19 pm

    Ah, you’re right. I didn’t take a very close look at it from the AEenhancer’s forum.

  • Bryan Woods

    March 8, 2016 at 12:34 am

    Ok, I’ve gone in and rewritten it a little bit, but I’m getting an error at line 12: “Undefined is not an object”. Not sure if I’m missing the correct syntax for scaling or what.

    var percent = prompt("Resize factor (percentage):","50");
    var mySelection = app.project.selection;
    app.beginUndoGroup("scaleSelectedComps.jsx");
    for (var i = 0; i < mySelection.length; i++){
    if (mySelection[i] instanceof CompItem){

    mySelection[i].width = Math.round(mySelection[i].width * percent/100);
    mySelection[i].height = Math.round(mySelection[i].height* percent/100);
    curComp = mySelection[i];
    for (var b = 0; b <= curComp.numLayers; b++){
    var curLayer = curComp.layer[b];
    curLayer.scale.setValue([percent,percent]); //this is where the error is
    }
    }
    }
    app.endUndoGroup();

  • Dan Ebberts

    March 8, 2016 at 1:59 am

    I’d change your layer loop to look like this:


    for (var b = 1; b <= curComp.numLayers; b++){
    var curLayer = curComp.layer(b);
    curLayer.scale.setValue([percent,percent]);
    }

    Dan

  • Bryan Woods

    March 9, 2016 at 10:33 pm

    Ah, super helpful. Thanks Dan.

    For anyone interested, here is my script. It will take all selected comps, and scale them by the factor you set, and re-center the layers in them. Simple, but strangely I couldn’t find anyone else who made something like this that worked for more than one selected comp.

    Copy and paste the below into a new text document, with a .jsx extension and throw into your AE scripts folder:

    //prompt percentage and set selections
    var percent = prompt("Resize factor (percentage):","200");
    var mySelection = app.project.selection;
    app.beginUndoGroup("scaleSelectedComps.jsx");

    //for every comp selected, set resolution based on percent value from user
    for (var i = 0; i &lt; mySelection.length; i++){
    if (mySelection[i] instanceof CompItem){
    mySelection[i].width = Math.round(mySelection[i].width * percent/100);
    mySelection[i].height = Math.round(mySelection[i].height* percent/100);
    curComp = mySelection[i];

    //for each layer within each selected comp, scale and re-center based on percent value from user
    for (var b = 1; b &lt;= curComp.numLayers; b++){
    var curLayer = curComp.layer(b);
    curLayer.scale.setValue([percent,percent]);
    curLayer.position.setValue([curComp.width/2,curComp.height/2]);
    }
    }
    }
    app.endUndoGroup();
    //done! :)

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy