I’m working on a grasshopper script where we are listening to a signal (via an event listener), and that signal will cause a geometry to be shown on the canvas. I want to avoid expiring a component everytime and use a preview component, but rather just subscribe to the signal and manually add the geometry to the display pipeline.
But I’m very much in doubt how.
Below is a code snippet where I show a sphere, but it, as the function also suggests, is drawn on top of everything. How can I ensure that it is “part of the scene”?
import System
import Rhino
import Grasshopper
import rhinoscriptsyntax as rs
import Rhino
import scriptcontext as sc
import System.Drawing as sd
class MyComponent(Grasshopper.Kernel.GH_ScriptInstance):
def RunScript(self, pt: Rhino.Geometry.Point3d, radius, show, clear):
# Create the conduit once and stash it so it survives across runs
if "sphere_conduit" not in sc.sticky:
sc.sticky["sphere_conduit"] = PersistentSphereConduit()
conduit = sc.sticky["sphere_conduit"]
if clear:
conduit.Enabled = False
sc.doc.Views.Redraw()
return "Cleared"
if show:
conduit.update_sphere(pt, radius)
return "Drawing at ({:.1f}, {:.1f}, {:.1f})".format(pt.X, pt.Y, pt.Z)
return "Idle"
class PersistentSphereConduit(Rhino.Display.DisplayConduit):
def __init__(self, colour=sd.Color.CornflowerBlue):
super(PersistentSphereConduit, self).__init__()
self._mesh = None
self._mat = Rhino.Display.DisplayMaterial(colour, 0.8)
def update_sphere(self, centre, radius=1.0):
sph = Rhino.Geometry.Sphere(centre, radius)
self._mesh = Rhino.Geometry.Mesh.CreateFromSphere(sph, 16, 16)
self.Enabled = True
sc.doc.Views.Redraw()
def CalculateBoundingBox(self, e):
if self._mesh:
e.IncludeBoundingBox(self._mesh.GetBoundingBox(True))
def DrawForeground(self, e):
if self._mesh:
e.Display.DrawMeshShaded(self._mesh, self._mat)