(Python) ERROR: This object cannot be modified because it is controlled by a document

I have a series of python scripts that edit layers and move objects between layers. At random, apparently, I’ll get this error:

pasted_image_at_2017_09_06_11_31_am

I’m using Worksessions with my document as well as linked blocks, but this particular script does nothing but insert layers. Also, it will work for hours happily with worksessions, then it will do this. Restarting the document fixes this.

It happens on several scripts wherever LayerNames() is called.

   def LayerNames(sort=False):
   ""Return names of all layers in the document.
    Parameters:
   sort [opt] = return a sorted list of the layer names
   Returns
   list of strings
   """
   rc = []
   for layer in scriptcontext.doc.Layers:
       if not layer.IsDeleted: rc.append(layer.FullPath)
   if sort: rc.sort()
   return rc

the error is on the line

   if not layer.IsDeleted: rc.append(layer.FullPath)

Here is the (hacky) script itself:
https://pastebin.com/BabrjaGX

Hi @shanel,

This works in both Rhino 5 and the Rhino WIP:

import rhinoscriptsyntax as rs
layers = rs.LayerNames()
if layers:
    for layer in layers: 
        print layer

What are we missing?

Yes, exactly. I have no idea. It works until it doesn’t. I thought it might have to do with scriptcontext, My experience with this error has been that the scripts work just fine, but randomly LayerNames() starts throwing that error. I haven’t been able to find any cause or pattern, so I know I’m all but useless in helping track down the issue. Three separate scripts threw this error, all on LayerNames().

I’m not sure what is different, I’ve been using these scripts for over a year with no issue, but in the last week I’ve started re-writing them and I’m getting these random errors.

Here are the scripts that have shown that error (and their dependent module).

Layer Sorting Module:
https://pastebin.com/5ErfnSnc

MoveToLayer:
https://pastebin.com/vieZiTS1

Change Active Layer Set:
https://pastebin.com/dnfKtUGG

Populate Layer with CAD Sub-Layers:
https://pastebin.com/cCrrvicg

@shanel,

what happens if you replace line 53:

rs.AddLayer(entry, Color.FromArgb(layerDict[entry]['LayerColor']), parent=currentLayer)

with this line:

rs.AddLayer(cLayerName, Color.FromArgb(layerDict[entry]['LayerColor']))

does your error come up if you do not use WorkSessions too ?

_
c.

Near the end of my day this happened again. I was using one of the scripts above (MoveToLayer) just fine, until I tried it again and got the error. Now even:

import rhinoscriptsyntax as rs
print(rs.LayerNames())

gives me:

Message: This object cannot be modified because it is controlled by a document.
Traceback:
   line 340, in LayerNames, "C:\Users\shanel\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython 
   (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\layer.py"
   line 6, in <module>, "C:\Users\shanel\AppData\Local\Temp\TempScript.py"

I literally used the command once and it worked fine, then immediately tried to use it again and it borked.

It is difficult to tell if it is Worksessions… I have to be in worksessions to do my work, and the error is so hit and miss I could never be quite sure if a non-worksession file is fixed or just happened to get lucky. It did start happening when I started doing this particular project (I’ve worksessioned before with earlier versions of this script with no problems before), but I also started re-writing the scripts with the Layer Sorting Module.

Could the module have something to do with it?

Hi @shanel

What is the “Layer Sorting Module” doing? Is it the sort=True function argument?

In an ideal world, we could find a set of actions that cause this to happen.

Also, we improved worksessions heavily, and also the way RhinoScript/Python accesses them in Rhino WIP. Do you feel like seeing if that makes the error go away?

The layer sorting module is looking for any layers beginning with an L or P followed by digits, and returning that layer. It filters out it’s children. Then it returns a sorted list of strings that my other scripts can operate on.

I’d love to try out the WIP, but will it work with others Rhino 5 files? I suppose I would have to save as Rhino 5 so they could work with my files. We have a team of about 5 and I don’t think we are ready to upgrade everyone to the WIP.

I’m curious of the nature of the error “Message: This object cannot be modified because it is controlled by a document.”. Why would that effect LayerNames() since it isn’t modifying anything? What object is it trying to modify? Pardon my Python ignorance.

Yes it will.

Sorry, we would really need some way to track this down. And even then, the Rhino 6 worksessions logic is much more solid. It would really be great to hear if this is fixed in WIP.