Hello,
I am using RhinoCommon in Python to load a 3dm file. Right now, this is happening “headless”, but I would like the code to not break if I run it in the Rhino GUI. The code snippet below seems to load the correct number of objects, but the layers are lost. There is only one layer named “default” instead of the ones that are really in the file.
I suspect that the way the objects are added into the document is not correct.
Thanks in advance for your advice.
# Check if the file exists
if os.path.exists(filePath) is False:
message = f'The file does not exist: "{filePath}"'
self.log(message)
return 0
# If the file does not exist it throws an exception
# If the file exists but is not a Rhino file, then it returns None
file3dm = self.rhino.FileIO.File3dm.Read(filePath)
self.log(f'Result of reading the Rhino 3dm file is {file3dm}')
if file3dm is None:
return 0
counter = 0
for obj in file3dm.Objects:
geo = obj.Geometry
attr = obj.Attributes
# Add the objects to active document
self.document.Objects.Add(geo, attr)
counter += 1
self.log(f'Added {counter} objects to the document')
self.document.Views.Redraw()
# For testing, let's print all of the layer names
layerCount = self.document.Layers.Count
self.log(f'Here are the layer names for {layerCount} layers')
for n in range(layerCount):
layerObject = self.document.Layers[n]
self.log(f' Name={layerObject.Name}')
return file3dm.Objects.Count