This article demonstrates how to connect to the ZOS-API with the Interactive Extension using Python. Once connected to OpticStudio, an Interactive Extension can perform any task that a User Extension is capable of and the OpticStudio Interface will interactively display the result of that task.
Authored By Sandrine Auriol
The Interactive Extension mode is almost identical to a User Extension except an Interactive Extension does not have to be a standalone executable. This capability allows connections from scripting environments such as Matlab or Python where there is no compiled executable that OpticStudio can launch.
The Interactive Extension interactively controls the OpticStudio Interface from external scripting environment. That article will explain how to connect OpticStudio with the Interactive Extension.
To create a boilerplate template to connect to the Interactive Extension, click Programming...Python...Interactive Extension.
The script will be placed in the ZOS-API Project Folder: ..\Documents\Zemax\ZOS-API Projects\PythonZOSConnection
If more than one Interactive Extension is created, then the file names will be sequentially numbered.
The software will open the File Explorer at the file location:
To start the Interactive Extension, click Programming...Interactive Extension.
A dialog window will open, and OpticStudio will wait for an external application, in this case Python, to connect.
The dialog box shows the instance ID for the running instance of OpticStudio. This allows Python to connect to a specific instance of OpticStudio if more than one instance is running.
The next step depends on the Integrated Development Environment (IDE) being used.
IDLE (Native IDE that comes with Python)
- Right click on the script file
- Select “Edit in the IDLE”, “Edit with IDLE” Python version
- Note: If this option does not appear, you can use this external guide to find IDLE within the Python folder: "How to Make IDLE the Default Editor for Python Files on Windows."
- Run the code
- The status of the Interactive Extension shows the extension is now connected.
- Individual commands can now be executed in the Python Command Window.
For example, a sample file can be loaded:
- The open instance of OpticStudio will display the loaded file and the Python command window will return the Boolean value True as the file has been successfully loaded.
- Once the command has been executed, the shell displays “>>>” meaning that it is ready for a new command.
- In PyCharm open the code of PythonZOSConnectionX.py.
- Right click and select “Run File in Console”:
- The Console window will indicate the status of the connection. Then individual commands can be executed in the Console.
The right pane displays the variables available for the project.
It means that there is an “Intellisense” when working with the Interactive Extension.
Blocks of codes can also be executed. For example, open the attached file. This script is an extract from the sample file "\Zemax\ZOS-API Sample Code\Python\PythonStandalone_01_new_file_and_quickfocus.py". The lines of code related to the connection to ZOS-API have been removed.
Select the script. Right-click and select “Execute Selection in Console”.
OpticStudio displays the newly created file.
And the Console shows the new variables:
The connection can be terminated either by clicking Terminate in the Interactive Extension dialog box
or by running the CloseApplication method in the Python Command Window