How to run If command in grasshopper Expresion

Hi There,

I have got a list of y coordinates values in grasshopper, which I want to use the Expression (If command) to insert “A” in y coordinates list whenever one of the y coordinates values is bigger than its previous value plus 3.
So it basically means if Y(i+1)>Y(i)+3 then insert A

Here is a picture:

And here is the grasshopper File:

Question.gh (9.3 KB)

It seems like adding those tiny little multiplication symbols (*) does the trick.

Plus you need to have valid input for each variable in order to benefit from the realtime sanity check saying what’s wrong with the formula, or when it’s OK.

// Rolf

In your case, there’s no big difference between before & after if you don’t flip your original curve direction.
Another curve which has more sine_curve like shape shows the difference clearly.

Question_Re_V2.gh (15.3 KB)

Thanks for your response. But unfortunately, it seems like adding the multiplier sign actually multiplies the value of y by i.
But what I am looking for is that it checks the second y coordinate and compare it to the value of the first one plus 3, if the value was bigger then insert A to the list. and if it wasn’t, it inserts B. And continues doing this comparison between the third fourth and so on…
So by “i” in here i mean the number of the index (first y coordinate, second y coordinate and so on…).
However, in the picture you are showing, it is multiplying y with the i + 1 value and comparing it to Y multiplied by i summed up by 3.

Perhaps you need a scripting component instead of an expression component, where you can create loops.

1 Like

Thank you

Is it hard to create a scripting component? I am not familiar with that.

here is a python script that does what you want:

[code]#create empty list
list =

#loop through the y component list
for i in range(len(y)):
#check if current value is larger than previous+3
#if so, then add A to list
if y[i] > y[i-1]+3 and i>0:
list.append(A)
#if not, keep the original value
else:
list.append(y[i])
[/code]


Question.gh (9.9 KB)

1 Like

Maybe I’ve misunderstood your description. Check this as well.

Question_Re_V3.gh (9.7 KB)

2 Likes

Yes, all I was trying was to get rid of the error messages. I should have guessed that you intended to traverse a list. :slight_smile:

As others have suggested, traversing a list is probably easier to do with a script (I still have no idea of what you want to do though)

// Rolf

Hi,

Thank you very much. That works very well.
I have another problem as well. I tried to develop the script that you sent. but I have two problems.

This time I am trying to get the python script to loop through both y and x coordinates values. Then check if current values (of both x and y) are larger than their previous values +3 ( if y[i] > y[i-1]+3 or x[i] > x[i-1]+3 and i>0) then add A to the list.

If not, I want the script to write G1 x value y Value E value (for example G1 X90.585 Y119.095 E0.52988) Or (G1 X[i] Y[i] E)

Here is an image:

And here is the Grasshopper File:
Question2.gh (12.5 KB)

I would really appreciate if you could help me.
Thank you

thank you