Trying to set UserText to a grasshopper model object (GH_ModelObject)

If the goal is to simply filter objects out by layer, then the easiest way to probably do that would be to use the Query Model Objects component and use the Layer input to specify the name of the layer that you want to filter for. Then, only the objects on that layer will be returned. If you right-click on that component and choose Show All Parameters, it will then also split the data out by type into different outputs. But, that’s not really the question here. So, let me explain how some of the other components work.

Let’s start with the simplest components (Group By Attribute, and Order By Attribute). In this example, I have the following model with lots of different types of objects and these are placed on different Layers (Layers 1-4).

We can use the Query Model Objects component to reference all of the Rhino geometry into Grasshopper. Then, we connect the Objects output to the Group By Attribute component. We can right-click on the Key input and look at the Tree view at all of the different attributes we can use as grouping criteria in the component. If we want to break objects out by Layer, we simply select the Layer root node in the tree view and the output will be broken out into different branches where each branch corresponds to a different Layer.

You’ll notice that the output isn’t sorted… but the branching does correspond to the list of values returned in the Values output. This output gives you all the unique values that it found for that grouping criteria. So, if you chose Layer, then you’ll get a list of unique Layers.

If you want to the branches to be sorted, then we simply need to add an Order By Attribute component between the Query Model Objects and the Group By Attribute components. Again, we’ll use the Layer as the sorting criteria and it will then return a list that is ordered according to the Layer name.

So, with these two components, you can begin to break down your data in really interesting ways. You can expand the Layer root node and select Display Color for example. Now, it will use the Layer color as the criteria to use for grouping on different branches. If you had several layers which had the color “Red” for example (255,0,0), then all of those objects would be placed on the same branch.

Or, let’s say you wanted all of the objects broken down by Material name. Simply expand the Material node and select the Name attribute, and now it will group objects together that have the same material name. Simple. Want to group objects based on their type (ie. Point, Curve, Brep, etc.)? Simply choose the Type Name as the search key and you’re done.

Now, let’s consider the concept of Filtering objects by a rule or criteria. I’ve now got a different model open. This one contains a bunch of boxes (on different layers), each of which have a number of different User Text entries associated with it. These keys include: Color, Cost, ID, Manufacturer, and Volume (which is derived from a formula). Let’s use some of these User Text values as filtering criteria.

The first thing we need to do is add a Filter By Rule component to our canvas and connect the Query Model Objects output to the Content input. Next, we need to tell the Filter By Rule component that we want to filter by a User Text entry instead of a data type attribute. To do this, right-click on the Filter By Rule component and choose By User Text Key in the menu. Note: you can do this same procedure in the Group By Attribute and Order By Attribute components if you want to use a User Key as the search criteria.

Now, right-click on the Key input and select one of the User Text keys to use as our filtering criteria. Let’s say we only want to return the “Orange” objects. So, we select the User Text key named Color as our filtering criteria.

Next we need to define a rule that describes how we want to filter the objects. In this case, we want a rule that says "Return any object which has a User Text value which is equal to “Orange”. So, we’re going to use the Equality rule component to achieve this. Notice that the Filter By Rule component only returns objects that match that rule.

Now, let’s say we wanted to filter the objects so that we only return the objects who have a volume larger than some value. We need to change the Key input so that it’s using the Volume numeric value as the filtering key (instead of color). And then we can define a new rule using the Larger Than Rule component. We can use a slider to dynamically control the filtering. In the image below, I’m selecting objects whose volume is greater than 630. The same works with the Smaller Than Rule component.

What if we wanted to define a compound rule where we only wanted objects whose volume was greater than 150 but also smaller than 400. We can use the Conditional And Operator to combine rules together. So, we first define the Larger Than and Smaller Than rules and combine them together so that our search criteria will only return the values we want.

Hopefully this helps explain the basics of how these components work… but there’s a lot you can do with these that I didn’t necessarily cover here. Feel free to ask any follow up questions if you have any.

3 Likes