Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions After Effects Scripting – swapping composition dimensions

  • After Effects Scripting – swapping composition dimensions

    Posted by Tom Holmes on April 27, 2016 at 11:03 am

    Hey hey!

    I work with both HD portrait and landscape video and I often need to swap between the dimentions so I’ve created a little script that basically swaps the X and Y dimensions of a comp:

    var comp = app.project.activeItem;
    var compSize = [comp.width,comp.height];
    comp.height = compSize[0];
    comp.width = compSize[1];

    eg. 1920×1080 becomes 1080×1920 (instead of opening the comp setting and changing it manually)

    It works but the problem I’m having is that the content of the comp does not behave in the same way that it does when you change it manually. When you change the comp size manually, position properties of all layers update so they are relative to the frame size. For Example:

    A 1920×1080 comp has a solid which is centred in the frame at position 960,540…. If i change the comp dimensions manually (using composition setting window) to 1080×1920, the comp appears to resize from the centre and the solid position will remain in the centre of the frame but will have a new position value of 540×960. If i use the script however, the solid’s position will not change (as if the composition is being scaled from the top left) and will then end up in a different position (visually speaking).

    Is there something i can add to the script which will make the changing of the frame size behave the same as if i was doing it manually with the composition settings window, ie. have the comp resize from the centre.

    I understand it might seem odd why i want to do this, but it’s part of a larger.

    Any light you can shed would be amazing!

    Thanks!

    Tom

    Ross Klettke replied 10 years ago 2 Members · 2 Replies
  • 2 Replies
  • Ross Klettke

    April 28, 2016 at 5:39 pm

    Haven’t fully tested it out but attached is something that might help

    //

    Borrowed an idea and a bit of code from “Scale Composition.jsx” — thanks to whoever wrote that (I believe it comes bundled with AE)


    function makeParentLayerOfAllUnparented(theComp, newParent)
    {
    for (var i = 1; i <= theComp.numLayers; i++) {
    var curLayer = theComp.layer(i);
    if (curLayer != newParent && curLayer.parent == null) {
    curLayer.parent = newParent;
    }
    }
    }

    var comp = app.project.activeItem;
    var origCompSize = [comp.width,comp.height];

    var null3DLayer = comp.layers.addNull();
    null3DLayer.threeDLayer = true;
    null3DLayer.position.setValue([origCompSize[0]/2,origCompSize[1]/2,0]);
    makeParentLayerOfAllUnparented(comp, null3DLayer);

    var newCompSize = [origCompSize[1], origCompSize[0]];

    comp.width = newCompSize[0];
    comp.height = newCompSize[1];

    null3DLayer.position.setValue([newCompSize[0]/2, newCompSize[1]/2,0]);

    null3DLayer.remove();

  • Ross Klettke

    April 28, 2016 at 5:44 pm

    Just a note that the top for-loop should have:

    i <= theComp.numLayers

    with a less-than sign — not, the weird html entity thing

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