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:
- 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
valuesthen replace those defaults. Omitvaluesto keep every default. Operands from the wizard’s Start At row through the end of the editor are replaced.valueandcontributionare 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
settingsin the result. A glass or air thickness turns that boundary on when its checkbox is omitted.configurationselects one configuration, andfieldselects one field, when the matching “all” checkbox is omitted.- Returns:
settingsandoperands.settingsare the wizard controls that were applied. Each operand has the same fields asmerit_function().- Return type:
- 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.
valueandcontributionon each operand are the numbers from this calculation.- Returns:
meritandoperands.meritisNonewhen OpticStudio does not return a finite value. Each operand has the same fields asmerit_function().- Return type:
- 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
functionpropagates 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.
Nonemeans Zemax’s helper locates OpticStudio, thenOPTICSTUDIO_DIRECTORYis 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_numberis the on-axis value at the primary wavelength.total_lengthruns 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
Nonewhen OpticStudio does not return a finite value.statusis empty when the update reports nothing.- Return type:
- Raises:
OpticStudioError – The session is already closed, or OpticStudio cannot answer.
- 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.
Nonereads 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
thicknessandthickness_solve.tceandtce_solveare omitted when the surface does not use the TCE column.- Return type:
- Raises:
ValueError –
surfaceis outside the editor.OpticStudioError – The session is already closed, or OpticStudio cannot answer.
- 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:
- 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.
Nonereads every operand.- Returns:
Every operand, or the one requested operand. Each operand includes the parameter columns its type uses.
target,weight,value, andcontributionare omitted when the operand does not use them.- Return type:
- Raises:
ValueError –
operandis outside the editor.OpticStudioError – The session is already closed, or OpticStudio cannot answer.
- 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.
Nonereads every operand.- Returns:
The current configuration, the configuration columns, and the operands. With
operand, that operand.- Return type:
- Raises:
ValueError –
operandis outside the editor.OpticStudioError – The session is already closed, or OpticStudio cannot answer.
- 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_changesis 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:
- Raises:
ValueError – The lens has unsaved changes and
discard_changesis 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:
- Returns:
The algorithm, cycle count, number of variables and targets, and the merit function before and after the run. A merit value is
Nonewhen OpticStudio does not return a finite number.- Return type:
- 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
pathto 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.
Nonesaves the current file.- Returns:
The lens identity after the save. The fields are the same as those returned by
lens_file().- Return type:
- Raises:
ValueError – The lens has no current file,
pathis 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.
addinserts a surface atnumber. Surfaces at that number and after move up.- Parameters:
action (str) –
change,add, orremove.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().radiusandthicknessacceptInfinityfor an infinite length. A radius of 0 is returned asInfinity. A thickness of 0 is zero.thicknessandthickness_solveare rejected on the image surface.tceandtce_solveare rejected when the surface does not use the TCE column.typechanges the surface type first.parametersmaps column names such asPar1to a value or tovalueandsolve.radius_solveand the other cell solves set that cell’s solve.
- Returns:
The surfaces after the change.
- Return type:
- 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.
addinserts an operand atnumber. Operands at that number and after move up.- Parameters:
action (str) –
change,add, orremove.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().typechanges the operand type first.parametersmaps column names such asParam1to new values.targetandweightare rejected when the operand does not use them.
- Returns:
The operands after the change.
- Return type:
- 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.
addinserts atnumber. Following operands or configurations move up.- Parameters:
section (str) –
operandorconfiguration.action (str) –
change,add,remove, ormake_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().typechanges the operand type first.parametersmapsParam1,Param2, orParam3to an integer.cellsmaps a configuration number to a value or tovalueandsolve. For a new configuration,copycopies the current configuration’s cells.
- Returns:
The editor after the change.
- Return type:
- 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
valuesare written. Field points and the wavelength list are rows and stay unchanged.- Parameters:
name (str) – Setting group. The same names as
system_data().values (dict[str, object]) – Keys to change. The fields are the same as those returned by
system_data()for that group.
- Returns:
The setting group after the change.
- Return type:
- Raises:
ValueError –
nameis unknown,valuesis 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.
addappends a row.make_primaryselects the primary wavelength.- Parameters:
- Returns:
The rows after the change. Field rows are the field points.
- Return type:
- Raises:
ValueError –
namehas 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.
hxandhyare the normalized field coordinates.pxandpyare the normalized pupil coordinates.trace_typeselects the table shown in that window.- Parameters:
- Returns:
The ray settings and one row per surface.
columnsare the table headings.valuesfollow those headings. A missing number, such as an infinite object thickness, isNone.messageis empty when the table has no error line.- Return type:
- 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, orray_aiming.- Returns:
The group values. Wavelengths are a list. The other groups are dictionaries.
- Return type:
- Raises:
ValueError –
nameis not a setting group.OpticStudioError – The session is already closed, or OpticStudio cannot answer.
- 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:
- Raises:
OpticStudioError – The session is already closed.
Failure while locating or using OpticStudio.