Skip to content

Instantly share code, notes, and snippets.

@gperonato
Forked from bengolder/gist:1019295
Last active May 23, 2016 13:40
Show Gist options
  • Save gperonato/0b6bfee6ee32d3770f22faf4fa8e1843 to your computer and use it in GitHub Desktop.
Save gperonato/0b6bfee6ee32d3770f22faf4fa8e1843 to your computer and use it in GitHub Desktop.
Grasshopper File and Geometry Import Script
# Original code by bengolder https://gist.github.com/bengolder/1019295
# Modified by gperonato https://gist.github.com/gperonato/
#Inputs: FilePath (String) and DoImport (Boolean)
#Outputs: Geometry (Tree of geometry) and LayerList (List of strings)
import Rhino
# for accesssing GH classes
import clr
clr.AddReference("Grasshopper")
from Grasshopper.Kernel.Data import GH_Path
from Grasshopper import DataTree
# read geometry and layer names out of 3dm files
if DoImport and FilePath:
Geometry = DataTree[Rhino.Geometry.GeometryBase]() # make a DataTree
model = Rhino.FileIO.File3dm.Read(FilePath)
Layers = model.Layers
LayerList = []
for layer in Layers:
LayerList.append(str(layer))
for i, layer in enumerate(LayerList):
path = GH_Path(i)
geometry = []
objs = model.Objects.FindByLayer(layer)
for obj in objs:
geom = obj.Geometry
geom.EnsurePrivateCopy()
geometry.append(geom)
Geometry.AddRange(geometry, path)
# models can take up a considerable amount of
# memory that we don't need, so get rid of it
# after copying the geometry we want out of it
model.Dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment