I’m toying around with an “import layers from file” function. I am able to successfully import layers (including tree hierarchy, which is good) using the following code:
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
def ImportLayersFromFileTest():
filename=rs.OpenFileName("Choose file to import layers")
if not filename: return
#optional, doesn't change much
#tbl_filt=Rhino.FileIO.File3dm.TableTypeFilter.None
#no_obj=Rhino.FileIO.File3dm.ObjectTypeFilter.None
#read_file=Rhino.FileIO.File3dm.Read(filename,tbl_filt,no_obj)
read_file=Rhino.FileIO.File3dm.Read(filename)
if read_file:
layer_list=read_file.AllLayers
for layer in layer_list:
sc.doc.Layers.Add(layer)
ImportLayersFromFileTest()
This does work to import the layer arrangement without objects, which is what I want. But, some of the layer characteristics are not imported - notably the layer material assignment if any the linetype. OTOH, layer print color and print width are imported.
So the question is this: is there some easy trick I’m missing or do I also need to get the material table separately, add the materials to the file if they do not already exist, and then re-assign them to the imported layers? Same for the linetypes?
I also understand that I will need to do some checking for existing layers and possible conflict resolution, but that’s for later…
Hi @Helvetosaur , how do you execute this script. I copied the text into a .py file and tried to run it but nothing happened. Does the script work on macs?
No idea if it runs on Mac. To try in Mac V6, save the file somewhere like on your desktop, then use RunPythonScript and browse to the .py file to run it. Then follow the prompts to find the file to read layers from. This is just a test script though, there is much more work to do - however, I’m not able to finish it at the moment.
Not really; they are both the same thing under the hood. AllLayers property was added to support some more operations that we couldn’t add with the older Layers property without breaking the SDK.
Hi Mitch, I needed this today and it did the trick. Thank you. Not sure if you ever kept tuning this or not, but would be great to have the option to choose the layers to import via a checkbox list or something.
For those wondering about use case: I need JUST the layers, named the way they are, to be imported into a different file. Handy to have this script instead of needing to delete all geometry first then importing the whole file with the other overhead.
@pascal Could be nice to have the option to export/import the actual layers via Layerstatemanager. Then importing the state from another file could give the option to import the actual layers as well, sans objects of course.
Apologies if this has all been solved with other means by now.
I looked into this, and it’s complicated if there are sublayers. I can make it work more or less by importing the parent layers of any sublayers chosen, but it breaks down if a layer in the current file has the same name as a parent layer to be imported…
There are several limitations that make it this way, one is that the GetLayers() layers dialog only operates on the current file’s full layer list, it’s not possible to make it work on the imported file’s layers nor on a subset of them.
I’m sure it’s possible to do, but it will likely take bigger brains than mine to do it. Dealing with sublayers makes my head swim…