I’ve found that rs.DeleteObject won’t delete object in locked layer. Do I always have to programmatically check whether a layer is locked? It gets quite complex when the layer is nested in another layer, since rs.LayerLocked won’t work either. Any workarounds?
Something like this should work (try it with a layer called “LockedLayer”):
import rhinoscriptsyntax as rs
import scriptcontext as sc
objIDs=rs.ObjectsByLayer("LockedLayer")
if objIDs:
for obj in objIDs: sc.doc.Objects.Purge(rs.coercerhinoobject(obj))
sc.doc.Views.Redraw()
I guess it just assumes that objects or layers are locked for a reason and that you don’t want to delete them… That being said, @dale@stevebaer it seems to me that this is a good candidate for an additional optional argument to DeleteObjects()…
rs.DeleteObjects(objs, DeleteLocked=False)
Would work like now if no argument or False was specified; if the DeleteLocked flag is set to True, then objects that are locked or on locked layers are allowed to be deleted…
I think that keeping objects from being accidentally deleted is pretty much the main reason for having a layer locking feature. I also think that from a scripting point of view there is practically no difference between your unlock/relock scheme and the DeleteLocked option scheme, with the exception that the unlock/relock scheme is probably less programming accident-prone and easier to notice bugs.
Well, I’m not sure about that… One thing I don’t like is if the user aborts somewhere in mid script, that layers that were locked are left unlocked… Adding an additional argument to DeleteObjects that you have to expressly set in order to delete objects seems to me to be a good way to deal with this.
This has been requested before and already added to RhinoScript WIP (both DeleteObject and DeleteObjects methods now have a blnIgnoreModes switch). Not sure if RhinoscriptSyntax / Python methods were updated yet.
Hi @dale, seems there is a same issue with doc.Objects.Replace() for objects on locked layer. In C#, objects cannot be replaced if they are on a locked layer. That’d be great if you could add the same mode check for doc.Objects.Replace().
Does this work in rhino 7? I am trying it and apparently not…
I would like to delete all the block instances on hidden layers and maybe locked with a python scirpt,How can I do it?