Functions
Last updated
Last updated
Functions can be created in a code block and recalled elsewhere in a Dynamo definition. This creates another layer of control in a parametric file, and can be viewed as a text-based version of a custom node. In this case, the "parent" code block is readily accessible and can be located anywhere on the graph. No wires needed!
The first line has the key word “def”, then the function name, then the names of inputs in parentheses. Braces define the body of the function. Return a value with “return =”. Code Blocks that define a function do not have input or output ports because they are called from other Code Blocks.
Call the function with another Code Block in the same file by giving the name and the same number of arguments. It works just like the out-of-the-box nodes in your library.
Download the example file by clicking on the link below.
A full list of example files can be found in the Appendix.
In this exercise, we will make a generic definition that will create spheres from an input list of points. The radius of these spheres are driven by the Z property of each point.
Let's begin with a number range of ten values spanning from 0 to 100. Plug these into a Point.ByCoordinates nodes to create a diagonal line.
Create a Code Block and introduce our definition.
Use these lines of code:
The inputPt is the name we've given to represent the points that will drive the function. As of now, the function isn't doing anything, but we'll build up this function in the steps to come.
Adding to the Code Block function, we place a comment and a sphereRadius variable which queries the Z position of each point. Remember, inputPt.Z does not need parenetheses as a method. This is a query of an existing element's properties, so no inputs are necessary:
Now, let's recall the function we've created in another Code Block. If we double-click on the canvas to create a new code block, and type in sphereB, we notice that Dynamo suggest the sphereByZ function that we've defined. Your function has been added to the intellisense library! Pretty cool.
Now we call the function and create a variable called Pt to plug in the points created in the earlier steps:
We notice from the output that we have all null values. Why is this? When we defined the function, we are calculating the sphereRadius variable, but we did not define what the function should return as an output. We can fix this in the next step.
An important step, we need to define the output of the function by adding the line
return = sphereRadius;
to the sphereByZ function.Now we see that the output of the Code Block gives us the Z coordinates of each point.
Let's create actual spheres now by editing the Parent function.
We first define a sphere with the line of code:
sphere=Sphere.ByCenterPointRadius(inputPt,sphereRadius);
Next, we change the return value to be the sphere instead of the sphereRadius:
return = sphere;
This gives us some giant spheres in our Dynamo preview!
1. To temper the size of these spheres, let's update the sphereRadius value by adding a divider:
sphereRadius = inputPt.Z/20;
Now we can see the separate spheres and start to make sense of the relationship between radius and Z value.
On the Point.ByCoordinates node, by changing the lacing from Shortest List to Cross Product, we create a grid of points. The sphereByZ function is still in full effect, so the points all create spheres with radii based on Z values.
And just to test the waters, we plug the original list of numbers into the X input for Point.ByCoordinates. We now have a cube of spheres.
Note: if this takes a long time to calculate on your computer, try to change #10 to something like #5.
Remember, the sphereByZ function we've created is a generic function, so we can recall the helix from an earlier lesson and apply the function to it.
One final step: let's drive the radius ratio with a user defined parameter. To do this, we need to create a new input for the function and also replace the 20 divider with a parameter.
Update the sphereByZ definition to:
Update the children Code Block by adding a ratio variable to the input:
sphereByZ(Pt,ratio);
Plug a slider into the newly created Code Block input and vary the size of the radii based on the radius ratio.