Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Expression issue

Community Beginner ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

Hi to all,

 

I'm trying to save layers position's to an array and, later, recover them on depending of layer index and a random number pick up on a Null layer.

 

The expresion is this:

myArray=[];
for (var i = 1; i<=24;i++) {myArray.push(thisComp.layer(i).position.key(1));
}
 
res=chequeo();
 
function chequeo(){
var pos,x;
sliderNulo = thisComp.layer("Null 1").effect("01")("Slider");
miIndex = thisLayer.index;
 
if (sliderNulo+miIndex >= 24){
 
x=(sliderNulo+miIndex) - 24;
pos=x;
} else {
pos=sliderNulo+miIndex;
}
 
return pos;
}
 
 
myArray[res];
 
and the random number expression is this:
 
posterizeTime(3);
Math.abs(Math.floor(random(0,23)));
 
Thank you in advance
 
TOPICS
Error or problem, Expressions

Views

185

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

And what exactly is your question (aside from the code generally making no sense)?

 

Mylenium

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

Thank you Mylenium. The questiosn is that I wonder why is not working, I'm getting overlapping layers when I suppose to get distribuyed layers. The first 8 layers works poperly but the rest are repeating positions

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

And what's the issue?

 

*Martin

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

I'm getting this

Captura de pantalla 2021-04-13 a las 11.06.27.png

 

when I want this

 

Captura de pantalla 2021-04-13 a las 11.05.24.png

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

I think those lines are creating the overlapping squares:

if (sliderNulo+miIndex >= 24){
   x=(sliderNulo+miIndex) - 24;
   pos=x;
 
If you exceed 24, you are starting at 0 again:
24 >= 24: 24-24; x = 0
25 >= 24: 25-24; x = 1
and so on
 
The code is a bit confusing... if you just want to collect all positions from all layers and randomly pick one postion for each layer, who not just:
   myArray=[];
   for (var i = 1; i<=24;i++) {
      myArray.push(thisComp.layer(i).position.key(1));
   }
 
   rndIndex = Math.floor(random(0,25));
   myArr[rndIndex];
 
I think you can even randomize the array (https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) and take each layers index as array index to easily avoid douple postioning.
 
If you just aiming for shuffling colors, just randomize the color of each square.
 
Give us more details, so maybe we can point you to a better/working solution.
 
*Martin

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 13, 2021 Apr 13, 2021

Copy link to clipboard

Copied

Thank you Martin,

 

The reason why I though I have to use those lines is that I wanted to no repeat positions of the squares and there are 24 layers, so the layer index plus the result of the random give me one unique number for that layer, and if it pass 24 it could use an index whic it's in the range (0-24). In addition to this I use the $.evalFile() to only write one expression.

 

"If you just aiming for shuffling colors, just randomize the color of each square." It's another approach I was thinking of, but I wanted to try to learn how to make it works this way

 

"I think you can even randomize the array (https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) and take each layers index as array index to easily avoid douple postioning." Thank you for this, I'll try this later

 

Thank you

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Apr 14, 2021 Apr 14, 2021

Copy link to clipboard

Copied

"so the layer index plus the result of the random give me one unique number" - yes, that is true and you'll get the same result just using the random in a range of 0,25. It's easier, less lines, more logic.

 

However, none of those approches will prevent you from getting repeated postions, because 7.4566 and 7.1293 are two unique random numbers, but math.floor(random(...)) will make both of them to 7.

Using Math.round() might be a better approach, but still not a bulletproof solution (7.64 and 8.001 will be both 8).

 

This is a good example why expressions really should have global variables...

 

My recommendation is to just shift the colors, not the squares, because you cannot easily prevent having double integers, as I stated above. When only shifting colors, however, you can prevent a visual "black hole" - instead you will only get same colors two times which is hard to notice anyways.

 

One last glimps of hope is, that expressions might be evaluated layer by layer and not parallel for all layers. I'm not sure about this, but if AE is evaluating layerwise, you can check if another layer is already at the postion you want to set the current layer to.

In this scenario, you need to know if AE evaluates from top to buttom of the layer stack, or buttom to top.

Taking the second case, a layer could questioning: "I picked a random number and this turns out to a given position, is a layer below me already at this position? If yes, I pick another random number and ask again - if not, I take this position".

 

*Martin

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 14, 2021 Apr 14, 2021

Copy link to clipboard

Copied

LATEST

Thank you Martin,

 

As you said, there were "black holes" in those approaches. I've tried the shuffleArray from Slack, and precomposing and duplicating those I've fixed it, so it works for me, but as you also said, it wold be nice to have global variables. 

 

Thank you for your help .

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines