Unable to Subscribe to Grasshopper DocumentRemoved Event

import Grasshopper
print(type(Grasshopper.GH_InstanceServer.DocumentServer.DocumentRemoved))
print(type(Grasshopper.Kernel.GH_DocumentServer.DocumentRemoved))
print(type(Grasshopper.Instances.DocumentServer.DocumentRemoved))

DocumentRemoved.gh (10.5 KB)

@eirannejad

It seems Grasshopper module in Script component refers to Grasshopper2.dll.

import Grasshopper
a = Grasshopper.__file__

@eirannejad
Could you please check this? The script component is still referencing grasshopper2.dll.

@Mahdiyar I have logged the issue here

The problem is both GH1 and GH2 use the Grasshopper namespace. This means python 3 will combine all the sub types and namespaces in both these assemblies under the same Grasshopper namespace.

This effectively replaces Grasshopper.Instances from GH1 with Grasshopper.Instances in GH2

RH-76310 Python 3 binds Grasshopper namespace in both Grasshopper.dll and Grasshopper2.dll

I’ll discuss with David and will look into this

Checking for __file__ is not a valid way of determining which dll the namespace is bound to as it could be multiple dlls.

1 Like

Thanks for pointing that out.
I primarily need access to the Grasshopper DLL to identify the locations of the libraries and user objects folders, which is essential for adding a DLL reference, especially in Rhino before Grasshopper is loaded.

import Grasshopper as gh
import clr
clr.AddReference(str(gh.Folders.AssemblyFolders[0]) + "KangarooSolver.dll")
import KangarooSolver as ks

Additionally, our plugin, Euglena, has used this approach in the past to copy .ghuser files into the User Objects folder.

import os
import shutil

import Grasshopper as gh

if run:
    source = os.path.join(directory, "UserObjects")
    destination = gh.Folders.DefaultUserObjectFolder
    for item in os.listdir(source):
        item_path = os.path.join(source, item)
        shutil.copy2(item_path, destination)

    try:
        __import__("euglena")
        print("Succeeded!")
    except ImportError:
        print("Failed.")
1 Like

You can use the HostUtils static methods for this:

GetAssemblyPaths.gh (6.9 KB)

2 Likes