Skip to content

Instantly share code, notes, and snippets.

@fstwn
Last active September 6, 2023 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fstwn/b3e223bff0ad21619ae402b400b93b96 to your computer and use it in GitHub Desktop.
Save fstwn/b3e223bff0ad21619ae402b400b93b96 to your computer and use it in GitHub Desktop.
GhPython code for finding the KangarooSolver.dll that Rhino 6/7/8 ship with and importing it properly in most settings.
import Rhino
import clr
import os
if "KangarooSolver" in str(clr.References):
import KangarooSolver as ks
else:
try:
rhino_version = Rhino.RhinoApp.ExeVersion
if os.name == "nt":
if rhino_version == 6:
k2ap = ("C:/Program Files/Rhino 6/Plug-ins/Grasshopper/"
"Components/KangarooSolver.dll")
clr.AddReferenceToFileAndPath(os.path.normpath(k2ap))
elif rhino_version == 7:
k2ap = ("C:/Program Files/Rhino 7/Plug-ins/Grasshopper/"
"Components/KangarooSolver.dll")
if not os.path.exists(k2ap):
k2ap = ("C:/Program Files/Rhino 7 WIP/Plug-ins/Grasshopper/"
"Components/KangarooSolver.dll")
clr.AddReferenceToFileAndPath(os.path.normpath(k2ap))
elif rhino_version == 8:
k2ap = ("C:/Program Files/Rhino 8/Plug-ins/Grasshopper/"
"Components/KangarooSolver.dll")
if not os.path.exists(k2ap):
k2ap = ("C:/Program Files/Rhino 8 WIP/Plug-ins/Grasshopper/"
"Components/KangarooSolver.dll")
clr.AddReferenceToFileAndPath(os.path.normpath(k2ap))
elif os.name == "posix":
if rhino_version == 7:
k2ap = ("/Applications/Rhinoceros.app/Contents/Frameworks/"
"RhCore.framework/Versions/A/Resources/ManagedPlugIns/"
"GrasshopperPlugin.rhp/Components/KangarooSolver.dll")
clr.AddReferenceToFileAndPath(os.path.normpath(k2ap))
elif rhino_version == 8:
k2ap = ("/Applications/RhinoWIP.app/Contents/Frameworks/"
"RhCore.framework/Versions/A/Resources/ManagedPlugIns/"
"GrasshopperPlugin.rhp/Components/KangarooSolver.dll")
clr.AddReferenceToFileAndPath(os.path.normpath(k2ap))
import KangarooSolver as ks
except (IOError, ImportError):
try:
clr.AddReferenceToFile("KangarooSolver.dll")
import KangarooSolver as ks
except (IOError, ImportError):
raise RuntimeError("KangarooSolver.dll was not found! please add the "
"folder to your module search paths manually!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment