How to add Grasshopper library icon in GhPython compiled GHPYs

found this post:
tab icon

Where would I insert these in the ghpy compilation process?
btw Guilio thanks for all the help. Hope you don’t mind all these pestering questions. Working with very limited programming skills/experience here

Hi @Will_Wang,

You do not need to override GH_AssemblyPriority in order to add a library icon.
However, you will need to use the “advanced” compiling mode, and manage compilation yourself.

After having copied the code to compile,…
image

you will have a stub for an AssemblyInfo class:

class AssemblyInfo(GhPython.Assemblies.PythonAssemblyInfo):
    def get_AssemblyName(self):
        return "Sample"
    
    def get_AssemblyDescription(self):
        return """"""

    def get_AssemblyVersion(self):
        return "0.1"

    def get_AuthorName(self):
        return ""
    
    def get_Id(self):
        return System.Guid("#use your ID")

That one allows also a get_Icon method, so you can add:

    def get_Icon(self):
        pass #access some resources somehow...

Now… how can we implement this method?

A simple way to embed the icon is to do what the component does by itself: parse an image stream. You can get the code for the current component by just setting an icon (drag-and-drop as 24x24 icon onto GhPython) and running the “Copy compilable code” wizard again. The icon code for the component looks like this:

    def get_Internal_Icon_24x24(self):
        o = "iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAFKSURBVEhL7ZS9K0VhAIePr0EYbFbZrBjY/A0SixVlkEEpZfEHiFJK2QwGG6tFlJLBriwGi1HI1/Pc9311u+49nJs7qPvU0/s776n3dz7ec7Im9dITx0RnHCspn++KYy7D+IQfuIp9+ByP33AIxeOXOG6h58x7mMsCLob4jTlcCbG02GCI2QOOhJjdYmuI1fGWd/EMR50Ar/ARXXTNCTAnbuIoV9gdYj4zeIITuOkEzGIqeI+jFCrYRq9O53EA0/N10Wp3UJl/vINebAvxi1q76P8xFseGsYPjITaGPyvwi3ab+lW7e9ZRygs20B3m+SknimDBXYgl0lZMBZN44QT4Xk5D/D0WHIZY4hL7MRUso6XXeI7HWAgL/K8kXKwFU8E0+ojqxoJ7fEUXX0IpfwcH6Dn1XRTCgiNsR6+8Fh3ol18YC/ZDbNJQsuwT/DhKCJAtLxAAAAAASUVORK5CYII="
        return System.Drawing.Bitmap(System.IO.MemoryStream(System.Convert.FromBase64String(o)))

So, at this point, just place the resulting code into the AssemblyInfo -> get_Icon def.
You can also devise any other valid IronPython method to implement get_Icon.

I hope this helps,

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

1 Like

Should I do that for all components under a tab?
I put that get_Icon() in one of the components and recompiled but the tab icon did not change.
Also this get_Icon() won’t override existing tab icon will it? I recompiled a few components into an existing tab that has others so maybe that’s why.

It seems that there’s a little confusion.

If you create more than one component, you should compile them into a single GHPY assembly.
Every assembly should have a single AssemblyInfo class.

Avoid shipping more than one GHPY assembly for the same functionality.

Does it help?

so this is what I copied from the compiled code from GhPython for one of three components I intend to make. Each time the GhPython would generate a different AssemblyInfo class. are you saying I should manually unify this class across three components?image

Then what about thisimage
I thought this is what controls how a component shows up in the tabs

1 Like

Yes. Just delete all except one. The wizard only does what it can, it cannot be rational human being programming for you. :slight_smile:
You should keep the ID that you used originally, so that serialization can work correctly.
You can also to read the Grasshopper SDK documentation to know more info on components, the GH SDK, etc.

You need one of each for each component in your collection :slight_smile: that is really the component code.
the __new__() def initializes and creates the instance of the component for you. It’s a .Net constructor.

1 Like

It seems that getting the AssemblyInfo straight helped me grouped them. (Now my three components are separated from others by a divider)image
Still can’t seem to get a tab icon, even when I assigned a new unique tab and recompiled. image
Sadly last time I went fishing in the GH SDK, I got mightly lost. Maybe one day I’ll understand enough to navigate it.

@DavidRutten will have to say where that is taken from. I am not entirely sure.
The icon for GH_AssemblyInfo goes into the little “+” that is in the About box.

Yes! I saw mine. So I guess it’s really just not placed correctly. Perhaps we are looking for a Category(there is this GH_ComponentServer.AddCategoryIcon method. no clue how to use it), not an Assembly? Really appreciate your time @piac helping on this

Oh, yes, that indeed sounds like the right method. Now that I understand what you are after, you can easily do the same for that method with the icon (same process as before) and the method:

You can invoke this, I think, from a new __init__(self) initializer def in the GH_AssemblyInfo class.

You need to create a 16x16 icon, though, and without the help of the wizard, it will be a little more complicated… I’ll write a sample for you tomorrow if you need it.

Code sample of how it’s invoked in the AssemblyInfo class will be great! Can I use the same string-to-bitmap conversion used for the assembly and component icons? I can copy the code if that’s the case.

Here is the code. This is ready for usage.

class AssemblyInfo(GhPython.Assemblies.PythonAssemblyInfo):
    def __init__(self):
         #use your icon here
         icon_text = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwgAADsIBFShKgAAAABl0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMC4yMfEgaZUAAAAMSURBVBhXY/j//z8ABf4C/qc1gYQAAAAASUVORK5CYII="
         icon = System.Drawing.Bitmap(System.IO.MemoryStream(System.Convert.FromBase64String(icon_text)))
         Grasshopper.Instances.ComponentServer.AddCategoryIcon("IWantAnIconAddon",   );
         Grasshopper.Instances.ComponentServer.AddCategorySymbolName("IWantAnIconAddon", 'I');

#... keep the rest here
2 Likes

YES and YES
fun fact: when I invoked gh library by typing
Grasshopper.
in rhino python editor, grasshopper splash screen comes up and looks like it’s launched. But this is the least of my concerns.

1 Like