Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Expression that parents existing nulls to existing layers… maybe by #, in a loop, till all are parented?

  • Expression that parents existing nulls to existing layers… maybe by #, in a loop, till all are parented?

    Posted by James Poulakos on July 5, 2016 at 10:45 pm

    Ultimate goal: a human brain made of icons. Camera flies through it.

    My hope: can an expression be used to automagically parent each icon to an existing null?

    Details:

    I have a cloud of nulls, each one placed in 3D space. If you could see them, it would look like a human brain modeled in 3D.

    The nulls have names with numbers, ie “_Empty_001”

    I want to parent a 3D layer to each null (so that I can put an image in the layer that always faces the camera. Facing the camera’s easy).

    The 3D layers have numbered names, too, ie “misc icons isolated-10.png”

    Can an expression do the repetitive work of parenting these together, so that each null gets one (1) layer parented to it?

    I was hoping an expression could do something like this:

    Look at Null name.
    Find 3D Layer that has same number in its name.
    Parent 3D layer to Null.
    Go to next Null and repeat till ….? (I could enter a number).

    I don’t actually care if the numbers match. What I’m after is to assign each 3D layer, to its own null. That will create a 3D object made up of little images. I need to be able to vary the images, so I’d like to simply use a 3D layer, because I could replace an image with a comp full of images, if I need to.

    So I suppose this psuedocode recipe would work, too:
    Look at 3D layer: does it have a parent Null?
    If no, find a Null that has no child.
    Parent this layer to Null.
    Repeat till all layers have parent nulls.

    Can it be done? How would you write it?

    James Poulakos replied 9 years, 10 months ago 2 Members · 5 Replies
  • 5 Replies
  • Dan Ebberts

    July 5, 2016 at 11:24 pm

    An expression can’t do that, but a script could. If you don’t care which null goes with which image, it might look like this:


    var myComp = app.project.activeItem;
    var myNulls = [];
    var myImages = [];
    var myLayer;

    for (var i = 1; i <=myComp.numLayers; i++){
    myLayer = myComp.layer(i);
    if (myLayer.nullLayer && myLayer.threeDLayer){
    myNulls.push(myLayer);
    continue;
    }
    if ((myLayer instanceof AVLayer) && myLayer.threeDLayer && myLayer.source != null && myLayer.source.mainSource.isStill){
    myImages.push(myLayer);
    continue;
    }
    }

    for (var i =0 ; i < Math.min(myNulls.length,myImages.length); i++){
    myImages[i].parent = myNulls[i];
    }

    Dan

  • James Poulakos

    July 8, 2016 at 4:03 pm

    Dan, thanks so much. That almost does the trick.

    But the parented images don’t move to the positions of their parents. They just sit there, exactly where I placed them. They move in relation to the nulls, but I need each one to be relocated to its parent’s position in 3D space.

    That’s normal parenting behavior. I need the parenting behavior you get when you hold down Shift while parenting. This snaps the child to the parent’s position.

    Is there a way to modify the expression so that they get parented the same way that parenting works when I hold down Shift?

    Or, if this is a quicker fix: I’m searching for a way to paste an expression on the child’s position that simply tells it: ‘make your position the same as your parent’ … but the methods I’m finding all seem to require an explicit reference to a parent, by its name. That won’t work. It would have to be “this layer’s parent, whatever that may be called”.

    There’s gotta be a way to refer to a given layer’s parent, in expressions, without knowing in advance what the layer’s name is, right?

  • Dan Ebberts

    July 8, 2016 at 4:26 pm

    You just need to add one line to the script, like this:


    var myComp = app.project.activeItem;
    var myNulls = [];
    var myImages = [];
    var myLayer;

    for (var i = 1; i <=myComp.numLayers; i++){
    myLayer = myComp.layer(i);
    if (myLayer.nullLayer && myLayer.threeDLayer){
    myNulls.push(myLayer);
    continue;
    }
    if ((myLayer instanceof AVLayer) && myLayer.threeDLayer && myLayer.source != null && myLayer.source.mainSource.isStill){
    myImages.push(myLayer);
    continue;
    }
    }

    for (var i =0 ; i < Math.min(myNulls.length,myImages.length); i++){
    myImages[i].property("Position").setValue(myNulls[i].property("Position").value);
    myImages[i].parent = myNulls[i];
    }

    You could instead add this expression:

    [0,0,0];

    To answer your other question, you can refer to layer’s parent like this:

    parent.position

    parent.rotation

    etc.

    Dan

  • James Poulakos

    July 8, 2016 at 4:31 pm

    I think I just found a solution to the position problem: if I set the child’s position to 0,0,0 *but only after parenting it*, it jumps to the position of its parent.

    I can change all of the children’s positions at once (thanks to After Effects’s brilliant keyboard shortcuts and its ability to edit a parameter on all selected objects at once).

    Before I do any of this (running your script, editing their position), I must remember to set all image layers as 3D (duh) and set them to auto-orient to face the camera. If I leave the auto-orient step out till later, it won’t work, but if I apply it before I parent them, the orientation works.

  • James Poulakos

    July 8, 2016 at 10:45 pm

    Whoops, I was writing my long post while you were posting your more elegant solution.

    Wow, great to know that line about setting a layer’s position equal to another’s. Thanks again!

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