Custom Sprite Component

Thank you both again!

I ended up combining both of your scripts to make a Custom Symbol Display component that can optionally be drawn in the foreground.

I think I’m going to eventually hardcode a library of the symbols as the base64 strings but for now and for testing the file path method works well enough as I’m still graphically iterating quite a bit.

Here’s the code I went with (mostly @anon39580149’s method with the draw order method from you @AndersDeleuran :

from ghpythonlib.componentbase import executingcomponent as component
import Grasshopper, GhPython
import System
import Rhino
import Rhino as rh
import Rhino.Geometry as rg
import rhinoscriptsyntax as rs

class MyComponent(component):
    
    def RunScript(self, P, F, S, D, V):

        if S == None: S = 20
        
        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, e):
        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)
                e.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

@AndersDeleuran playing around with your script I have a lot of new, inspired ideas about interesting use cases for drawing the text and symbol to the viewport 2D like that. Thank you very much, I’m excited to experiment more with it.

Custom Symbols In Action:

1 Like