Get Pugins used in Grasshopper canvas

Hello everyone,

I wrote a C# code (R8) in order to find the plugins used in a canvas. It works quite well although to make it update from time to time I have to attached a trigger. I wonder if there is a way to do the same but using events as @sonderskovmathias did for the grouping template.

GitHub - Sonderwoods/GrasshopperScribbles: A few cool grasshopper scripts such as template and eventhandlers

// Grasshopper Script Instance
using System;
using System.IO;
using System.Collections.Generic;
using Grasshopper.Kernel;
using Rhino;
using Rhino.Geometry;

public class Script_Instance : GH_ScriptInstance
{
    private void RunScript(ref object Disallowed_Plugins)
    {
        
        // --- Plugin Checking ---
        var plugins = new Dictionary<string, string>();
        var Allowed_Plugins = new List<string> { "MetaHopper", "Grasshopper-Tekla Link 2024.1.17" };
        var Different_Plugins = new List<string>();

        // Access the Grasshopper document from the script context
        var components = this.Component.OnPingDocument().Objects;

        foreach (var obj in components)
        {
            if (obj is IGH_Component component)
            {
                var plugin = Grasshopper.Instances.ComponentServer.FindAssemblyByObject(component.ComponentGuid);

                if (plugin?.IsCoreLibrary == false && 
                    !string.IsNullOrEmpty(plugin.Name) && 
                    plugin.Version != null && 
                    !plugin.Name.StartsWith("Rhino"))
                {
                    plugins[plugin.Name] = plugin.Version.ToString();
                }
            }
        }

        // If no plugins found, set Disallowed_Plugins to "No plugins used"
        if (plugins.Count == 0)
        {
            Disallowed_Plugins = "No plugins used";
        }
        else
        {
            foreach (var plugin in plugins)
            {
                if (!Allowed_Plugins.Contains(plugin.Key))
                {
                    Different_Plugins.Add(plugin.Key);
                }
            }

            Disallowed_Plugins = Different_Plugins.Count == 0 ? "No disallowed plugins used" : Different_Plugins;
        }
    }
}

Regards!

I will write something up for you as soon as I get time from all my kids :grimacing:

1 Like

Suuuper, thank you for your help :slight_smile:

BTW where did you learn about the events stuff? I would be very interested in learning more about that!

They are part of most C# online courses and books around :wink:

Once you find where they are hidden in rhinodoc or ghdoc it’s easy to subscribe to them. It’s a fantastic way from mcneels side to make it expandable.

1 Like