Blender, Python and Your Custom Data Part 2: using custom attributes in Geometry Nodes

Ben de Vries
6 min readJun 1, 2023

--

In this second part of our three-part blog series, we will continue using scientific data to make a 3D visualization in Blender 3.5.0. In the first blog of this series, we loaded data into Blender attributes using the Python API. This second part will show you how to use your custom attributes within Geometry Nodes to create, delete and alter geometry.

Figure 1: the scene we will make in this three part blog series

Recap of what we did in the first blog

In the previous blog we used particle collision data from the CERN accelerator, and we ended up with many vertices in a cube-like arrangement (see Fig. 3). Furthermore, every vertex has a float value associated with it through the attribute myAttributeFloat (see the spreadsheet in Fig. 3). This value represents the energy recorded by the detector belonging to the vertex.

Figure 2: animated version of Fig. 1

If you are totally new to Geometry Nodes

This blog is not meant as an introduction to Geometry Nodes, but is to show an example of how you can use Geometry Nodes to manipulate geometry and your own custom data. If you are new to Geometry Nodes have a look at the first free video of this course from Blender Studio. That will give you enough background for now.

Figure 3: our starting point for this blog. To see how we got here, please read part 1 in this blog series. Also on the left you see the very useful Spreadsheet editor included in the Geometry Nodes workspace.

Three step plan towards the animation in Figure 2

In three steps we will create the basis for the animation in Fig. 2.

  1. We remove detectors/vertices that did not detect anything.
  2. We replace the detectors/vertices with a cube geometry. We do this so that we can shade them in the next blog.
  3. We want to be able to make detectors (dis)appear so that we can make the animation as in Fig. 2.
Figure 4: first version of the Geometry Node modifier we are building. Here we use the attribute ‘myAttributeFloat’ to remove detectors that did not measure any energy from particles.

Step 1: removing non-detections.

Many vertices seen in Fig. 3 represent detectors that detected no energy from a particle. We want to remove those vertices that have an myAttributeFloat value lower than a certain threshold. We can remove geometry using the Delete Geometry node (see Fig. 4). In this case we want to remove vertices, so we set it to delete Points. We now need to feed in a list of Booleans into the Selection socket, which determines what gets deleted.

Figure 5: the result from the Geometry Node seen in Fig. 4.

To get access to the attribute myAttributeFloat we use a Named Attribute node and fill in the attribute name (see Fig. 4). Next we want to compare the myAttributeFloat value to a threshold and for this we use a Compare node and set it to Less Than and feed the attribute into socket A and set socket B to a threshold value of 0.1 (see Fig. 4). If we now connect the Delete Geometry node to the Group Output node we see the result as in Fig. 5.

Figure 6: second version of the Geometry node we are building. We now make instances of cubes from the vertices we had.

Step 2: replace vertices with cubes.

To be able to shade every detector, we generate a geometry, in this case a cube, at the location of every vertex/detector. We do this by creating an instance of a cube at every vertex using the Instance on Points node (see Fig. 6). But because this node requires a point cloud as input we first convert our vertices into points using the Mesh to Points node (see Fig. 6). We now add a Cube node which we connect to the Instance socket. The result now looks like that shown in Fig. 7.

Figure 7: here is the result from the Geometry Node in Fig. 6.

Step 3: make detectors appear and disappear.

To make the animation seen in Fig. 2, we also need to make the cubes appear and disappear. We will make them appear from the center outwards (in the radial direction). Thus, we need to be able to cut away detectors located at a radius larger than a certain threshold. And instead of hardcoding this threshold, we will make a new Geometry Node input for the threshold, which will make it possible to animate the threshold.

Figure 8: version three of the Geometry Node we are making. We can now also remove detectors at locations on the x axis larger and smaller than a certain threshold.

We can access the position of the detectors by adding a Position node (see Fig. 8). We use the length of the points using the Vector Math node and set it to Length. We will feed it into a Compare node (set to Greater than or Equal) which will be fed into a new Delete Geometry node together with the threshold (See Fig. 8).

Figure 9: result from the Geometry Node in Fig. 8.

We now need to implement the threshold that determines which detectors are removed. We will not hard code this threshold like we did for the other Compare and Delete Geometry nodes. We will make a new Group Input node instead and we will use the socket that has no name. If we connect the unnamed socket to the B socket of the Compare node, we will see the unnamed socket is given a default name (for example B).

The new input also shows up in the Modifier Properties tab in the Properties editor, at the right side of the Blender application (see Fig. 8). You can also press the “n” key in the Geometry Node Editor window to open the Sidebar (or go to the View tab and tick the Sidebar option) and then navigate to the Group tab in the Sidebar to see the list of inputs and outputs of this Geometry Node modifier (also see Fig. 8). Here you will see (and have the possibility to rename) the new input we just created. I named it “Cutoff for animation”. You can see an example result in Fig. 9.

In the next blog we will use the fact that you can animate the inputs of modifiers. We can click the small dot next to the value of “Cutoff for animation” in the Modifier Properties tab to start animating the threshold. For now, you can play with the value of “Cutoff for animation” to see what it looks like.

A last addition to the Geometry Node we made

In the next blog we want to give all the detectors/cubes we just made a different color depending on the value of the myAttributeFloat attribute. But now all the cubes are an instance of one and the same cube. We need to make every instance a real geometry and we do this using the Realize Instances node (see Fig. 10). Now this does not change how the scene looks, but it is essential for the shading we do in the next blog of this series.

Figure 10: we use Realize Instances to turn all the cube instances into real geometry data.

Final Thoughts

This has turned out to be a longer blog than I thought. It takes some work to explain all steps involved. I hope this gives you an idea of what you can do with attributes and Geometry Nodes. What I did here only touches the surface of what is possible. In the third blog of this series we will shade the geometry we just made and we will see how we can use attributes in the shading. See you then.

Cheers,
Ben

--

--