if then else problem

Moderators: Disciple, zlovatt

Post Reply
Foudou
Posts: 19
Joined: March 27th, 2009, 1:54 am

I have often problems with using condition test in my expression.

The code should work, but the logic of expression doesn't make what i think.

This is the last example.

I want that the object who i had expression on position, use random position value until it finds position value over a reference calque (by using sample Image and alpha)

The purpose is to have a random dispersion of object (solid or layer) who take the form of the alpha of a layer named Reference.

This is the code :

Code: Select all

if (time<1) {
P = [random(thisComp.width),random(thisComp.height)];
} else {
	P1=value;
	T1=thisComp.layer("Reference").sampleImage(P1);
	if (T1[3]=1) {
	P1;
	} else {
		while (T2=0) {
		P2 = [random(thisComp.width),random(thisComp.height)];
		T2 = thisComp.layer("Reference").sampleImage(P2);
		};
		P2;
	};
};
Some explanations.

The time test is for randomizes the first position of the object, in order to duplicate many times this things whithout keep a same position.

The expression record the actual value and test if the object is over the reference layer, if it's true (the alpha value equals 1) he keep the position.
If he is not over, it test random value until he found one over the reference layer.

The problem is that if i write

Code: Select all

if (T1[3]=1) { 
it's always moving.
And if i write

Code: Select all

if (T1[3]>0) {
it's doesn't move even if the object is over the void.

Using script could be a better way, but i want to use expression, in order to create a more interactive tool for my project (GUI panel is too hard for me).

Thanks.
Image
FrenchEnglish inside.
Yenaphe
Posts: 84
Joined: February 3rd, 2009, 6:30 pm
Location: Paris - France
Contact:

Does it work if you try this:

Code: Select all

if (T1[3]) {
Foudou
Posts: 19
Joined: March 27th, 2009, 1:54 am

No change.

I have tried this code on opacity and it works :

Code: Select all


  P1=transform.position;
   T1=thisComp.layer("Reference").sampleImage(P1);
   if (T1[3]) {
   100
   } else {
      while (T2=0) {
      P2 = [random(thisComp.width),random(thisComp.height)];
      T2 = thisComp.layer("Reference").sampleImage(P2);
      };
   100
   };
It's the same logic.

Maybe the problem it's caused by the logic on the position value, value test and his application.
Image
FrenchEnglish inside.
kobyg
Posts: 128
Joined: December 7th, 2007, 10:11 am

Foudou,

Whenever you are using a condition (such as in IF or WHILE functions) you must use == in order to check equality between two things. If you use = the script will do an assignment and willl then take the result of the assignment as the value for the condition...

This is why when you do:

Code: Select all

if (x=1) { ...
you are actualy doing:

Code: Select all

x = 1;
if (1) { ...
Instead of what you need, which is:

Code: Select all

if (x==1) { ...
I guess this is why you got strange behaviour when you used IF conditions...

Koby.
Foudou
Posts: 19
Joined: March 27th, 2009, 1:54 am

thanks kobyg

I think it's a part of the problem.

I have try this in order to locate the origin of my bug.

Code: Select all

P1=[random(thisComp.width),random(thisComp.height)];
   T1=thisComp.layer("Reference").sampleImage(P1);
   if (T1[3]==1) {
   P1;
   } else {
	[0,0]
   };
The solid stay in the corner of the comp many part of time, and sometimes it moved for just a frame in a positon on the reference form.

This code works too.

Code: Select all

P1=transform.position;
P2=[random(thisComp.width),random(thisComp.height)];
   T1=thisComp.layer("Reference").sampleImage(P2);
   if (T1[3]==0) {
   P1;
   } else {
   P2;
   };
Now i have to "freeze" the position value when they are at a good position.

Frame by frame the solution is near.

Thanks.
Image
FrenchEnglish inside.
Dan Ebberts
Posts: 320
Joined: June 26th, 2004, 10:01 am
Location: Folsom, CA
Contact:

See if this is close to what you're looking for:

Code: Select all

seedRandom(index,true);

do {
  P = random([thisComp.width,thisComp.height]);
  R = thisComp.layer("Reference");
  T = R.sampleImage(R.fromComp(P));
}while(T[3] < 1);

P
Actually, this is probably better if you want the layers to maintain their positions relative to Reference as Reference moves around:

Code: Select all

seedRandom(index,true);

do {
  R = thisComp.layer("Reference");
  P = random([R.width,R.height]);
  T = R.sampleImage(P);
}while(T[3] < 1);

R.toComp(P)

Dan
Last edited by Dan Ebberts on May 5th, 2009, 7:19 am, edited 1 time in total.
kobyg
Posts: 128
Joined: December 7th, 2007, 10:11 am

Try this (I hope I understood correctly what you are trying to do):

Code: Select all

do {
   P1=[random(thisComp.width),random(thisComp.height)];
   T1=thisComp.layer("Reference").sampleImage(P1);
} while (T1[3]==0);
P1
p.s.: I don't have access to AE right now, so I can't debug this... I hope I didn't miss something.

Koby.
Foudou
Posts: 19
Joined: March 27th, 2009, 1:54 am

Thanks Dan and kobyg, it works.

But now the next part of my effects doesn't work.

Maybe it's impossible, but this problem is for a other topic.

Thanks for all.
Image
FrenchEnglish inside.
Foudou
Posts: 19
Joined: March 27th, 2009, 1:54 am

New problem.

I make my layer reference as 3D layer and move it fare away from camera (like -30000 in Z axis) because i'm using an trapcode horizon configuration. So i need to scale-it up (like 1500%) to have a good size.

My solid are now 3D too, and i have changed the code with a toWorld instead of toComp.

Code: Select all

seedRandom(index,true);

do {
  R = thisComp.layer("Reference");
  P = random([R.width,R.height]);
  T = R.sampleImage(P);
}while(T[3] < 1);

R.toWorld(P)
But i have an difference between the layer reference and the solid position.

If i used 150 solid i have a little version of my reference form under and at right of the reference layer.

I'm not aware of the exact translation of the value with toWorld, and how i have to consider the scale factor in the calculation of the solid position.

I have noticed than when the reference lyer is near the camera the difference exist but is really small, it increase with the z-axis value.

Strange. I will read teh dan ebberts site section about toComp and to World, in order to understand. But if somebody have a first clue or idea...

Thanks.
Image
FrenchEnglish inside.
Post Reply