Using Kangaroo outside of Grasshopper?

Is it possible to use Kangaroo outside of Grasshopper i.e. in a Python script that’s opened from Rhino with EditPythonScript? I know you can use Grasshopper components with:

import ghpythonlib.components as ghcomp

but Kangaroo is a little different because it has state.

The short answer is yes. Here is a sample from @DanielPiker which demonstrates how to use the Kangaroo API in Rhino: https://github.com/Dan-Piker/Drop

2 Likes

We’ve also been implementing Kangaroo2 in IronPython since it was a wee Joey. There’s an example of how to reference the assembly here and an implementation of a zombie solver (i.e. one that solves the goals in one go, such that you won’t have to worry about implementing the iterative step solving as a dynamic process):

Edit: note that these example files are quite old at this point, and a lot has happened since. So I’m sure there are better, more efficient approaches here and there.

3 Likes

@fraguada @AndersDeleuran thanks for those references.

I just did a straight translation of one of the Kangaroo examples (Catenary.gh) to a Rhino Python script and it seems to work OK:

import ghpythonlib.components as ghc
from Rhino.Geometry import Vector3d, Rectangle3d, Plane, Point3d
from scriptcontext import doc

# Mesh plane
plane = Plane(Point3d(-10, -10, 0), Vector3d(0, 0, 1))
boundary = Rectangle3d(plane, 20, 20)
mesh, _ = ghc.MeshPlane(boundary, 10, 10)

# Stretch resistance for edges
naked_edges, interior_edges, _ = ghc.MeshEdges(mesh)
length_goal = ghc.Kangaroo2Component.LengthLine(naked_edges + interior_edges, None, 10)

# Fix corners in place
corners = ghc.Kangaroo2Component.MeshCorners(mesh, 2.356194)
anchor_goal = ghc.Kangaroo2Component.Anchor(corners, None, 1000)

# Apply loads to all vertices
vertices, _, _, _ = ghc.DeconstructMesh(mesh)
load_goal = ghc.Kangaroo2Component.Load(vertices, Vector3d(0, 0, 1), 1)

goals = length_goal + anchor_goal + load_goal
_, _, output = ghc.Kangaroo2Component.Solver(goals, False, 1e-15, 0.01, True)

# Output lines
for line in output:
    doc.Objects.AddLine(line)
doc.Views.Redraw()

I like the idea in Dynamo where you can switch to DesignScript if you need to. Seems like you can do the same in Grasshopper.