Hello,
I’m attempting to update some of my IronPython components to the new script component and Python 3 language and I’m stuck on the event handlers and I don’t understand what it’s expecting in terms of arguments.
I found this template code related to DrawWires:
"""Grasshopper Script Instance (With Draw Methods)"""
import System
import Rhino
import Grasshopper
import rhinoscriptsyntax as rs
class MyComponent(Grasshopper.Kernel.GH_ScriptInstance):
def RunScript(self, x, y):
return x + y
@property
def ClippingBox(self):
return Rhino.Geometry.BoundingBox.Empty
def DrawViewportWires(self, args):
pass
def DrawViewportMeshes(self, args):
pass
Here’s my working IronPython code:
from ghpythonlib.componentbase import executingcomponent as component
import System
import Rhino as rh
import Rhino.Geometry as rg
class MyComponent(component):
def RunScript(self, P, F, S, D, V):
if S == None: S = 20
if D == None: D = True
if V == None: V = False
self.F = F
self.P = P
self.D = D # Store the value of D as an instance variable
if self.F:
bitmap = System.Drawing.Bitmap.FromFile(F)
self.B = rh.Display.DisplayBitmap(bitmap)
self.S = S
self.size = V
if self.P and len(P) > 0:
bb = rg.BoundingBox(P)
self.bb = bb
else:
self.bb = rh.Geometry.BoundingBox.Empty
def DrawForeground(self,sender, arg):
if self.D: # If D is True, draw the sprite in the foreground
if self.P and self.F:
items = rh.Display.DisplayBitmapDrawList()
items.SetPoints(self.P)
arg.Display.DrawSprites(self.B, items, self.S, self.size)
def DrawViewportWires(self, arg):
if not self.D: # If D is False, draw the sprite as before using DrawViewportWires
if self.P and self.F:
items = rh.Display.DisplayBitmapDrawList()
items.SetPoints(self.P)
arg.Display.DrawSprites(self.B, items, self.S, self.size)
def get_ClippingBox(self):
return self.bb
def IsPreviewCapable(self):
return True
def __exit__(self):
rh.Display.DisplayPipeline.DrawForeground -= self.DrawForeground
def __enter__(self):
rh.Display.DisplayPipeline.DrawForeground += self.DrawForeground
And my attempt at updating to Python 3:
#from ghpythonlib.componentbase import executingcomponent as component
import System
import Rhino as rh
import Rhino.Geometry as rg
class MyComponent(Grasshopper.Kernel.GH_ScriptInstance):
def RunScript(self, P, F, S, D, V):
if S == None: S = 20
if D == None: D = True
if V == None: V = False
self.F = F
self.P = P
self.D = D # Store the value of D as an instance variable
if self.F:
bitmap = System.Drawing.Bitmap.FromFile(F)
self.B = rh.Display.DisplayBitmap(bitmap)
self.S = S
self.size = V
if self.P and len(P) > 0:
bb = rg.BoundingBox(P)
self.bb = bb
else:
self.bb = rh.Geometry.BoundingBox.Empty
@property
def DrawForeground(self, sender, args):
if self.D: # If D is True, draw the sprite in the foreground
if self.P and self.F:
items = rh.Display.DisplayBitmapDrawList()
items.SetPoints(self.P)
args.Display.DrawSprites(self.B, items, self.S, self.size)
pass
def DrawViewportWires(self, args):
if not self.D: # If D is False, draw the sprite as before using DrawViewportWires
if self.P and self.F:
items = rh.Display.DisplayBitmapDrawList()
items.SetPoints(self.P)
args.Display.DrawSprites(self.B, items, self.S, self.size)
pass
def get_ClippingBox(self):
return self.bb
def IsPreviewCapable(self):
return True
def __exit__(self):
rh.Display.DisplayPipeline.DrawForeground -= self.DrawForeground
def __enter__(self):
rh.Display.DisplayPipeline.DrawForeground += self.DrawForeground
The error I can’t solve:
1. Error running script: DrawForeground() missing 2 required positional arguments: 'sender' and 'args'
Thank you for your help!