Hello everyone, I am a newbie to gh with python. I am currently learning from a youtube and encountered a problem in Baking even I have followed the video. Are there any kind people share your knowledge on it? Many thanks.
import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
from Grasshopper.Kernel import GH_RuntimeMessageLevel as rml
import scriptcontext as sc
import System.Drawing as sd
import Rhino.DocObjects as rd
geo = []
colours =[]
for x in range(int(eX)):
for y in range(int(eY)):
pt1 = rg.Point3d(x*eX,y*eY,0)
pt2 = rg.Point3d(x*eX+1,y*eY,0)
pt3 = rg.Point3d(x*eX,y*eY+1,0)
plane = rs.PlaneFromPoints(pt1,pt2,pt3)
dis = rs.Distance(pt,pt1)
geo.append(rg.Box(plane,rg.Interval(sX/2*-0.9,sX/2*0.9),rg.Interval(sY/2*-0.9,sY/2*0.9),rg.Interval(sZ/2*-0.9,dis/2*0.9)))
colours.append(sd.Color.FromArgb(x/eX*250,y/eY*250,150))
def baker():
for i in range(len(geo)):
brep = rg.Brep.CreateFromBox(geo[i])
attr = rd.ObjectAttributes()
attr.ColorSource=rd.ObjectColorSource.ColorFromObject
attr.ObjectColor = colours[i]
sc.doc.Objects.AddBrep(brep, attr)
if bake:
baker()
You have to add this before calling sc.doc.Objects.AddBrep() (or rs.AddXXX() which you may see in other places) to let the Grasshopper switch the context to your currently active doc.
import Rhino
sc.doc = Rhino.RhinoDoc.ActiveDoc
For example:
import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
from Grasshopper.Kernel import GH_RuntimeMessageLevel as rml
import scriptcontext as sc
import System.Drawing as sd
import Rhino.DocObjects as rd
import Rhino
sc.doc = Rhino.RhinoDoc.ActiveDoc
geo = []
colours =[]
for x in range(int(eX)):
for y in range(int(eY)):
pt1 = rg.Point3d(x*eX,y*eY,0)
pt2 = rg.Point3d(x*eX+1,y*eY,0)
pt3 = rg.Point3d(x*eX,y*eY+1,0)
plane = rs.PlaneFromPoints(pt1,pt2,pt3)
dis = rs.Distance(pt,pt1)
geo.append(rg.Box(plane,rg.Interval(sX/2*-0.9,sX/2*0.9),rg.Interval(sY/2*-0.9,sY/2*0.9),rg.Interval(sZ/2*-0.9,dis/2*0.9)))
colours.append(sd.Color.FromArgb(x/eX*250,y/eY*250,150))
def baker():
for i in range(len(geo)):
brep = rg.Brep.CreateFromBox(geo[i])
attr = rd.ObjectAttributes()
attr.ColorSource=rd.ObjectColorSource.ColorFromObject
attr.ObjectColor = colours[i]
sc.doc.Objects.AddBrep(brep, attr)
if bake:
baker()