ZOS-API Python: How to deal with AttributeError and apply the correct enumeration

  • 19 March 2020
  • 6 replies
  • 350 views

Userlevel 4
Badge +2
I was running very simple code in python: 

TheMFE=TheSystem.MFE
TheMFE.GetOperandAt(1).Value

But I got error message: 

AttributeError: '<win32com.gen_py.ZOSAPI_Interfaces.IMFERow instance at 0x1627391037392>' object has no attribute 'Value'

l confirmed that the Property 'Value' exits when I checked ZOS-API Syntax Help:



I also want to config DataType setting as StrehlRation for RMSField analysis, but I cannot make the enumeration correct with python. 


6 replies

Userlevel 4
Badge +2

Helpfile documentation as well as the syntax being used by C#/Matlab are the same, they are based on .NET.

However, when using python, we connect python with the windows application(OpticStudio) through a module named pywin32,  which is a package for Windows Extension, can provides access to many of the Windows APIs from Python. There is ‘translated’ work being done for interfaces, classes, objects and methods when connection being established using COM (or Python). And because of this, there are small part of attributes name are not mapped quite well, just as the above example shown (the upper and lower case for 'V' is not matched when compared with helpfile documentation). We are now making efforts to transit to a .NET package to eliminate the discrepancy.


For now, if Attribute error appears and you have no idea what's the correct attribute names, _prop_map_get.keys() can help, for example, by listing keys name, attribute name used in python is lower case 'value'  here.  


>>>TheMFE=TheSystem.MFE
>>>Operand1=TheMFE.GetOperandAt(1)
>>>Operand1._prop_map_get_.keys()
dict_keys(['Contribution', 'ContributionCell', 'IsActive', 'OperandNumber', 'RowColor', 'Target', 'TargetCell', 'ValueCell', 'Weight', 'WeightCell', 'type', 'typeName', 'value'])

Code completion can help to navigate the method:



Enumeration in python being imported as an object named 'constants'. So the way of naming is different from that being used in C#/Matlab. The following code can generate a excel table include all enumerations in python, you may check the table when you got errors indicate that you are using incorrect enumeration. 


from win32com.client import constants
from win32com.client import gencache
gencache.EnsureModule('{EA433010-2BAC-43C4-857C-7AEAC4A8CCE0}', 0, 1, 0)
gencache.EnsureModule('{F66684D7-AAFE-4A62-9156-FF7A7853F764}', 0, 1, 0)
a=constants.__dict__['__dicts__'][0]
string=a.keys().__str__().split(", ",-1)
# the path below should be changed to reflect your situation
fo=open("C:\\Users\\julia.zhang\\Desktop\\Attribute_name.csv","w")
for i in range(1,len(string)-3):
fo.write("constants." + string[i].strip("'") + "\n")
fo.close()


Userlevel 7
Badge +3
Hey Julia,



My conclusion from this is that pywin32 introduces a case sensitivity that it not inherent in ZOS-API. Is that correct?
Userlevel 6
Badge +2
Hi Mark,



Most programming languages, such as C++, Matlab, and Python, are case sensitive. So the ZOS-API should also be considered case sensitive. For example, the Matlab code below uses "value" instead of the API-specific "Value". 

 

TheSystem = TheApplication.PrimarySystem;
TheMFE = TheSystem.MFE;
val1 = TheMFE.GetOperandAt(3).value;



When I run this, I will get the error "No appropriate method, property, or field 'value' for class 'ZemaxUI.ZOSAPI.Editors.ZOSAPI_MFERow'." This is similar to an attribute error in Python.



The problem that Julia is describing is this: we put together the ZOS-API using the .NET interface. This means that any program connecting to the API through .NET (Matlab, C++, C#, Mathematica) will have the ZOS-API package directly installed. The syntax of the properties ("Value", "MFE", "PrimarySystem", etc.) will match the ZOS-API syntax guide exactly.



Python, on the other hand, is connected to the ZOS-API through the COM interface. Because of this, we have to use pywin32 to translate the API commands into the COM language. During this translation, the syntax of some of the properties may be carried over to COM incorrectly. For example, in OpticStudio 20.1 and 20.1.1, Python uses "value" instead of "Value". So, it's not so much that pywin is introducing case sensitivity; rather, it's introducing incorrectly formatted keywords.  



Julia has put together a brilliant way to check if the Python attribute error is caused by one of those incorrectly formatted keywords, or by some other problem in the code. I'm excited to put this code to use!
Userlevel 7
Badge +3
Thank you very much both Julia and Allie! I hadn't appreciated that. Again, we need that LIKE button 
Badge +2

In sample python code that I believe was bundled with a release I installed on July 31st of 2019, there were references to win32com.  


In the sample python code that I have dated September 14, 202, there are references to neither win32com nor pywin32.


What has changed?  And where might I find a list of modules required for interfacing to the ZOS-API from python?

Userlevel 5
Badge +2

Hello Steve,


Thanks for your comment here!


Starting from Zemax OpticStudio 20.2, released on 18 May 2020, we moved from the COM connection with the pywin32 module to the .NET framework. Since the ZOS-API is written using the .NET framework, using languages that can directly communicate with .NET will give the most flexibility and the best performance. Connection via COM with the pywin32 module is still supported, but we recommend to use Python with the .NET framework.


Regarding the required modules, installation, and the differences between the two connection methods, please check out the following knowledgebase articles and forum threads:


Getting started with Python


Python installation troubleshooting


ZOS-API using Python.NET


What is the difference between Python COM and Python .NET?


If you have further questions, please let us know and we will be happy to help!


Best,


Csilla

Reply