Recursive "For Loop" using Dynamo Nodes


#1

Dynamo has the unique ability to be able to place custom nodes within themselves, creating the ability to script recursive functions without using a python node. This has a great deal of potential for advanced geometry analysis, evolutionary form-finding, and custom list operations.

 

The example below shows how to implement a simple for loop with this system. The variables are defined in a way similar to traditional scripting environment, with the mechanics of the loop defined within the custom node. The highlighted area in the second image shows where the user-defined script to be looped would be placed. In this example I have just added a simple script to increment a variable alongside the loop function, but just about anything could be placed here with more inputs as more variables are needed.

 


#2

That is really helpful. Thank you.


#3

In Dynamo, the If node performs conditional evaluation, so like any standard programming language, if the conditional test is true, then only the true branch is evaluated. The false input corresponds with an else branch in your standard programming language. In Dynamo, the entire conditional "branch" is the whole expression plugged into the true/false inputs of the If node, which would include the node directly plugged in and all nodes connected to that and so on.


(Note that if the outputs of nodes in the branch are used in other places that would require their evaluation, they will still evaluate correctly in those places.)


Because Dynamo is based on functional programming, there is usually not a need to "bracket" multiple statements, because most things can be expressed with a single statement. Dynamo has the concept of brackets, which evaluate multiple statements in order, in the form of the Perform All node. Usually this is only necessary to process side-effects (computations that have no return value, or void methods in standard programming vernacular), of which there are few in Dynamo.


#4

Thanks for the reply!



Just a question on Dynomo "theory." Normally if/then statements are bracketed one way or the other (such as end if or a parenthesis, etc.), so that the items within the bracketed statement will only be evaluated if the "if" requirements are true. In Dynamo, there is no bracketing for the if/then. So my question is, if the node is not evaluated because it is plugged into an if/then, and did not satisfy the costraints, does that mean that the return value is simply not passed through, or does it mean that the node is not evaluated at all. In other words, if the node were to, say, place an object in Revit, would that object be placed even if the if/then was not satisfied, but the return value of the object is NOT passed through? Or, is the node not even evaluated?



If the node is not evaluated, is it just THAT node, or prior nodes plugged into that node?



Does the nature of the question make sense? I'm hoping to get an understanding of how/when revit nodes are evaluated.


#5

That's correct: The If node will only evaluate the true port if the test is true. If the test is false, then the false port is evaluated, and since there is no recursive call there, the recursion ends.


#6

Basic question regarding recursion. In a traditional linear program, the individual "branches" of a recursion will stop when the counter reaches 0 (or whatever counter number is the min/max) by way of an "End" type of statement, or no further actions on the if/then that tests the counter - if the if then tests the counter as reaching it's min/ max.


In your example, how does Dynamo know how to stop? Is it simply that Test Recursion is plugged into the "True" port and not the "False" port?



Regards


#7

Hi David, Yes, that is correct. It's comforting to hear that i've followed a similar thought process to yourself - thanks.



I'm building the ref point mesh to provide a design 'playground' for the form finding as the ref points update almost instantly.



I've built the script/program to allow for the skin overlay to be flipped on once the form has settled down.



Cheers



Jason


#8

Ahh I see, you're taking the XYZs from the ref. points created through the image (I suppose those are ref. points...not actually required to place the adaptive component), slicing them in lists of 4 XYZs and using those to place the 4-point adaptive components. At least that's what I think I'd be doing!


#9

Hi David, i ended up creating two additional custom nodes, i didn't know this was possible until you prompted me to re-read this blog post - that'll learn me for only looking at the pictures :)



one node is extracting the coordinates required to position the corners of an adaptive family



the other node is loop the above to collate all the coordinates to cover the 'mesh'



it may not be the best way to do it but it works and allows for quick testing of different point meshes based on a 'seed' file that can be setup in photoshop.



then the routine can place one or multiple adaptive family types over that mesh


#10

Welcome, you did it all ;) Are you passing lines through points in that example?


#11

Thanks for the help David, i got the Custom Node working and the "loop" is now repeating a "skin" over the grid correctly. =)


#12

Thanks David,



Very helpful - i will give the custom node a whirl and post my end result on the gallery when i get it working.



Cheers


#13

The loop is what you need to encapsulate in a custom node, which is where recursion will occur. Take a look at the examples posted here and reverse engineer...the general structure is essentially the same. Instead of processing all values, you just have to evaluate the input and terminate when your required value is reached. You can make a more versatile recursive node if you make these requirements as variables - ex: Step input (instead of hardwiring it as 1) and the max value at which you want the loop to terminate. This way the node is reusable and more versatile. As to your other questions, I'm not sure if something broke in the latest build.


#14

Hi all,


I'm relatively new to Dynamo and I'm very interested in the recursive node concept.



I have a test project that i'm working on where i need to "loop" through a list to generate another list to form groupings of XYZ coordinates to place an adaptive component based on these coordinates.



I have the nodes working to create the first "set" of coordinates and now i would like to "loop" these nodes changing the "seed" value by 1 until a maximum value is reached.



If someone could help point me in the right direction of offer up an alternative approach i would be very grateful for the assistance.



sample files can be found here : http://db.tt/FFaWGno9



Also i notice that after i upgraded from version 0.5.2 to 0.5.3 the Get From List node will no longer except a list of numbers in the index field that worked perfectly fine in build 0.5.2 - Any thoughts?



Kind regards



Jason


#15

I created (I should say, "adapted") my first recursive node here as a practical example.


#16

Cool, I'm really glad to see you guys exploring recursion, it's a powerful feature of custom nodes. Here is an example of a fibonacci sequence that we were looking at earlier. http://autodeskvasari.com/forum/topics/a-simple-progressive-list-fibonacci-sequence


I'd like to put together some more examples of this kind of tool, other examples you can think of?



#17

Yeah, to be honest, It took a bit of sketching out relationships to help me understand how it needed to go together, but I'll see if I can explain the mechanics.


The "start" "end" and "step" inputs are all in place to control how many times the component loops. Each time the recursive node is called, the "step" input is added to the "start" input. If the result of that addition is less than the "end" value, the operation in the red box is called. The result of that operation is fed back into the recursive node as the new "initial" value, and the "start" + "step" result is fed back in the recursive node as the new "start" value. This continues to loop, and each time the loop happens, the "start" value continues to increment up. Once the "start" value is greater than the "end" value, the "if" node returns the "initial" value by itself without looping it through the recursive component, but at this point the "initial" value has been run through the operation in the red box many times.


In my specific example, the function loops until "start" increments up to 20 by 1, resulting in 20 loops of the operation. Each time the operation is called, 1 is added to the initial value, resulting in a final value of 20. This specific operation isn't too useful, there are much easier ways to count to 20, but the operation in the red box can be expanded to become much more complex than it is now. Things like the Fibonacci sequence and fractals are maybe more straightforward examples of things that can be created through recursion.


#18

Thanks for posting. I'm just getting my feet wet and cannot say I'm understanding your example, but am grasping the possibilities.


#19

That is right. I suppose it could have a more elegant name. This screen shot came straight from a testing file, I didn't even know if this was even possible until it was all assembled!


#20

So "TEST Recursion" is a custom node, correct?