My codes (that were working with the 2024 version) do not work anymore with the 2025 release. This is the error I get:
Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'ZemaxEngine.dll' or one of its dependencies. A dynamic link library (DLL) initialization routine failed. (Exception from HRESULT: 0x8007045A).
You can also have multiple versions of OpticStudio installed on your computer so you can install OpticStudio 2024R2 to use your code and OpticStudio 2025R1 with the latest features for your other work.
I am using the latest version of zospy, but when I want to obtain the Zernike coefficients:
I get the same exception from HRESULT: 0x8007045A:
Unhandled Exception: Python.Runtime.InternalPythonnetException: Failed to create Python type for ZemaxUI.ZOSAPI.Analysis.Aberrations.AS_ZernikeStandardCoefficients ---> Python.Runtime.InternalPythonnetException: Failed to create Python type for ZemaxUI.ZOSAPI.Analysis.AS_Base ---> System.IO.FileLoadException: Could not load file or assembly 'ZemaxEngine.dll' or one of its dependencies. A dynamic link library (DLL) initialization routine failed. (Exception from HRESULT: 0x8007045A) at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType) at System.Signature..ctor(IRuntimeFieldInfo fieldHandle, RuntimeType declaringType) at System.Reflection.RtFieldInfo.get_FieldType() at System.Reflection.RuntimeFieldInfo.ToString() at Python.Runtime.MaybeMemberInfo`1..ctor(T fi) at Python.Runtime.ClassManager.GetClassInfo(Type type, ClassBase impl) at Python.Runtime.ClassManager.InitClassBase(Type type, ClassBase impl, ReflectedClrType pyType) at Python.Runtime.ReflectedClrType.GetOrCreate(Type type) --- End of inner exception stack trace --- at Python.Runtime.ReflectedClrType.GetOrCreate(Type type) at Python.Runtime.DefaultBaseTypeProvider.GetBaseType(Type type) at Python.Runtime.DefaultBaseTypeProvider.GetBaseTypes(Type type, IList`1 existingBases) at Python.Runtime.PythonBaseTypeProviderGroup.GetBaseTypes(Type type, IList`1 existingBases) at Python.Runtime.TypeManager.GetBaseTypeTuple(Type clrType) at Python.Runtime.TypeManager.InitializeClassCore(Type clrType, PyType pyType, ClassBase impl) at Python.Runtime.ReflectedClrType.GetOrCreate(Type type) --- End of inner exception stack trace --- at Python.Runtime.ReflectedClrType.GetOrCreate(Type type) at Python.Runtime.CLRObject.GetReference(Object ob) at Python.Runtime.PyObject.FromManagedObject(Object ob) at Python.Runtime.PyObjectConversions.TryEncode(Object obj, Type type) at Python.Runtime.Converter.ToPython(Object value, Type type) at Python.Runtime.MethodBinder.Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase info, MethodBaser] methodinfo) at Python.Runtime.MethodObject.Invoke(BorrowedReference target, BorrowedReference args, BorrowedReference kw, MethodBase info) at Python.Runtime.MethodBinding.tp_call(BorrowedReference ob, BorrowedReference args, BorrowedReference kw)
The python kernel dies and is restarted. In Zemax, the Zernike window is open, the connection to python is lost, and I can just click on update and I get the coefficients.
python version 3.13.2,
Zemax: 2025 R1.00
Doing different things works, so it’s not overall broken.
Simple question but is the GUI instance you’re running also 25R1.00? If you’re trying to connect as an extension (say 25R1.00) but you’re connecting with a different version of OpticStudio (say 24R2), you can get a runtime error. Does the script work if you simply connect as a Standalone Application and not an Interactive Extension?
Also, it looks like the error is happening with setting the Field. I don’t know how ZOSPy is setup if no field is passed (does it try to set it to 1 or does it not set it at all), so can you try to remove the field=1 and see if it works?
I have just one version installed (I uninstalled the old one before I installed the current). I even let Zemax recreate the Documents/ZEMAX folder, to avoid having files from previous versions (however, I copied back my projects).
In standalone mode, the kernel is also dying but it does not give an error message.
I went through the code, and it defaults the field to 1.
Tomorrow, I’ll try to use the ZOS-API directly without the zospy implementation and see if I can reproduce the same error.
I tried to use the ZOS-API directly, but after I give up at this point:
It is not possible to change the settings with that (“Surface” I tried to guess). zernike_analysis unfortunately has no properties I can modify directly. And having to create a file to write, change and reload feels a bit wrong, but I certainly misunderstood something.
In the documentation, I found this comment (in example 15):
# MODIFYSETTINGS are defined in ZPL help files: The Programming Tab > About the ZPL > Keywords
In the programming tab, I cannot find the ZPL keywords:
Hey Joachim,
The IAS_ZernikeStandardCoefficient (as well as Annular & Fringe) is hooked up to the ZOS-API:
The following code works for me to get the Zernike Standard Coefficients:
I can now access the Sx, ... values, however the Surface is just read only. Why is there no property for the surface number? Is that the reason why the zospy code crashes?
Hey Michael,
your example helped me a lot to narrow down the problem:
In pycharm (also IDLE), your script works, it also works with my file, it works in extension and interactive mode. Perfect.
To analyze problems, in pycharm I was using the debugging mode, and there it fails:
Unhandled Exception: Python.Runtime.InternalPythonnetException: Failed to create Python type for ZemaxUI.ZOSAPI.Analysis.Aberrations.AS_ZernikeStandardCoefficients ---> Python.Runtime.InternalPythonnetException: Failed to create Python type for ZemaxUI.ZOSAPI.Analysis.AS_Base ---> System.IO.FileLoadException: Could not load file or assembly 'ZemaxEngine.dll' or one of its dependencies. A dynamic link library (DLL) initialization routine failed. (Exception from HRESULT: 0x8007045A) at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType) at System.Signature..ctor(IRuntimeFieldInfo fieldHandle, RuntimeType declaringType) at System.Reflection.RtFieldInfo.get_FieldType() at System.Reflection.RuntimeFieldInfo.ToString() at Python.Runtime.MaybeMemberInfo`1..ctor(T fi) at Python.Runtime.ClassManager.GetClassInfo(Type type, ClassBase impl) at Python.Runtime.ClassManager.InitClassBase(Type type, ClassBase impl, ReflectedClrType pyType) at Python.Runtime.ReflectedClrType.GetOrCreate(Type type) --- End of inner exception stack trace --- at Python.Runtime.ReflectedClrType.GetOrCreate(Type type) at Python.Runtime.DefaultBaseTypeProvider.GetBaseType(Type type) at Python.Runtime.DefaultBaseTypeProvider.GetBaseTypes(Type type, IList`1 existingBases) at Python.Runtime.PythonBaseTypeProviderGroup.GetBaseTypes(Type type, IList`1 existingBases) at Python.Runtime.TypeManager.GetBaseTypeTuple(Type clrType) at Python.Runtime.TypeManager.InitializeClassCore(Type clrType, PyType pyType, ClassBase impl) at Python.Runtime.ReflectedClrType.GetOrCreate(Type type) --- End of inner exception stack trace --- at Python.Runtime.ReflectedClrType.GetOrCreate(Type type) at Python.Runtime.CLRObject.GetReference(Object ob) at Python.Runtime.PyObject.FromManagedObject(Object ob) at Python.Runtime.PyObjectConversions.TryEncode(Object obj, Type type) at Python.Runtime.Converter.ToPython(Object value, Type type) at Python.Runtime.MethodBinder.Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase info, MethodBase[] methodinfo) at Python.Runtime.MethodObject.Invoke(BorrowedReference target, BorrowedReference args, BorrowedReference kw, MethodBase info) at Python.Runtime.MethodBinding.tp_call(BorrowedReference ob, BorrowedReference args, BorrowedReference kw)
and it is the line with GetSettings() that triggers the problem.
When I run the script in Spyder (6.0.5), the kernel just dies without further information. In debug mode, it fails with the message I had in the first place (Exception from HRESULT: 0x8007045A).
Hmm, interesting. Obviously not an ideal situation, but there appears to be a known issue with cpython speedup in debug mode for PyCharm (and I assume other IDE’s in debug mode):
The simple solution is to add the PYDEVD_USE_CYTHON=NO environment variable to the debug configuration.
However, most users won’t know this is an issue with the IDE and won’t know how to fix it .
Yes, I found that issue with pycharm as well, but disabling did not help in this case. I was thinking it might be that the debug mode analysis the variable types and structures, also something that the Spyder-IDE might be doing that in this case does not work great. Unfortunately, I don't see through this code at all.
I finally use jupyter notebooks, there I use an established connection to Zemax. I can see and analyse data, modify my routines. So I have similar flexibility like with Spyder. In the jupyter notebook I can use your code but also the initial code directly from the zospy library.