Hi Bruno,
Your sample was the key for figuring out what is going on - thanks for spending the time to type this up.
I’ll try my best to explain what is going on and how you can fix your code.
First, if you open the Layers.py
file included with Rhino, you will see the following definition for the rs.LayerNames
method:
def LayerNames(sort=False):
rc = []
for layer in scriptcontext.doc.Layers:
if not layer.IsDeleted: rc.append(layer.FullPath)
if sort: rc.sort()
return rc
Notice the scriptcontext.doc
statement. If you open scriptcontext.py
, again included with Rhino, you will see this:
'''The Active Rhino document (Rhino.RhinoDoc in RhinoCommon) while a script
is executing. This variable is set by Rhino before the execution of every script.
'''
doc = None
When you first run your script, scriptcontext.doc
is set to the active document.
Your script then calls rs.Command
and opens a new document. The document referenced by scriptcontext.doc
is no longer valid.
You then call rs.LayerNames
, which tries to get layer names from an invalid document. Thus, nothing is returned.
To fix this problem, add the following statement immediately after opening a new document:
scriptcontext.doc = Rhino.RhinoDoc.ActiveDoc
Now, scriptcontext.doc
references a valid document and all rs
methods will work.
@stevebaer, @Alain, I’m not sure you’ve run into this before (I certainly haven’t). Something to bookmark I guess…
Let me know if there are any questions.
– Dale