Connection

OpticStudioSession owns the Interactive Extension connection for this process. Creating a session starts the STA thread and connects to the OpticStudio window that is waiting. The session methods are the operations the tools call. Close the session with close(), or use it as a context manager.

SUPPORTED_RELEASE is the release this server accepts. OpticStudioError is raised when OpticStudio cannot be located or cannot answer.

A connection to one open OpticStudio window.

ZOS-API calls run on a single STA thread. The user opens Interactive Extension first. An instance number of 0 attaches to whichever window is waiting. The installation is located through Zemax’s own helper. When that cannot find OpticStudio, set OPTICSTUDIO_DIRECTORY to the installation directory.

class OpticStudioSession(instance: int = 0, directory: str | None = None)

Own one Interactive Extension connection and the thread that calls it.

Parameters:
  • instance (int) – OpticStudio instance number. 0 attaches to whichever window is waiting.

  • directory (str) – Installation directory. When omitted, Zemax’s helper locates it, then OPTICSTUDIO_DIRECTORY is used.

Raises:

OpticStudioError – OpticStudio cannot be located, or the connection fails.

apply_optimization_wizard(values: dict[str, object] | None = None) → dict[str, object]

Apply the Optimization Wizard and return the settings and operands.

The wizard is reset to its dialog defaults. Keys in values then replace those defaults. Omit values to keep every default. Operands from the wizard’s Start At row through the end of the editor are replaced. value and contribution are whatever the editor shows after that, and are not calculated.

Parameters:

values (dict[str, object] | None) – Wizard controls to change. The fields are the same as settings in the result. A glass or air thickness turns that boundary on when its checkbox is omitted. configuration selects one configuration, and field selects one field, when the matching “all” checkbox is omitted.

Returns:

settings and operands. settings are the wizard controls that were applied. Each operand has the same fields as merit_function().

Return type:

dict[str, object]

Raises:
  • ValueError – A wizard setting cannot be applied.

  • OpticStudioError – The session is already closed, or OpticStudio cannot answer.

calculate_merit_function() → dict[str, object]

Calculate the merit function and return its value and the operands.

value and contribution on each operand are the numbers from this calculation.

Returns:

merit and operands. merit is None when OpticStudio does not return a finite value. Each operand has the same fields as merit_function().

Return type:

dict[str, object]

Raises:
  • ValueError – The merit function cannot be calculated.

  • OpticStudioError – The session is already closed, or OpticStudio cannot answer.

call(function)

Run a function on the ZOS-API thread and return its result.

An exception raised by function propagates to the caller.

Parameters:

function – Called on the session thread.

Returns:

The value returned by function.

Raises:

OpticStudioError – The session is already closed.

close()

Disconnect from Interactive Extension and stop the ZOS-API thread.

Calling this again does nothing.

directory: str | None

Installation directory passed to this session. None means Zemax’s helper locates OpticStudio, then OPTICSTUDIO_DIRECTORY is used.

first_order() → dict[str, float | str | None]

Return first-order values for the lens open in the OpticStudio window.

The system is updated first. Lengths are in lens units. working_f_number is the on-axis value at the primary wavelength. total_length runs from surface 1 to the image surface.

Returns:

Effective focal length, entrance pupil diameter, paraxial and working F-numbers, and total length. A number is None when OpticStudio does not return a finite value. status is empty when the update reports nothing.

Return type:

dict[str, float | str | None]

Raises:

OpticStudioError – The session is already closed, or OpticStudio cannot answer.

installation_directory: str

OpticStudio installation directory this session is connected to.

instance: int

OpticStudio instance number. 0 attaches to whichever window is waiting.

lens_data(surface: int | None = None) → dict[str, object]

Return Lens Data Editor surfaces for the lens open in the OpticStudio window.

Parameters:

surface (int | None) – One surface number. None reads every surface. 0 is the object surface.

Returns:

Every surface, or the one requested surface. Each surface includes the parameter columns its type uses. The image surface omits thickness and thickness_solve. tce and tce_solve are omitted when the surface does not use the TCE column.

Return type:

dict[str, object]

Raises:
lens_file() → dict[str, str | bool]

Return identity details for the lens open in the OpticStudio window.

Returns:

The file path and name, title, author, and notes, the sequential or non-sequential mode, and whether the file has unsaved changes.

Return type:

dict[str, str | bool]

Raises:

OpticStudioError – The session is already closed, or OpticStudio cannot answer.

merit_function(operand: int | None = None) → dict[str, object]

Return Merit Function Editor operands for the lens open in the OpticStudio window.

Parameters:

operand (int | None) – One 1-based operand number. None reads every operand.

Returns:

Every operand, or the one requested operand. Each operand includes the parameter columns its type uses. target, weight, value, and contribution are omitted when the operand does not use them.

Return type:

dict[str, object]

Raises:
multi_configuration(operand: int | None = None) → dict[str, object]

Return the Multi-Configuration Editor for the lens open in the OpticStudio window.

Parameters:

operand (int | None) – One 1-based operand number. None reads every operand.

Returns:

The current configuration, the configuration columns, and the operands. With operand, that operand.

Return type:

dict[str, object]

Raises:
new_lens(discard_changes: bool = False) → dict[str, str | bool]

Replace the lens open in the OpticStudio window with a new lens.

Unsaved changes are discarded only when discard_changes is true. A lens with unsaved changes is left open otherwise. The new lens has not been saved.

Parameters:

discard_changes (bool) – True discards unsaved changes. False refuses when the lens needs to be saved.

Returns:

The lens identity after the new lens is created. The fields are the same as those returned by lens_file().

Return type:

dict[str, str | bool]

Raises:
  • ValueError – The lens has unsaved changes and discard_changes is false, or OpticStudio cannot create the lens.

  • OpticStudioError – The session is already closed, or OpticStudio cannot answer.

run_local_optimization(algorithm: str = 'DampedLeastSquares', cycles: str = 'Automatic') → dict[str, object]

Run local optimization on the lens open in the OpticStudio window.

The call waits until the run finishes. The core count stays at the value already set in the Optimize dialog.

Parameters:
  • algorithm (str) – DampedLeastSquares or OrthogonalDescent.

  • cycles (str) – Automatic, Fixed_1_Cycle, Fixed_5_Cycles, Fixed_10_Cycles, or Fixed_50_Cycles.

Returns:

The algorithm, cycle count, number of variables and targets, and the merit function before and after the run. A merit value is None when OpticStudio does not return a finite number.

Return type:

dict[str, object]

Raises:
  • ValueError – The algorithm or cycles cannot be used, the lens has no variable or no target, or the run does not finish.

  • OpticStudioError – The session is already closed, or OpticStudio cannot answer.

save(path: str | None = None) → dict[str, str | bool]

Save the lens open in the OpticStudio window.

Omit path to save the current file. Pass a full path to save the lens as a new file. An existing file at that path is replaced. A lens that has never been saved needs a path.

Parameters:

path (str | None) – Full path of the new lens file. None saves the current file.

Returns:

The lens identity after the save. The fields are the same as those returned by lens_file().

Return type:

dict[str, str | bool]

Raises:
  • ValueError – The lens has no current file, path is not a full path, or OpticStudio cannot save.

  • OpticStudioError – The session is already closed, or OpticStudio cannot answer.

set_lens_rows(action: str, number: int | None, values: dict[str, object] | None) → dict[str, object]

Change, insert, or remove one Lens Data Editor surface and return the editor.

add inserts a surface at number. Surfaces at that number and after move up.

Parameters:
  • action (str) – change, add, or remove.

  • number (int | None) – Surface number. 0 is the object surface.

  • values (dict[str, object] | None) – Keys to write. The fields are the same as those of one surface returned by lens_data(). radius and thickness accept Infinity for an infinite length. A radius of 0 is returned as Infinity. A thickness of 0 is zero. thickness and thickness_solve are rejected on the image surface. tce and tce_solve are rejected when the surface does not use the TCE column. type changes the surface type first. parameters maps column names such as Par1 to a value or to value and solve. radius_solve and the other cell solves set that cell’s solve.

Returns:

The surfaces after the change.

Return type:

dict[str, object]

Raises:
  • ValueError – The action, number, or values cannot be applied.

  • OpticStudioError – The session is already closed, or OpticStudio cannot answer.

set_merit_rows(action: str, number: int | None, values: dict[str, object] | None) → dict[str, object]

Change, insert, or remove one Merit Function Editor operand and return the editor.

add inserts an operand at number. Operands at that number and after move up.

Parameters:
  • action (str) – change, add, or remove.

  • number (int | None) – 1-based operand number.

  • values (dict[str, object] | None) – Keys to write. The fields are the same as those of one operand returned by merit_function(). type changes the operand type first. parameters maps column names such as Param1 to new values. target and weight are rejected when the operand does not use them.

Returns:

The operands after the change.

Return type:

dict[str, object]

Raises:
  • ValueError – The action, number, or values cannot be applied.

  • OpticStudioError – The session is already closed, or OpticStudio cannot answer.

set_multi_configuration_rows(section: str, action: str, number: int | None, values: dict[str, object] | None) → dict[str, object]

Change one Multi-Configuration Editor operand or configuration and return the editor.

add inserts at number. Following operands or configurations move up.

Parameters:
  • section (str) – operand or configuration.

  • action (str) – change, add, remove, or make_current.

  • number (int | None) – 1-based operand or configuration number.

  • values (dict[str, object] | None) – Keys to write. For an operand, the fields are the same as those of one operand returned by multi_configuration(). type changes the operand type first. parameters maps Param1, Param2, or Param3 to an integer. cells maps a configuration number to a value or to value and solve. For a new configuration, copy copies the current configuration’s cells.

Returns:

The editor after the change.

Return type:

dict[str, object]

Raises:
  • ValueError – The section, action, number, or values cannot be applied.

  • OpticStudioError – The session is already closed, or OpticStudio cannot answer.

set_system_data(name: str, values: dict[str, object]) → dict[str, object] | list[dict[str, object]]

Change keys in one System Explorer setting group and return the group.

Only keys in values are written. Field points and the wavelength list are rows and stay unchanged.

Parameters:
Returns:

The setting group after the change.

Return type:

dict[str, object] | list[dict[str, object]]

Raises:
  • ValueError – name is unknown, values is empty, or a key cannot be changed.

  • OpticStudioError – The session is already closed, or OpticStudio cannot answer.

set_system_rows(name: str, action: str, number: int | None, values: dict[str, object] | None) → list[dict[str, object]]

Change, add, or remove one field point or wavelength, then return the rows.

add appends a row. make_primary selects the primary wavelength.

Parameters:
  • name (str) – fields or wavelengths.

  • action (str) – change, add, remove, or make_primary.

  • number (int | None) – 1-based row. Required for change, remove, and make_primary.

  • values (dict[str, object] | None) – Keys to write. The fields are the same as those of one row returned by system_data().

Returns:

The rows after the change. Field rows are the field points.

Return type:

list[dict[str, object]]

Raises:
  • ValueError – name has no row list, or the action, number, or values cannot be applied.

  • OpticStudioError – The session is already closed, or OpticStudio cannot answer.

single_ray_trace(wavelength: int = 1, hx: float = 0, hy: float = 0, px: float = 0, py: float = 0, trace_type: str = 'DirectionCosines') → dict[str, object]

Return the Single Ray Trace table for one ray.

hx and hy are the normalized field coordinates. px and py are the normalized pupil coordinates. trace_type selects the table shown in that window.

Parameters:
  • wavelength (int) – 1-based wavelength number.

  • hx (float) – Normalized field coordinate X.

  • hy (float) – Normalized field coordinate Y.

  • px (float) – Normalized pupil coordinate X.

  • py (float) – Normalized pupil coordinate Y.

  • trace_type (str) – DirectionCosines, TangentAngle, or YmUmYcUc.

Returns:

The ray settings and one row per surface. columns are the table headings. values follow those headings. A missing number, such as an infinite object thickness, is None. message is empty when the table has no error line.

Return type:

dict[str, object]

Raises:
  • ValueError – The ray settings or the table cannot be used.

  • OpticStudioError – The session is already closed, or OpticStudio cannot answer.

system_data(name: str) → dict[str, object] | list[dict[str, object]]

Return one System Explorer section for the lens open in the OpticStudio window.

Parameters:

name (str) – units, aperture, fields, wavelengths, environment, polarization, or ray_aiming.

Returns:

The group values. Wavelengths are a list. The other groups are dictionaries.

Return type:

dict[str, object] | list[dict[str, object]]

Raises:
system_file()

Return the file path of the lens open in the OpticStudio window.

Returns:

The lens file path. An unsaved system returns an empty string.

Return type:

str

Raises:

OpticStudioError – The session is already closed.

version() → dict[str, str | int]

Return version details for the connected OpticStudio.

Returns:

Product, file, and ZOS-API version fields, plus the license and connection mode of this window.

Return type:

dict[str, str | int]

Raises:

OpticStudioError – The session is already closed, or OpticStudio cannot answer.

SUPPORTED_RELEASE: str = '2024 R2'

OpticStudio release this server supports.

Failure while locating or using OpticStudio.

exception OpticStudioError

Bases: Exception

OpticStudio could not be located or used.