import rhinoscriptsyntax as rs
import System.Drawing as sd
import Rhino.Geometry as rg
import Rhino.DocObjects as rd
import scriptcontext as sc
import Rhino
spaceX = int(spaceX)
spaceY = int(spaceY)
spaceZ = int(spaceZ)
min= int(min)
max = int(max)
# Get the input point from Grasshopper
input_point = p # "x" is the default input parameter name for the first input in Grasshopper
if input_point:
# Convert the Grasshopper point to a Rhino point
rhino_point = rg.Point3d(input_point.X, input_point.Y, input_point.Z)
# Deconstruct the Rhino point into X, Y, and Z components
px = rhino_point.X
py = rhino_point.Y
pz = rhino_point.Z
geo = []
colour = []
print("Length of geo before loop:", len(geo))
print("Length of colour before loop:", len(colour))
for x in range(min, max, spaceX):
for y in range(min, max, spaceY):
pt1 = rg.Point3d(x + px, y + py, pz)
pt2 = rg.Point3d(x + px + spaceX, y + py, pz)
pt3 = rg.Point3d(x + px, y + py + spaceY, pz)
plane = rs.PlaneFromPoints(pt1, pt2, pt3)
dist = rs.Distance(pt, pt1)
geo.append(rg.Box(plane, rg.Interval(spaceX / 2 * -gap, spaceX / 2 * gap), rg.Interval(spaceY / 2 * -gap, spaceY / 2 * gap), rg.Interval(dist / 2, spaceZ / 2 * -1)))
colour.append(sd.Color.FromArgb(x / max * 255, y / max * 255, 150))
print("Length of geo after loop:", len(geo))
print("Length of colour after loop:", len(colour))
def baker():
# Store the current Grasshopper document
ghdoc = sc.doc
# Switch to Rhino document
sc.doc = Rhino.RhinoDoc.ActiveDoc
# Create and add geometry
for k in range(len(geo)):
brep = rg.Brep.CreateFromBox(geo[k])
attr = rd.ObjectAttributes()
attr.ColorSource = rd.ObjectColorSource.ColorFromObject
attr.ObjectColor = colour[k]
rebrep = sc.doc.Objects.AddBrep(brep, attr)
# Switch back to Grasshopper document
sc.doc = ghdoc
# Call the baker function if bake is True
if bake:
baker()
Hi @lishiqi,
Your boxes are invalid Rhino geometry, because their Z coordinates are decreasing: https://developer.rhino3d.com/api/rhinocommon/rhino.geometry.box/isvalid. Thus the brep = rg.Brep.CreateFromBox(geo[k])
line returns empty geometry.
If you change the line where the box is created to:
geo.append(rg.Box(plane, rg.Interval(spaceX / 2 * -gap, spaceX / 2 * gap), rg.Interval(spaceY / 2 * -gap, spaceY / 2 * gap), rg.Interval(dist / 2, spaceZ / 2 * -1)))
the boxes get added to the document.
DI-164017_Baking boxes.gh (6.8 KB)
–Pierre