ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Complex ways to specify width and height for expressions.

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Nestor Mojica
Complex ways to specify width and height for expressions.
on Dec 29, 2016 at 5:25:12 am


I am fairly new to writing expressions in Adobe After Effects and need some help. I am trying to write an expression that will generate 300 positions for 300 location-pin images inside a composition without having to manually move each pin to a new location inside the composition.

To do that, I created an expression for the first location pin's position as follows:

Then copied and pasted it 300 times.

The problem with this is that it sets the random position to the entire composition's width and height, whereas I want it to set to x and y values that are inside the confines of a not-perfectly-shaped map in the composition. I have tried to mask my object and then write something like this:

temp = thisComp.layer("USAMAP").mask("Mask 1").maskPath.width;
temps = thisComp.layer("USAMAP").mask("Mask 1").maskPath.height;

But as I mentioned, I'm still really new to this, so any help is greatly appreciated.

Side question (not as important as my first): When dealing with tons of small images on a very large scale like 300+, is there an easier way to replicate the image without physically copying and pasting? Is there some sort of code that tells After Effects to copy an image and run it `x' amount of times in the background? As of now, I've been using sequence layers and stretching the right tail-end of my timeline for all 300 images. I'm just curious.

Thanks for all the help!

Return to posts index

Kevin Camp
Re: Complex ways to specify width and height for expressions.
on Dec 29, 2016 at 6:25:59 pm

the easiest way would be to use a particle generator that could can use a layer map as an emitter/producer, thus only producing pins with the layer map... trapcode's particular can do this, as can particle playground (though it's more complicated).

the negative side of these particle systems is that the layer map is really just used as a visibility map, in that particles do get produced outside that layer map, but you can't see them... so if you truly need 300 pins with the map boundaries, it would be tough to get exactly 300.

you could use the same setup that you have now, but add a sampleImage() expression to the opacity of each pin... sampleImage() can be used to look at pixels in another layer, and base on what it sees you could have the layer turn on or off. this has the same drawback as the particle system/layer map, where you may have 300 pins in the comp, but maybe only 200 are visible because those 200 are within the map layer....

if you really need 300 pins, then I think i'd use your expression to get the randomness of most of the pins, but then manually move any pins outside of the map so they lay within the boundaries. simply adding the pin layer's position into the random expression will allow you to do that:
value + random([thisComp.width,thisComp.height])

you'll likely need to zero all the pin positions by simply selecting all of them and setting one of the pin's positions to 0,0. then simply drag any pins that are outside the map to be within the map boundaries.

the only other way i can think of would be to incorporate sampleImage() into the position expression to test if it was within the boundary of the map, and if not, then change the random seed until it found a location that was. this could get computationally heavy as these expressions get calculated every frame, and you'd likely just get many pins that have the same random seed, and thus the same location. so i don't think that's a good route to take.

Kevin Camp
Art Director

Return to posts index

Nestor Mojica
Re: Complex ways to specify width and height for expressions.
on Dec 29, 2016 at 9:34:12 pm

This is extremely helpful. Thank you so much!

Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 All Rights Reserved