Nested functions/simplify dynamo file

Hello,

I have jumped into Dynamo as I want to start learning to programme and get familiar with some of the tools out there for Architecture and design.

I draw these series of sketches normally by hand where you start off with a line and draw the next line following it almost as perfectly as you can, by the end of the drawing it undulates in a natural flow. I have started to try and programme this (correct use of the word?). Somewhat succesfully

The logic followed here is that I draw a spline with specific x integer values, then make the y values vary between 0-1 and create a spline from this. The next line then takes the previous y values and adds a random value between 0-1.

Are there any suggestions on how I can simplify and improve this so I can make the x a slider and the number of splines a slider also?

Second image shown as I could only post one image so far

image

try node to code and enter the wonderfull world of designscript.
node to code works like this: select a couple of nodes and rightclick.
use node to code and the group of nodes changes into a codeblock in designscript that does the same thing. study the result and get your way around it
in the end you will learn to use it just like so in this thread

2 Likes

Hi,

Apologies i’m not at a PC right now so I can’t give a complete reply.

Marcel is right, another technique you might find useful is to write a function in a code block and repeatedly call it with different values.

Perhaps take the node to code and use that to make your function (or to make a custom node)…

I believe @JacobSmall is an expert at this… here’s one of his examples…

Lunchbox

Hope that’s useful.

Mark

Edit: information on design script http://designscript.io/DesignScript_user_manual_0.1.pdf

2 Likes

@T.Sullivan If you find yourself repeating too many nodes or steps, you most likely aren’t going about composing your workflow properly.

Below is a script that probably does what you set out to do. However, if you’ve only started exploring Dynamo, please treat it only as one possible approach, while you explore other alternate approaches.

def abc(a,b)
{
	ab = [Imperative]
	{
		y = [][];
		c = 0;
		d = 0;
		e = List.OfRepeatedItem(0,b);
		while (c<a)
		{
			f = Math.RandomList(b);
			while (d<b)
			{
				y[c][d] = f[d]+e[d];
				e[d] = y[c][d];
				d = d + 1;
			}
			c = c + 1;
			d = 0;
		}
		return = y;
	}
	e = Point.ByCoordinates((0..5..#a),ab<1>);
	f = NurbsCurve.ByControlPoints(e,3);
	return f;
};
4 Likes

Hi @Vikram_Subbaiah,

That’s awesome, I’ve tried to understand how it works, hopefully it’s ok!

Cheers,

Mark

  # definition with 2 inputs  
  def abc(a,b)
    {
        # imperative function for looping (as opposed to data flow function)
    	ab = [Imperative]
    	{
                # y is 2 variables (the first will be a random number, the second the list count (which is added sequentially), these are added to create random sequential y point values)
    		y = [][];
                #c & d are integers
    		c = 0;
    		d = 0;
                # repeat 0, b number of times 
    		e = List.OfRepeatedItem(0,b);
               #create a while loop which runs ‘a’ number of times 
    		while (c<a)
    		{
                        #create ‘b’ number of random doubles between 0 to 1
    			f = Math.RandomList(b);
                        #create a while loop which runs ‘b’ number of times (it’s a sub loop, ie. it runs b**a times)
    			while (d<b)
    			{
                                #state y’s variables in terms of b
                                #c = f(random ‘b’) at number d
                                #d = e(repeated ‘b’) at number d
                                # add random ‘b’ and repeated ‘b’
    				y[c][d] = f[d]+e[d];
                                #update so that the next run adds the previous y position
    				e[d] = y[c][d];
                                #add 1 to d, so that eventually it equals b and stops the loop
    				d = d + 1;
    			}
                         #add 1 to c, so that eventually it equals a and stops the loop
    			c = c + 1;
                        #reset d to 0 so that it’s clear for the next run of sub list looping
    			d = 0;
    		}
    		return = y;
    	}
         #create points with x coords up to 5 stepped by ‘a’ amount and y coords which use the ab function
#ab contains nested loops, <1> makes the whole nested list be iterated over by each main list value sequentially
    	e = Point.ByCoordinates((0..5..#a),ab<1>);
        #create curve from points with degree 3
    	f = NurbsCurve.ByControlPoints(e,3);
    	return f;
    };
1 Like

Perfect thanks all, I am just out at the moment so will have a read this afternoon!!

Thanks Vikram - very helpful

1 Like

Thanks Mark, I think I will have a look at the DesignScript manual first as I need to get my head around the logic of this

1 Like

A simpler approach (the script below should serve as a guide to create the same quite easily with a few nodes)…

rndLst1 = Math.RandomList(xCount*yCount);
rndLst2 = List.Chop(rndLst1,yCount);
rndLst3 = List.TakeItems(rndLst2<1>,1..yCount);
rndLst4 = Math.Sum(rndLst3);
nrbCrv1 = NurbsCurve.ByControlPoints(List.Transpose
(Point.ByCoordinates((0..5..#xCount),rndLst4)),3);

nrbPrl

5 Likes