Читать книгу Grasshopper: Visual Scripting for Rhinoceros 3D - David Bachman - Страница 13
ОглавлениеThe Weave and Dispatch Components
As we have already seen, most Grasshopper scripts contain components that act on every element in a list. However, it is often the case that you will want different things to happen to different elements of a list. To accomplish this, Grasshopper contains many special components to separate lists and put them back together. Two of the most common are the Weave and Dispatch components (both in Set tab, List panel). We illustrate the use of both of these components with the script of Figure 6.1, which is a further modification of the paneling script from the last chapter.
The Brep component shown here is set to reference the panel depicted in Figure 6.2. The U and V inputs to the Divide Domain2 component are set to 16 and 9, respectively, creating 144 subdomains of the cylinder. The N input of the Duplicate Data component (Set tab, Sequence panel), fed by the Brep component, is set to 144. This creates a list of 144 copies of the referenced surface, one for each subdomain. This list is immediately passed to the Dispatch component. By default this component creates two lists. At the A output is a list of every even numbered list element of the input list. At the B output is a list consisting of the odd numbered inputs.
FIGURE 6.1. Advanced paneling with the Weave and Dispatch components.
FIGURE 6.2. The object referenced by the Brep component in the script of Figure 6.1.
Every element of the B list is now sent to a Mirror component (Transform tab, Euclidean panel), which creates the mirror image object across the XY plane. Finally, both the list at the A output of the Dispatch component and the output of the Mirror component are sent to a Weave component. This has the opposite effect as the Dispatch component: by default it creates a new list whose even numbered elements come from the 0 input, and whose odd elements come from the 1 input.
The last thing of note before the Surface Morph component is that the entire list of surfaces is passed to a Bounding Box component. Notice in the figure that this component has been set to act in “Union Box” mode (right-click to see this option). This creates one bounding box that contains everything coming in to the component, rather than separate bounding boxes for each input geometry.
The script of Figure 6.1 produces the object depicted in Figure 6.3. Here we have colored two individual panel components so you can see that one comes from the mirror image of the other.
In the previous example we used the Dispatch and Weave components to perform different actions on every other element of a list. A different use of Dispatch and Weave is to create a conditional “if... then... else...” statement. This is accomplished by evaluating some boolean (e.g., true/false) condition for each list element, and feeding the resulting list of true/false values to the P inputs.
We illustrate this idea in the Grasshopper script depicted in Figure 6.4. This script has the same effect as the Offset component (Curve tab, Util panel). However, a slight modification of this script will produce variable offset curves, which are not possible with the Offset component.
The script begins with a Curve component, set to reference some closed curve created in Rhino’s XY plane. As discussed in previous chapters, we obtain 100 values of the curve’s parameter with a Divide Curve component, with the N input set to 100. We then use a Curvature component (Curve tab, Analysis panel) to obtain a vector K that is perpendicular (normal) to the curve at each curve point P, corresponding to each input parameter value t. We then rescale each of these normal vectors to have length one with a Unit Vector component (Vector tab, Vector panel). Adding these vectors to the curve point at which they are based then produces a set of points that are exactly one unit away from the curve. These points are depicted in Figure 6.5.
You can immediately see that there are many problems with these points. Some are inside the curve and some are outside. If we were to create a new interpolated curve through these points it would cross the original curve in several places. This is clearly not the desired offset.
FIGURE 6.3. The colored surfaces show the two different panels used to create this object.
FIGURE 6.4. A curve offsetting script to illustrate the use of Dispatch and Weave components to create a conditional action.
FIGURE 6.5. The points shown here are the result of offsetting curve points along a unit normal vector given by the Curvature component.
To remedy this we have added more to the script to test whether or not each point is inside the curve, and to do something to change it if it is. The Point In Curve component (Curve tab, Analysis panel) is what performs the test. This component will output a 0 for each point that is inside the curve, and a 2 for those points that are outside. By feeding this to the A input of an Equality component (Math tab, Operators panel), with the B input set to 0, we get a list of true/false values.
The true/false values are now sent to the P input of a Dispatch component, with the L input receiving the list of unit normal vectors. The Dispatch component will now separate the list of unit normal vectors into two separate lists, depending on whether the point obtained earlier was inside the curve or outside. For those points that are outside the curve we do nothing. For points inside the curve we negate the vector with a Negative component (Math tab, Operators panel), flipping its direction.
Finally, all of the vectors have to be re-assembled into one list again. The important part is that they must end up in the same order that they started. This is precisely what the Weave component will do when we send the list of true/false values obtained earlier to the P input. Adding these modified unit normal vectors to the curve points now produces a set of points that are all on the outside of the curve. The final step is to create an interpolated curve through these points with an Interpolate component (Curve tab, Spline panel). The result is depicted in Figure 6.6.
FIGURE 6.6. The offset curve produced by the script of Figure 6.4.
Simple Recursion with the Shift List Component
Grasshopper has no native mechanism for creating loops, although there are multiple plug-ins that add this functionality. At first glance, this may seem to eliminate the possibility of recursive definitions. To some extent this is true, but many recursive operations can be accomplished through list shifting. In the next example, shown in Figure 6.7, we present a script that begins with a list of points, and creates lines connecting each point to its predecessor in the list, a simple kind of recursion.