Code a loop in python

Hi everyone,

Can anyone please translate this set of nodes into a python loop? I still don’t really grasp the essence of the conventional programming language and with the visual programming you can’t really do this kinds of things…

Loop.dyn (21.0 KB)

Try to learn Python basic first. It is much better that you learn how to code instead of someone is giving you the “result”

if you code loops in python then you can do it like this

for item in list:
   do soemthing with the item...

if you need the index of an item, then do it like this

for index, item in enumerate(list):
   do something with the list[index] or the item...

example if you have two lists of same length and you dont want to loop over two lists (a matter of RAM usage)

compare  #a comparable item
list1  #list of item to search values in
list2  #list of values

for index, item in enumerate(list1):
    if compare == item:
        give me item in list2[index]
1 Like

I got as far as this… cries in the corner

you dont append anything…

lst.append(something)

start learning Python :slight_smile:

1 Like

Thank you for the replay anyway

I am sorry but your code doesn’t really do anything. It is a matter of understanding how basic python (and programming in general) works.

If you loop a list and have an item “a”, then this “a” must be used for something. What you do is calling a function in dynamo but it doesn’t use “a”, and what is it you want to append?

It is better to learn how things work. Otherwise, will I suggest you ask someone to do it on consultancy basis for you.

Not sure what you’re looping with - there isn’t a start or end or a limit, but, try to make a custom node or call the functions in a custom design script definition. You’d be amazed what you can do.

What I want is to create spheres from start point of a line to the end where the center point of each sphere is the intersection point of previous with the line. Maybe loop isn’t the correct description but I need to iteratate the process so that I can feed lines of any lengths.

I do use costume nodes as functions but I don’t get how to do it here. I’ve tried yesterday to python it but I struggle to understand the logic of the code. VP is easier but looping is for me a bit to tricky.

I feel like I’am banging against a wall while there’s missing only one thing…

There are many mistakes here…

First, a good way to define a function in DesignScript is to tell Dynamo what are the types of each variable : is it a number, a point ?
Second, in your loop, you are treating i as both a point and a int. Which one is it ?
Third, you are in a for loop, but iterating on a object that is (i guess…) not a list (startpt), which is a wrong way to define for loops. Try iterating on a list.
Fourth, if i is an integer (I don’t know what you trumly want it to be actually), why are you incrementing it ? You are already in a for loop.
Fifth, from what I see, you don’t even need a loop, just as @JacobSmall said. You may want to take a look at Dynamo list management principles : http://dynamoprimer.com/en/06_Designing-with-Lists/6_designing-with-lists.html

You appear to have some problems with coding in general (it’s ok, everybody was at that point at some moment). As @erfajo said, don’t try to rush things. Try to learn how Python/DesignScript/coding works before trying to do such things on your own.

2 Likes

Hey,

Just to get you going, I’ve tried to interpret your description into a graph…

If so, we can look at interpreting that into code?

Hope that helps,

Mark

CirclesAlongALines.dyn (11.9 KB)

1 Like

@mellouze How to do that?

It has to be a point.

Here’s what I want: take first point and detect end point of the intersecting curve with the original curve, then take that point and repeat the function until end point of the intersecting curve is the same point as end of the original curve (or i can divide the length of the curve by the ctc and take that as repeat amount). So I’m starting with a point and want to end with a list of points.

So, just so we’re clear: You want a series of equidistant points along a curve?

Wouldn’t it be easier to just use Curve.PointsAtEqualSegmentLength or alternatively Curve.PointAtEqualChordLength? (I’m not really sure what exactly the difference between those two is, aside from giving slightly offset solutions from each other).
Or if you want to control the distance rather than the number of divisions, Curve.PointsAtChordLengthFromPoint.

If these don’t solve your problem, why do you specifically want to use intersections?

1 Like

Then what is the meaning of the line i = i+1 ? Plus, you are in a for loop, that line should never be necessary.

Just do as @Avz is suggesting. Your problem does not appear to need a loop at all.

One goes with the “normal” distance whereas the other goes with the curvilinear distance i guess.

1 Like

@mellouze @Avz
Maybe Curve.PointAtEqualChordLength is indeed what I actually need, but I don’t see a way to control the exact length of the lines. Especially when it’s 3D curved.

Not really. I need those points to be at distance that I choose. So if there is a left over length of the curve, it can be neglected.

Is that what you want ?

spacement.dyn (14.5 KB)

1 Like

In that case:

It allows you to set your curve, point (i.e. Curve.StartPoint), and the exact distance you want.

1 Like

This is exactly what I needed! :grin:
@Avz @mellouze Thank you very much guys!

1 Like