How to remove "everything" using RhinoCommon

I am working on a script that will eventually be a “server” using RhinoInside with Python. Therefore, I have to use RhinoCommon methods. The program will hang out until asked to do something - then process some files and hang out some more. This will be in production and probably be running for months before the computer is restarted.

The issue that I have is that I need to really “cleanup” after one process is finished. There cannot be any objects or layers left over. The attached script is my failed attempt.

The first part removes all of the objects. Sort of. The document.Objects.Count returns the same number before and after deleting the objects but then reports zero when the next loop is run. I don’t understand why.

The second part tries to remove the layers, but I end up gaining a new layer after each run. Of course, that isn’t going work.

Can anyone point out the problems?
clearDocumentLoop.py (1.3 KB)

Here is the output from running the script:

================================================================================ Run 0
Before importing the file there are 0 objects
Trying to completely delete 206 objects
Now there are 206 objects
There are currently 7 layers in the document
[False] → Purging layer “Default” with index 0
[True] → Purging layer “Layer 01” with index 1
[True] → Purging layer “Layer 02” with index 2
[True] → Purging layer “Layer 03” with index 3
[True] → Purging layer “Layer 04” with index 4
[True] → Purging layer “Layer 05” with index 5
[True] → Purging layer “Target” with index 6
There are now 7 layers in the document
================================================================================ Run 1
Before importing the file there are 0 objects
Trying to completely delete 206 objects
Now there are 206 objects
There are currently 8 layers in the document
[False] → Purging layer “Default” with index 0
[False] → Purging layer “None” with index 1
[False] → Purging layer “None” with index 2
[False] → Purging layer “None” with index 3
[False] → Purging layer “None” with index 4
[False] → Purging layer “None” with index 5
[False] → Purging layer “None” with index 6
[True] → Purging layer “Target” with index 7
There are now 8 layers in the document
================================================================================ Run 2
Before importing the file there are 0 objects
Trying to completely delete 206 objects
Now there are 206 objects
There are currently 9 layers in the document
[False] → Purging layer “Default” with index 0
[False] → Purging layer “None” with index 1
[False] → Purging layer “None” with index 2
[False] → Purging layer “None” with index 3
[False] → Purging layer “None” with index 4
[False] → Purging layer “None” with index 5
[False] → Purging layer “None” with index 6
[False] → Purging layer “None” with index 7
[True] → Purging layer “Target” with index 8
There are now 9 layers in the document
================================================================================ Run 3
Before importing the file there are 0 objects
Trying to completely delete 206 objects
Now there are 206 objects
There are currently 10 layers in the document
[False] → Purging layer “Default” with index 0
[False] → Purging layer “None” with index 1
[False] → Purging layer “None” with index 2
[False] → Purging layer “None” with index 3
[False] → Purging layer “None” with index 4
[False] → Purging layer “None” with index 5
[False] → Purging layer “None” with index 6
[False] → Purging layer “None” with index 7
[False] → Purging layer “None” with index 8
[True] → Purging layer “Target” with index 9
There are now 10 layers in the document
================================================================================ Run 4
Before importing the file there are 0 objects
Trying to completely delete 206 objects
Now there are 206 objects
There are currently 11 layers in the document
[False] → Purging layer “Default” with index 0
[False] → Purging layer “None” with index 1
[False] → Purging layer “None” with index 2
[False] → Purging layer “None” with index 3
[False] → Purging layer “None” with index 4
[False] → Purging layer “None” with index 5
[False] → Purging layer “None” with index 6
[False] → Purging layer “None” with index 7
[False] → Purging layer “None” with index 8
[False] → Purging layer “None” with index 9
[True] → Purging layer “Target” with index 10
There are now 11 layers in the document
================================================================================ Run 5
Before importing the file there are 0 objects
Trying to completely delete 206 objects
Now there are 206 objects
There are currently 12 layers in the document
[False] → Purging layer “Default” with index 0
[False] → Purging layer “None” with index 1
[False] → Purging layer “None” with index 2
[False] → Purging layer “None” with index 3
[False] → Purging layer “None” with index 4
[False] → Purging layer “None” with index 5
[False] → Purging layer “None” with index 6
[False] → Purging layer “None” with index 7
[False] → Purging layer “None” with index 8
[False] → Purging layer “None” with index 9
[False] → Purging layer “None” with index 10
[True] → Purging layer “Target” with index 11
There are now 12 layers in the document
================================================================================ Run 6
Before importing the file there are 0 objects
Trying to completely delete 206 objects
Now there are 206 objects
There are currently 13 layers in the document
[False] → Purging layer “Default” with index 0
[False] → Purging layer “None” with index 1
[False] → Purging layer “None” with index 2
[False] → Purging layer “None” with index 3
[False] → Purging layer “None” with index 4
[False] → Purging layer “None” with index 5
[False] → Purging layer “None” with index 6
[False] → Purging layer “None” with index 7
[False] → Purging layer “None” with index 8
[False] → Purging layer “None” with index 9
[False] → Purging layer “None” with index 10
[False] → Purging layer “None” with index 11
[True] → Purging layer “Target” with index 12
There are now 13 layers in the document
================================================================================ Run 7
Before importing the file there are 0 objects
Trying to completely delete 206 objects
Now there are 206 objects
There are currently 14 layers in the document
[False] → Purging layer “Default” with index 0
[False] → Purging layer “None” with index 1
[False] → Purging layer “None” with index 2
[False] → Purging layer “None” with index 3
[False] → Purging layer “None” with index 4
[False] → Purging layer “None” with index 5
[False] → Purging layer “None” with index 6
[False] → Purging layer “None” with index 7
[False] → Purging layer “None” with index 8
[False] → Purging layer “None” with index 9
[False] → Purging layer “None” with index 10
[False] → Purging layer “None” with index 11
[False] → Purging layer “None” with index 12
[True] → Purging layer “Target” with index 13
There are now 14 layers in the document
================================================================================ Run 8
Before importing the file there are 0 objects
Trying to completely delete 206 objects
Now there are 206 objects
There are currently 15 layers in the document
[False] → Purging layer “Default” with index 0
[False] → Purging layer “None” with index 1
[False] → Purging layer “None” with index 2
[False] → Purging layer “None” with index 3
[False] → Purging layer “None” with index 4
[False] → Purging layer “None” with index 5
[False] → Purging layer “None” with index 6
[False] → Purging layer “None” with index 7
[False] → Purging layer “None” with index 8
[False] → Purging layer “None” with index 9
[False] → Purging layer “None” with index 10
[False] → Purging layer “None” with index 11
[False] → Purging layer “None” with index 12
[False] → Purging layer “None” with index 13
[True] → Purging layer “Target” with index 14
There are now 15 layers in the document
================================================================================ Run 9
Before importing the file there are 0 objects
Trying to completely delete 206 objects
Now there are 206 objects
There are currently 16 layers in the document
[False] → Purging layer “Default” with index 0
[False] → Purging layer “None” with index 1
[False] → Purging layer “None” with index 2
[False] → Purging layer “None” with index 3
[False] → Purging layer “None” with index 4
[False] → Purging layer “None” with index 5
[False] → Purging layer “None” with index 6
[False] → Purging layer “None” with index 7
[False] → Purging layer “None” with index 8
[False] → Purging layer “None” with index 9
[False] → Purging layer “None” with index 10
[False] → Purging layer “None” with index 11
[False] → Purging layer “None” with index 12
[False] → Purging layer “None” with index 13
[False] → Purging layer “None” with index 14
[True] → Purging layer “Target” with index 15
There are now 16 layers in the document

How many requests are likely to arrive at the server simultaneously?

Is it possible to create a new document for each web request, and either discard or save then delete them after the response is served?

Hello James,

It will be in a factory, so there should never be simultaneous request. It is a pretty sequential set of steps. The main reason for the server concept is to avoid the overhead of Rhino starting all the time. It worked nice for the previous project.

I didn’t think of making a new document for each “run”. There might be some sneaky stuff involved… how to really “delete” a document and start a fresh one?
document = None
document = sc.doc

RhinoCommon is a struggle for me. Obvious, right?

I would like to know why the layers and document.Objects.Count are not behaving as expected.

Thank you for your ideas - I appreciate it.

Why import into an existing, empty document? Why not just open the document and then throw it away when finished?

– Dale

Hello Dale,

The server program works on different processes that require many files - it is common to load 2 Rhino files, 2 STEP files, and a CSV file. Unfortunately, it is not just one file.

I guess that I could have a blank file that I open and use that as a solution. It looks like that would require a way to know if it should use RhinoDoc.Open (when developing in the GUI) and RhinoDoc.OpenHeadless (when in use).

What is the command for “throw it away when finished”? I tried RhinoDoc.Dispose because it sounded appropriate.

Something this should do it:

if doc:
    doc.Modified = False
    doc.Dispose()
    doc = None

– Dale