Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Script to create solid above currently selected layer.

  • Script to create solid above currently selected layer.

    Posted by Daniel Hashimoto on April 13, 2018 at 8:09 pm

    I have a layer selected in my project, I want to create a solid directly above it in the layer stack. That’s the specific help I need.

    Some more detailed context (if your’e curious): I work with motioned-track data with a lot of 3-D Nulls, and sometimes I’d like to extrapolate a plane from a given 3 points.

    I’m using some info from Dan Ebberts and Walter Soyka, to cobble a script together to create a 3D solid that’s aligned to 3 3-D nulls. The script creates a 3D solid, uses Walter’s code to align it to the 3 nulls, and then uses some of Dan’s methods to bake the expression and just keep the result.

    This totally works if I have 3 3-D nulls at the top of my layer stack, and run it – but I’d love to be able to just run it with by highlighting the top-most null and running the script.

    In my ideal world, I’d shift-select the three nulls and then run this script – but I believe you can’t pull data from multiple selections into a script?

    {
    app.beginUndoGroup("Create Aligned Solid");

    myComp = app.project.item(1);
    mySolid = myComp.layers.addSolid([1.0,1.0,1.0], "Solid", 2000, 2000, 1);
    mySolid.threeDLayer = true;
    myPosition = mySolid.property("position");
    myOrientation = mySolid.property("orientation");
    myPosition.expression="I=index; p1 = thisComp.layer(I+1).transform.position; p2 = thisComp.layer(I+2).transform.position; p3 = thisComp.layer(I+3).transform.position; centroid = div(p1+p2+p3,3);";
    myOrientation.expression="I=index; p1 = thisComp.layer(I+1).transform.position; p2 = thisComp.layer(I+2).transform.position; p3 = thisComp.layer(I+3).transform.position; centroid = div(p1+p2+p3,3);n = normalize(cross(p2-p1,p3-p1)); lookAt(centroid,centroid+n)";

    function convertExpressionToValue(prop, evalTime)
    {
    if (prop.expressionEnabled && prop.numKeys<1)
    {
    prop.setValue(prop.valueAtTime(evalTime,false));
    prop.expressionEnabled = false; } return;
    }
    }
    var myComp = app.project.activeItem;
    if (myComp instanceof CompItem)
    {
    var i, numLayers=myComp.numLayers;
    var myLayer, evalTime;
    for (i = 1; i <= numLayers; i++)
    {
    myLayer = myComp.layer(i);
    evalTime = myLayer.inPoint;
    try{ convertExpressionToValue(myLayer.property("position"), evalTime);} catch(err){ };
    try{ convertExpressionToValue(myLayer.property("orientation"), evalTime);} catch(err){ };
    }
    }
    app.endUndoGroup();

    – Daniel Hashimoto
    \”Action Movie Dad\”

    Anna Petkovskaya replied 7 years, 1 month ago 3 Members · 3 Replies
  • 3 Replies
  • James Ronan

    April 14, 2018 at 8:10 am

    Try using the .moveBefore() method.

    var myLayers, myComp;
    myComp = app.project.activeItem;
    myLayers = myComp.selectedLayers; // array of selected layers

    var newSolid = app.project.activeItem.layers.addSolid([0, 1, 0], myLayers[0].name + " - Solid", myComp.width, myComp.height, myComp.pixelAspect, myComp.duration);
    newSolid.label = 9;

    newSolid.moveBefore(myLayers[0]); // moves new Solid before first selected Layer

  • Daniel Hashimoto

    April 16, 2018 at 5:36 pm

    Thank you so much! I integrated it and it’s doing just what I’d hoped.

    – Daniel Hashimoto
    \”Action Movie Dad\”

  • Anna Petkovskaya

    April 9, 2019 at 10:43 pm

    But this solution has one problem: when undoing there is one additional step, it does not delete the layer but first moves it to the very top (the place it was actually created before moving to a new position) and only one other undo will actually remove such created layer, not good.

    Is there any other DIRECT option how to place newly created layer RIGHT AWAY to a specified position (index)?

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