ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Expression random but with some extra stuff...

COW Forums : Adobe After Effects

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Bengt Bengtsson
Expression random but with some extra stuff...
on Mar 30, 2014 at 1:10:33 pm

Hello I want to do an expression that randomly generates a number between 5 to 10 "random(5,10)"...But I want to avoid the expression to generate a number that is the same or almost the same as the number it already has generated. So if it is 5 then I want 7,8,9 or 10. If it is 6 then I want 8, 9 or 10. If it is 7 then I want 5, 9 or 10. If it is 8 then I want 5, 6 or 10. If it is 9 then I want 5, 6 or 7. And if it is 10 then I want 5, 6, 7 or 8...

Can any body please tell me how to write this kind of expression.

Best regrds: Bengt


Return to posts index

Walter Soyka
Re: Expression random but with some extra stuff...
on Mar 30, 2014 at 5:44:09 pm

Do you want the expression to avoid repeating the same value in two consecutive frames, or do you want it to avoid repeating the same value ever?

Walter Soyka
Principal & Designer at Keen Live
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
RenderBreak Blog - What I'm thinking when my workstation's thinking
Creative Cow Forum Host: Live & Stage Events


Return to posts index

Bengt Bengtsson
Re: Expression random but with some extra stuff...
on Mar 31, 2014 at 4:17:55 pm

Hello, the first alternative, I want the expression to avoid repeating the same value (and the value which is one over and one under) in two consecutive frames. But I want the expression to go on "forever" so the same value can exist many times but not in a row.


Return to posts index


Doyle Lewis
Re: Expression random but with some extra stuff...
on Mar 31, 2014 at 1:08:40 pm

Sure...
on the effect you want to choose a random value put the expression:

if (value = effect("placeholder")("Slider")){
random(5,10);
} else {
value;
}

If you notice the expression references an effect called "placeholder" I added this as a slider to be a placeholder for the value that my effect is only one frame before. So on the same layer as your effect add a slider control and name it placeholder. Then add this expression to the "placeholder" slider:

t=timeToFrames(time)
f=framesToTime(t-1)
effect("Slider Control")("Slider").valueAtTime(f)

In that expression I just attached it to a slider control instead of an effect but just highlight "effect("Slider Control") ("Slider")" and pickwhip to the effect that you are randomizing.

That may not be the best or prettiest way to do what your asking but it is how I would do it.

Doyle Lewis, Assistant Videographer

thinkck.com


Return to posts index

Bengt Bengtsson
Re: Expression random but with some extra stuff...
on Mar 31, 2014 at 6:11:48 pm

Hello thx for the answer, I did what you told me but I did not see any difference between how it was in the first case...

best regards: Bengt


Return to posts index

Doyle Lewis
Re: Expression random but with some extra stuff...
on Mar 31, 2014 at 7:27:35 pm
Last Edited By Doyle Lewis on Mar 31, 2014 at 7:45:04 pm

You are right. That doesn't quite work sorry about that. Okay this one should though.

Build a placeholder slider and paste the expression:


t=timeToFrames(time);
f=framesToTime(t-1);

effect("Your effect name here")("Slider").valueAtTime(f)

Then on your effect paste the expression:

rand= Math.round(random(5,10));
s1=effect("Placeholder 1")("Slider");

if (rand != s1){
rand;
} else if ((rand+1) <= 10){
rand+1;
} else if ((rand-1) >= 5){
rand-1;
}

Keep in mind that the placeholder slider has to be placed above the effect because after effects calculates effect on top first.

So doing this worked for me and I tested much more frames than I did the first time. Sorry again about that. The only problem is that it only makes sure it is not equal to the last frame, not that it is not equal to two frames ago. This can't be solved by adding a duplicate placeholder that subtract 2 frames and adding the code:


s2=effect("Placeholder 2")("Slider");
if (rand != s1 && rand != s2 ){

because you may still get the value of "s2" when you add or subtract 1 to "rand." I have not figured out how to fix this yet but if I come up with something I will let you know

Doyle Lewis, Assistant Videographer

thinkck.com


Return to posts index


Doyle Lewis
Re: Expression random but with some extra stuff...
on Apr 3, 2014 at 3:01:47 pm
Last Edited By Doyle Lewis on Apr 3, 2014 at 3:03:19 pm

ok so here is the expression that does everything you want it to do. I am currently rewriting it to make it look prettier but this does the trick and is still pretty fast.

rand= Math.round(random(5,10));
seedRandom(12);
rand2= Math.round(random(5,10));
seedRandom(40);
rand3= Math.round(random(5,10));
s1=effect("Placeholder 1")("Slider");
s2=effect("Placeholder 2")("Slider");
s3=(effect("Placeholder 1")("Slider"))+1;
s4=(effect("Placeholder 1")("Slider"))-1;

if (rand != s1 && rand != s2 && rand != s3 && rand != s4){
rand;
} else if (rand2 != s1 && rand2 != s2 && rand2 != s3 && rand2 != s4){
rand2;
} else if (rand3 != s1 && rand3 != s2 && rand3 != s3 && rand3 != s4){
rand3;
} else if (5 != s1 && 5 != s2 && 5 != s3 && 5 != s4){
5;
} else if (6 != s1 && 6 != s2 && 5 != s3 && 6 != s4){
6;
} else if (7 != s1 && 7 != s2 && 7 != s3 && 7 != s4){
7;
} else if (8 != s1 && 8 != s2 && 8 != s3 && 8 != s4){
8;
} else if (9 != s1 && 9 != s2 && 9 != s3 && 9 != s4){
9;
} else if (10 != s1 && 10 != s2 && 10 != s3 && 10 != s4){
10;
}


The expression that you put on the second place holder is just:

t=timeToFrames(time);
f=framesToTime(t-2);

effect("Your effect name here")("Slider").valueAtTime(f)
'

and you still need the first placeholder just like before. and they both need to be above the effect on the effects pane.


I want to write it prettier because in reality with the requirements you want for your random number there are on any given frame only 2 to 3 options so I would like to write something that narrows those down first then chooses one randomly, but I haven't figured out how to do that just yet. I might keep working on it.

Doyle Lewis, Assistant Videographer

thinkck.com


Return to posts index

Bengt Bengtsson
Re: Expression random but with some extra stuff...
on Apr 4, 2014 at 6:37:47 am

Hello, and thx again for helping me! I am very new to expressions so I think I fail in the most easiest steps... I tried to do what you told me: I created a Placeholder (file-import-Placeholder) I added an slider to it. I put the placeholder over the effect (form) in the timeline. But where you wright "your effect name here" I wonder if I should use the pickwip to get it right? It is "disperse" in the effect form that I want to change. If I pickwip it it givs me this text string:(thisComp.layer("form 2").effect("Form")("Disperse") I tried to paste that in where it says("Your effect name here". Then afx says "Bad argument: effect () index out of range Expression disabled.

Do you have any tip for me?

Thanks//Bengt


Return to posts index

Doyle Lewis
Re: Expression random but with some extra stuff...
on Apr 4, 2014 at 1:28:31 pm

Hey no problem

I am learning expressions too. They are hard, but just spend time parching through other peoples expression and looking up the parts you don't understand and eventual it starts to make sense. It is difficult though.

So I'm sorry I didn't specify. The placeholders are not actual AE placeholders. It is actually just what I named two slider effects forgetting that placeholders were the name of an actual thing in AE. So the important thing is that those placeholders(actually slider effects) have to exist on the same layer as the effect and they have to live above the disperse effect.

So it should look like this



So instead of (thisComp.layer("form 2").effect("Form")("Disperse")it should be

effect("Form")("Disperse").valueAtTime(f)

Don't forget to add the .valueAtTime(f) after you pickwhip. What this tells the expression is to use the value of the disperse effect from a frame ago. because on the first slider we calculated the time value in frames of one frame ago with the expression

t=timeToFrames(time);
f=framesToTime(t-1);


So now f is equal to current time minus one frame. and the second slider is the same just it calculates two frames ago.

Let me know if you have any more questions sorry for the confusion.

Doyle Lewis, Assistant Videographer

thinkck.com


Return to posts index


Bengt Bengtsson
Re: Expression random but with some extra stuff...
on Apr 5, 2014 at 7:51:53 am

Hello and thx again for the time you spend on this problem! Well I tried the new way and I think it is something basic stuff I am doing the wrong way... If I pickwhip the "disperse" with the placehoder slider then I got the yellow slider value (5,00) to turn red as it should. But then it is just the text "effect("Form")("Disperse")". When I ad the rest of the text as in the screen dump you posted the value turns yellow again and afx gives me two error messages:





I have tried a couple of different things but I wont get it to work... When I try it now it works like the normal random(5,10) expression.

Thanks again for your time //Bengt


Return to posts index

Doyle Lewis
Re: Expression random but with some extra stuff...
on Apr 7, 2014 at 12:39:03 pm
Last Edited By Doyle Lewis on Apr 7, 2014 at 12:59:59 pm

No problem. This is a quick fix. The error message is saying that the command timeToFrame is undefined.
The problem is the correct command is timeToFrames. You just need to make it plural. The commands are sensitive. If they are not spelled exactly correct AE assumes they are variables and then tells you those variables are undefined. Let me know if you run into any further problems.

Also While I was looking at what you wrote you have currently as the third line,
effect("Form")("Disperse")(slider).valueAtTime(f)

it should instead be:

effect("Form")("Disperse").valueAtTime(f)


Doyle Lewis, Assistant Videographer

thinkck.com


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]