5
$\begingroup$

I am new to the Geometry Nodes and trying to use them for implementing an arithmetic progression.

I want to mimic the behavior of the following script, but I haven't been able to so far since I cannot figure out how to create for-loop-like geometry nodes (if even possible).

import bpy

rows = 5
increment = 2
x_spacing = 4
y_spacing = 4
size = 2

obj = bpy.context.active_object

for i in range(0, rows):
    if i == 0: increment = 1
    for j in range(0, i+increment):
        new_location = (
            obj.location.x + i * x_spacing,
            obj.location.y + j * y_spacing - (i-j) * x_spacing,
            obj.location.z,
        )
        bpy.ops.mesh.primitive_cube_add(size=size, location=new_location)

I could only come up with a hacky solution, but it is not what I need.

my hacky solution


(Both the script and my hacky solution with Geometry Nodes are included in the attached file)

$\endgroup$
1
  • 1
    $\begingroup$ Reproducing your code in geometry nodes can be done in B4.0 using two nested "repeat zones". Achieving the result you want would require you to elaborate more, as it seems you already achieved it? Keep in mind, the increment will always be 1 in your code. $\endgroup$ Aug 31, 2023 at 23:26

3 Answers 3

6
$\begingroup$

Well, I would say this is a useful solution:

enter image description here

The principle simply relies on Duplicate Elements and Accumulate Field.

First I create a mesh line. This represents the rows.

I accumulate the value for Increment per point, so that I get the values for an arithmetic progression.

With this value I duplicate the points.

Finally, I simply move them according to their Duplicate Index and instantiate the cubes.


(Blender 3.6+)

$\endgroup$
1
  • $\begingroup$ you could just use the (index + 1) × amount instead of accumulate field. $\endgroup$
    – shmuel
    Sep 8, 2023 at 12:32
5
$\begingroup$

If I've read you right, to achieve the final effect, (if not the internal logic,) you could just implement some adjusted variation of this: using the sum of X and Y coordinates as a threshold:

enter image description here

If this is what you're after:

enter image description here

$\endgroup$
4
$\begingroup$

Starting point for node group:

  • I'm sure someone else can do a cleaner job of this but I thought I'd offer this starting point for now and hopefully update it tomorrow.

Using simulation node in node group so you must ensure scene frame is equal to or greater than rows.

![](img/Arithmetic_progression_node_full.gif)

conceptually you grow one line segment each frame until the frame number is equal to the desired number of rows.

![](grow-x.png)

This in turn can be used to instance new lines that are grown along the y-axis as a function of rows-instance index.

![](grow-y.png)

re-sampling the instances on the y axis based on their lengths provides the points required to instance your final geometry mesh.

![](full.png)

also slight code change below:

import bpy

rows = 5
increment = 1
x_spacing = 4
y_spacing = 4
size = 2


if not bpy.context.active_object:
    bpy.ops.mesh.primitive_cube_add(size=size)
obj = bpy.context.active_object

for i in range(0, rows):
    inc = increment or 1
    for j in range(0, i+inc):
        new_location = (
            obj.location.x + i * x_spacing,
            obj.location.y + j * y_spacing - (i-j) * x_spacing,
            obj.location.z,
        )
        bpy.ops.mesh.primitive_cube_add(size=size, location=new_location)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .