Skip to main content

I am running some API code. It was working fine before, but now I am running it again and am getting the error 'Attribute Error: 'NoneType' object has no attribute 'ClearDetectors''. Why is this suddenly appearing?

This type of error typically appears when we are using a class or property from within the ZOSAPI.Tools namespace. This includes the Non-Sequential Ray Trace tool, each of the optimizers, and the tolerancing tool. In OpticStudio, only one tool may be open at a time – it is the same in the API. This error will usually appear when we attempt to open a tool without closing out of all of the other tools first.



There are two ways to close a tool. The most common way is by adding the line XXXXX.Close() (where “XXXXX” is the name of the tool) to the end of the code block utilizing the tool. This is typically placed directly after the XXXXX.RunAndWaitForCompletion line.



You can also try to close any tools that are currently open by running the line TheSystem.Tools.CurrentTool.Close(). With this line, you do not have to know the name of the object that houses the currently-opened tool. However, this line will return an error if no tools are open. As such, it is most useful to add this line to the top of the API script and to keep it commented out. Then, if the attribute error is appearing, you may un-comment this line and try to run the script with it in place.



In the API, a tool will stay open until it is closed. As such, if we exit the code early, a tool could remain open even when the code is re-run. For example, let's say we have the following code block:



 





 



In this code block, I have inserted a breakpoint at line 50. This breakpoint comes before I close out of the tool. Now, let's say I stop the code at this breakpoint. When I attempt to run the code again, the original NSCRayTrace tool from the first run will be considered 'open' and I will see the error 'Attribute Error: 'NoneType' object has no attribute 'ClearDetectors'' when I try to open and run it again.



If the error continues to appear even after using the above closing operations, then the next step will be to close the API program or OpticStudio (or both) and re-run the code after they are open again. This will 'refresh' the connection and ensure that any previously-opened tools are no longer saved in memory.


Reply