-
-
Save gperonato/0b6bfee6ee32d3770f22faf4fa8e1843 to your computer and use it in GitHub Desktop.
Grasshopper File and Geometry Import Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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