Python Help! Stuck with one rectangle...trying to create offsets

Hi everyone,

Not getting any errors but I’ve tried both + and * and still not getting the nesting square effect.

Essentially I am trying to recreate this artwork.

Can someone tell me what’s missing? Is it just my line 31?

Much appreciated!

If rect is a list, you need to “append” any rectangle to it at line 31.

Hi @tamsamantha06,

I attempted practically exactly the same thing about a year ago in Python. :slight_smile:

My goal was then to animate Vera Molnár’s artwork.

Here’s my script, if you’re interested:

import Rhino.Geometry as rg
from ghpythonlib import treehelpers
import random
import math

def construct_square_grid(plane, size, extendx, extendy, padding=0):
    """Creates a two-dimensional grid of square cells.
      plane (Rhino.Geometry.Plane): Base plane for the grid
      size (int|float): Size of the grid cells
      extendx (int): Number of grid cells in the base plane x-direction
      extendy (int): Number of grid cells in the base plane y-direction
      padding (int|float): Optional padding between the grid cells, 
        by default 0, which means no padding
      A list of grid cells as polylines.
    xform = rg.Transform.PlaneToPlane(rg.Plane.WorldXY, plane)
    grid = []
    for y in range(extendy):
        for x in range(extendx):
            pt1 = rg.Point3d(
                    x * size + padding * x, 
                    y * size + padding * y,
            pt2 = rg.Point3d(
                    (x + 1) * size + padding * x, 
                    (y + 1) * size + padding * y,
            rectangle = rg.Rectangle3d(rg.Plane.WorldXY, pt1, pt2)
    return grid

def random_vector2d(plane=rg.Plane.WorldXY, scale=1.0):
    """Returns a random, two-dimensional vector that can optionally
        be scaled to a desired magnitude. If no scale is provided
        a unit vector of magnitude 1.0 is returned."""
    theta = random.uniform(0, 2 * math.pi)
    x = math.cos(theta)
    y = math.sin(theta)
    vec = rg.Vector3d(x, y, 0)
    if scale != 1.0:
        vec *= scale
    xform = rg.Transform.PlaneToPlane(rg.Plane.WorldXY, plane)
    return vec

def distort(polyline_crv, dmin=0, dmax=1):
    """Randomly distorts a polyline curve.
      polyline_crv (Rhino.Geometry.PolylineCurve): Polyline Curve to distort
      dmin (int|float): Optional minimum distortion radius, by default 0.0
      dmax (int|float): Optional maximum distortion radius, by default 1.0
      The distorted polyline curve on success, otherwise the initial curve.
    if not isinstance(polyline_crv, rg.PolylineCurve):
        return polyline_crv
    rc, plane = polyline_crv.TryGetPlane()
    if not rc:
        return polyline_crv
    pline = polyline_crv.ToPolyline()
    if pline is None:
        return polyline_crv
    for i in range(pline.Count-1):
        rscale = random.uniform(dmin, dmax)
        pline[i] += random_vector2d(plane, rscale)
    if polyline_crv.IsClosed:
        pline[pline.Count-1] = pline[0]

    return pline.ToPolylineCurve()

# ------------------------------------------------------------------------------   
if __name__ == "__main__":
    if Seed is not None:
    grid = construct_square_grid(Plane, Size, ExtendX, ExtendY, Padding)

    transformations = []

    for cell in grid:
        prev_crv = cell.ToPolylineCurve()
        sub_cells = [prev_crv]
        for i in range(Count-1):
            offset_crv = prev_crv.Offset(Plane, -Padding, 0.01, 0)
            if offset_crv is not None:
                offset_crv = offset_crv[0]
                if not isinstance(offset_crv, rg.PolylineCurve):
                if not offset_crv.IsClosed or offset_crv.PointCount != 5:
                prev_crv = offset_crv
                if i == Index:
                    offset_crv = distort(offset_crv, Padding, Padding * 2)

    # Outputs
    Curves = treehelpers.list_to_tree(transformations) (11.2 KB)


Thanks for your response! While I have figured it out it’s always nice to see the different approaches! Really appreciate you sharing this! Cheers :smiley:

Noted! Thank you Tom