Hi Paolo,
Do you have LensUpdateMode
set to None (and the UI changes disabled):
from System import Enum
TheSystem.UpdateMode = Enum.Parse(ZOSAPI.LensUpdateMode, "None")
TheApplication.ShowChangesInUI = False
Beyond these small changes, there is no way of speeding this up, at least based on how OpticStudio and the ZOS-API are written.
For almost every set command in OpticStudio (either via the GUI or the API), a system level update needs to be performed; very low-level operations, mostly involving paraxial ray tracing, such where the Stop is located or the system F/#, are updated with each change to the system. Although these low-level operations are extremely fast, there is computation overhead which will add up inside a for loop.
For any get command, there is no low-level update to the system so these commands are almost instantaneous.
Outside of Zemax rewriting the core code or adding complex logic to the ZOS-API (which probably won’t happen...the testing alone from a change like this would take months to verify), the set will always be longer than the get. Zemax might be able to implement a case-by-case batch update for something like Zernike Coefficients, but this is probably very low on their roadmap.
The only work-around for this which is extremely hacky (READ: try at your own risk) would be to ensure your MC files are saved as the text-based ZMX format, open the raw ZMX file outside of the ZOS-API, modify the coefficients with simple Python write commands, and then load the modified ZMX in OpticStudio. There is no guarantee that the ZMX format will stay consistent, that Zemax won’t implement internal checks to future versions of OpticStudio to prevent the ability to load a modified ZMX files, or even that the ZMX format will be around in the future. So this would potentially be a lot of effort which might not pay off in the end.
Thank you Michael for the quick answer! It appears to do the trick:
%timeit M1_surf.SurfaceData.SetNthZernikeCoefficient(4, 6.46449601150208e-06)
12.7 ms ± 241 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
I get an even greater (4x) speedup by disabling the undo function from the Project Preferences menu: any way to do this programmatically?
Thanks again,
Paolo
Hi Paolo,
Unfortunately, there is not a way to programmatically turn on/off the undo function. The only related options in the Project Preferences via the ZOS-API would be saving session file but since the Undo only saves the ZMX/ZOS and not the ZDA, this setting will have no impact.
A workaround would be to save a Preferences CFG file with the undo turned off before running your API, use the TheConnection.PreferencesFile
to load the CFG with the undo turned off, and then to run your script. This is similar to using ModifySettings() with a CFG for analysis which do not have their individual settings hooked up to the API. The following articles talks about how to use a use Preferences CFG file:
Get and Set Folders in Project Preferences of OpticStudio from ZOS API | Zemax Community