Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions masking video clips automatically – issues

  • masking video clips automatically – issues

    Posted by Andy Engelkemier on November 1, 2022 at 3:24 pm

    I made a giant collage of rectangles and have a source below each rectangle, using that rectangle as a mask.

    I’m auto sizing the sources by assigning the position to the layer above first, then since they are actual rectangles, just doing rectangle height / source.height *100.

    I added the ability to mirror the footage as well, but that’s basically it.

    But then I wanted to be able to pan and scale the footage further to get the right crop for each, so added a transform modifier. And that’s where I failed. I was Hoping I could create some limits there. I tried using sourceRectAtTime and compare this layer left with the rectangle left. I thought that would take the actual contents into consideration, but it seems that it’s ignoring the transform effect?

    I have an adjustable stroke between all of the footage, currently at 10px. What I was hoping to avoid was me not noticing that when I adjusted the transform of the footage, accidentally leaving something like 12-14px on one side and not noticing. It’s a collage of 151 rectangles, so that leaves plenty of space for human error.

    In me trying to put it together, this is as far as I got before thinking maybe sourceRectAtTime doesn’t work with effects? My next attempt will be adding an expression slider for adding X, then adding that value to the position of the layer, avoiding the transform altogether, and see if that will work. OR, maybe one of you gurus will see a glaring error in how I’m trying to write this.

    ____on a transform effect > Position____

    maskLayer = thisComp.layer(index-1);
    maskL = maskLayer.sourceRectAtTime().left;
    maskW = maskLayer.sourceRectAtTime().width;
    maskR = maskL + maskW;
    thisL = thisLayer.sourceRectAtTime(includeExtents = true).left;
    thisW = sourceRectAtTime().width;
    thisR = thisL + thisW;
    reset = effect("Transform")("Anchor Point");
    if(thisL < maskL){x = maskL;}
    else if(thisR > maskR){x = maskR;}
    else x = effect("Transform")("Position")[0];
    y = effect("Transform")("Position")[1];
    [thisL,y]
    Andy Engelkemier replied 3 years, 6 months ago 2 Members · 4 Replies
  • 4 Replies
  • Filip Vandueren

    November 2, 2022 at 9:58 am

    hi Andy, it’s in the name: sourceRect -> it looks at the the source of the layer, not what it becomes through effects or transformations.

  • Filip Vandueren

    November 2, 2022 at 10:15 am

    I think this’ll work for your transform effect’s position:

    mask_w = thisComp.layer(index-1).sourceRectAtTime().width;
    wiggleRoom = source.width - mask_w/(scale[0]/100);
    x=clamp(value[0], (source.width-wiggleRoom)/2, (source.width+wiggleRoom)/2);
    [x,source.height/2];

    Assuming you start from the default settings where anchorPoint and position are nice and centered.

  • Filip Vandueren

    November 2, 2022 at 10:24 am

    Oh yeah, and if you want to make sure the image always fits, also when the mask is wider in aspect ratio than your image, so you’ll want to do it in 2 dimension:

    Transform -> scale:

    maskSize = thisComp.layer("Shape Layer 1").content("Rectangle 1").content("Rectangle Path 1").size;
    if (maskSize[0]/maskSize[1] < source.width/source.height) {
    [100,100]*maskSize[1]/source.height;
    } else {
    [100,100]*maskSize[0]/source.width;
    }

    Effect -> Transform -> Position ->

    mask_w = thisComp.layer(index-1).sourceRectAtTime().width;
    mask_h = thisComp.layer(index-1).sourceRectAtTime().height;
    wiggleRoomX = source.width - mask_w*(100/scale[0]);
    wiggleRoomY = source.height - mask_h*(100/scale[1]);
    x=clamp(value[0], (source.width-wiggleRoomX)/2, (source.width+wiggleRoomX)/2);
    y=clamp(value[1], (source.height-wiggleRoomY)/2, (source.height+wiggleRoomY)/2);
    [x,y];
  • Andy Engelkemier

    November 2, 2022 at 1:03 pm

    Thanks. I figured I’d have to calculate the sizes based on the source width and height, but also calculating for scale.

    I chose to use the rectangle dimensions directly, rather than sourceRectAtTime because I thought that would be faster to calculate (but I have no idea and I’ve found testing to be pretty inconsistent) and all of my footage is 16:9 so same, I checked against 1.77778 rather than dividing again. I’m sure it’s minuscule, but seemed like it should be faster.

    I wanted to also be able to scale with the transform effect on top of the existing scale.

    I ended up skipping that aspect, just because I needed to move on.

    I think the way to do it, since I wanted 2 scales, would be to add 3 sliders for X, Y, and scale. and handle the expressions all within the main scale expression and transform position. That way the position expression only has to look at the final scale, like you’ve done.

    And yes, I thought about the anchor point in the same way you did, so wrote an expression to ensure it was centered even if I tried to move it. With the 150 videos (not stills), and me trying to save some hard drive space so they are H.264, it looks like render time will be around 30-40seconds per frame. Not ideal, but I don’t know much about optimization for this. I’d probably spend more time on the larger ProRes downloads, and possibly rerendering them at 720 instead of 1080 than just waiting for it to render when I go to export.

    One thing I found to be really odd was I have a checkbox for mirroring X optionally, which just has an if statement to multiply the X value by -1 if the checkbox is checked. I noticed the render time was faster if the box was checked. I though, “oh, maybe it’s because when it evaluates the if statement it gets to skip the else?” So I switched the statement around. But that didn’t do it. The frame render time was still significantly different if the scale ended up being multiplied by -1. And it’s things like this that make me Not want to learn much about optimization. I also noticed that if I hit page up to go to the next frame, currently my frames are taking about 3.5 seconds. But if I hit space or Num0, the frames take 8.7seconds. I’m guessing it has something to do with multiframe rendering, and this being a case where that makes it worse?

    Anyway, I appreciate the help. Once I’m finished I’ll probably go back and write it out using your method, but adding the extra scale option. But for now, I needed to just get the footage all in there. Finding 151 pieces of unique footage, downloading them, and replacing the image footage…..it takes a while.

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