Trouble shooting while loop

I have some lofted geometry that I need to intersect a plane and return a specific curve length. I’ve been able to construct the geometry with dynamo and measure the length of the intersection. I am trying to use a while loop to incrementally adjust one of the points controlling the loft until I reach my target length.

I’ve poked around on this forum and in the design script documentation but I think I’m getting the syntax wrong.

I’ve set up an imperative in a code block with the variables “translate” and “increment”. “Translate” is the initial amount that I want to move the point by, and then I want it to keep moving in steps based on the “increment” value. I want this to keep looping until the length of the intersection between lofted surface and plane reaches my target length.

I suspect my problem is in getting the result from the “translate = translate + increment” function in my while loop pushed out of the code block and into the other parts of my script controlling the lofted geometry.

whileloop_troubleshoot

There are two problems, one of which might be a bug:

  1. TargetLength has a typo (in bold) which means the loop statement is using an unassigned variable. This looks like a bug as I would expect to see an exception. You should post this on the Dynamo GitHub and see if the Dynamo team can verify this. cc @Aparajit_Pratap. In any case your conditional is meaningless - its likely to be evaluating against null which means your while loop never executes as the result is always false

  2. You are not adjusting your actualLength in the statement body which means once you fix issue 1 the result of your conditional will always be true…and that will trigger a neverending loop. Try: actualLenght = actualLenght + increment;

If you are using a while you should also add a failsafe to prevent neverending loops, like adding a counter which can be evaluated in the conditional to terminate the loop if it hits a certain count (i.e. set the max number of cycles).

Thanks for your input Thomas. I want the actualLength to be updated based on the number I am returning from the watch node that is plugged into the code block.

This number is coming from another code block that I am using to measure a curve length elsewhere in the script.

I guess what I am getting confused with is how to get things into and out of the while statement. What I am aiming for is my entire code to execute initially with the number 2.5 “translate”… this will return a curve length “actualLength” that I want to compare with my “targetLength”. If it is less than the target length I want to slightly increase “translate” and re-execute the rest of my script with this new value. This will change the geometry and thus “actualLength”, I want to continue to do this until “actualLength” = “targetLength”.

Hopefully that is clear. Do I need to embedded some of these functions within the while loop statement? Ideally I wouldn’t do this as it would make my code difficult to change in the future since it would all need to edited within a lengthy code block.

To make a counter as you have recommended do I just as an “or” to the parenthesized conditions for the while loop?

If I understand correctly you want to start with translate = 2.5;
You want to increment translate by increment (steps of 0.1) with each loop until it equals actualLength and then you want to terminate the loop and return whatever the value of translate is?

If that is the case then your while loop conditional will always return false and loop forever as actualLength will never equal translate if you increment by 0.1. You therefore need to either evaluate within a range of actualLength or round it to the nearest 0.1 so your conditional isn’t inadvertently returning a constant.

You also need to evaluate these variables, so while(translate < targetLength)

1 Like