Very Slow Parameter Setting in ZOS-API

  • 18 September 2023
  • 2 replies

I am writing a program where I need to make several changes to values in the Lens Data Editor (e.g. parameters of a coordinate break surface) and evaluate merit function operands after each set of changes.  The problem is this is extremely slow, to the point that setting the parameters dominates runtime far beyond the time spent raytracing.  For example, this function averages about 1 second per call:

def set_offset(oss, surface_number, x, y, about_x, about_y):    oss.LDE.GetSurfaceAt(surface_number).SurfaceData.Decenter_X = x    oss.LDE.GetSurfaceAt(surface_number).SurfaceData.Decenter_Y = y    oss.LDE.GetSurfaceAt(surface_number).SurfaceData.TiltAbout_X = about_x    oss.LDE.GetSurfaceAt(surface_number).SurfaceData.TiltAbout_Y = about_y


I’m sure there are some minor optimizations I can make, but at its core, setting a single variable in memory shouldn’t take anywhere near 0.25 seconds.

  1. Am I doing something wildly wrong, or is there a far more efficient way to modify LDE entries?
  2. Is there a better way to go about large multidimensional parameter sweeps than a loop setting those parameter values and calling oss.MFE.CalculateMeritFunction() ?

I am using Zemax 2023 R2.01 and running the Python API in interactive mode.


2 replies

Userlevel 7
Badge +3

I’m not that familiar with the API, but are you possibly updating the lens every time you change something? In ZPL you can call SUSPENDUPDATES, make as many changes as you want, and then call a single UPDATE to flush all the changes at once. I’m not familiar enough with the API to say what the same commands are.


  • Mark
Userlevel 3

@ChrisP Do you run the api in interactive mode (which is slow) or in standalone mode (which is fast).

See also this thread 



Ps. If you do not want to be bothered by these type of implementation details, you could also give ZOSPy a try.