Skip to main content
Solved

surface opacity setting via ZOS-API and Python


Benjamin.N
Forum|alt.badge.img

Hi community,

I experienced some unexpected behaviour with ZOS-API (Zemax 2024 R1.00) and Python (3.11).

I tried to set the surface opacity of a surface in a sequential system via ZOS-API, but it did not work; the surface opacity maintained its default value of 100 %. The following code

app = ZOSAPI.ZOSAPI_Connection().CreateNewApplication()

lde = app.PrimarySystem.LDE
lde.InsertNewSurfaceAt(2)
surface2 = lde.GetSurfaceAt(2)

surface2.TypeData.SurfaceOpacity = ZOSAPI.Common.ZemaxOpacity.P50
print(str(surface2.TypeData.SurfaceOpacity))

yields “P100”, indicating that the surface opacity was not affected.

When I tried to find the source of the problem I noticed that the surface opacity is somehow affected by the setting of the surface color. To investigate, I set all possible combinations of surface color and surface opacity and subsequently read the actual values color_get and opacity_get (full code at end of post):

Show content
    color_set color_get opacity_set opacity_get
0     Default   Default         P00        P100
1     Default   Default         P10        P100
2     Default   Default         P20        P100
3     Default   Default         P30        P100
4     Default   Default         P40        P100
5     Default   Default         P50        P100
6     Default   Default         P60        P100
7     Default   Default         P70        P100
8     Default   Default         P80        P100
9     Default   Default         P90        P100
10    Default   Default        P100        P100
11    NoColor   NoColor         P00          25
12    NoColor   NoColor         P10          25
13    NoColor   NoColor         P20          25
14    NoColor   NoColor         P30          25
15    NoColor   NoColor         P40          25
16    NoColor   NoColor         P50          25
17    NoColor   NoColor         P60          25
18    NoColor   NoColor         P70          25
19    NoColor   NoColor         P80          25
20    NoColor   NoColor         P90          25
21    NoColor   NoColor        P100          25
22     Color1    Color1         P00         P90
23     Color1    Color1         P10         P90
24     Color1    Color1         P20         P90
25     Color1    Color1         P30         P90
26     Color1    Color1         P40         P90
27     Color1    Color1         P50         P90
28     Color1    Color1         P60         P90
29     Color1    Color1         P70         P90
30     Color1    Color1         P80         P90
31     Color1    Color1         P90         P90
32     Color1    Color1        P100         P90
33     Color2    Color2         P00         P80
34     Color2    Color2         P10         P80
35     Color2    Color2         P20         P80
36     Color2    Color2         P30         P80
37     Color2    Color2         P40         P80
38     Color2    Color2         P50         P80
39     Color2    Color2         P60         P80
40     Color2    Color2         P70         P80
41     Color2    Color2         P80         P80
42     Color2    Color2         P90         P80
43     Color2    Color2        P100         P80
44     Color3    Color3         P00         P70
45     Color3    Color3         P10         P70
46     Color3    Color3         P20         P70
47     Color3    Color3         P30         P70
48     Color3    Color3         P40         P70
49     Color3    Color3         P50         P70
50     Color3    Color3         P60         P70
51     Color3    Color3         P70         P70
52     Color3    Color3         P80         P70
53     Color3    Color3         P90         P70
54     Color3    Color3        P100         P70
55     Color4    Color4         P00         P60
56     Color4    Color4         P10         P60
57     Color4    Color4         P20         P60
58     Color4    Color4         P30         P60
59     Color4    Color4         P40         P60
60     Color4    Color4         P50         P60
61     Color4    Color4         P60         P60
62     Color4    Color4         P70         P60
63     Color4    Color4         P80         P60
64     Color4    Color4         P90         P60
65     Color4    Color4        P100         P60
66     Color5    Color5         P00         P50
67     Color5    Color5         P10         P50
68     Color5    Color5         P20         P50
69     Color5    Color5         P30         P50
70     Color5    Color5         P40         P50
71     Color5    Color5         P50         P50
72     Color5    Color5         P60         P50
73     Color5    Color5         P70         P50
74     Color5    Color5         P80         P50
75     Color5    Color5         P90         P50
76     Color5    Color5        P100         P50
77     Color6    Color6         P00         P40
78     Color6    Color6         P10         P40
79     Color6    Color6         P20         P40
80     Color6    Color6         P30         P40
81     Color6    Color6         P40         P40
82     Color6    Color6         P50         P40
83     Color6    Color6         P60         P40
84     Color6    Color6         P70         P40
85     Color6    Color6         P80         P40
86     Color6    Color6         P90         P40
87     Color6    Color6        P100         P40
88     Color7    Color7         P00         P30
89     Color7    Color7         P10         P30
90     Color7    Color7         P20         P30
91     Color7    Color7         P30         P30
92     Color7    Color7         P40         P30
93     Color7    Color7         P50         P30
94     Color7    Color7         P60         P30
95     Color7    Color7         P70         P30
96     Color7    Color7         P80         P30
97     Color7    Color7         P90         P30
98     Color7    Color7        P100         P30
99     Color8    Color8         P00         P20
100    Color8    Color8         P10         P20
101    Color8    Color8         P20         P20
102    Color8    Color8         P30         P20
103    Color8    Color8         P40         P20
104    Color8    Color8         P50         P20
105    Color8    Color8         P60         P20
106    Color8    Color8         P70         P20
107    Color8    Color8         P80         P20
108    Color8    Color8         P90         P20
109    Color8    Color8        P100         P20
110    Color9    Color9         P00         P10
111    Color9    Color9         P10         P10
112    Color9    Color9         P20         P10
113    Color9    Color9         P30         P10
114    Color9    Color9         P40         P10
115    Color9    Color9         P50         P10
116    Color9    Color9         P60         P10
117    Color9    Color9         P70         P10
118    Color9    Color9         P80         P10
119    Color9    Color9         P90         P10
120    Color9    Color9        P100         P10
121   Color10   Color10         P00         P00
122   Color10   Color10         P10         P00
123   Color10   Color10         P20         P00
124   Color10   Color10         P30         P00
125   Color10   Color10         P40         P00
126   Color10   Color10         P50         P00
127   Color10   Color10         P60         P00
128   Color10   Color10         P70         P00
129   Color10   Color10         P80         P00
130   Color10   Color10         P90         P00
131   Color10   Color10        P100         P00
132   Color11   Color11         P00          11
133   Color11   Color11         P10          11
134   Color11   Color11         P20          11
135   Color11   Color11         P30          11
136   Color11   Color11         P40          11
137   Color11   Color11         P50          11
138   Color11   Color11         P60          11
139   Color11   Color11         P70          11
140   Color11   Color11         P80          11
141   Color11   Color11         P90          11
142   Color11   Color11        P100          11
143   Color12   Color12         P00          12
144   Color12   Color12         P10          12
145   Color12   Color12         P20          12
146   Color12   Color12         P30          12
147   Color12   Color12         P40          12
148   Color12   Color12         P50          12
149   Color12   Color12         P60          12
150   Color12   Color12         P70          12
151   Color12   Color12         P80          12
152   Color12   Color12         P90          12
153   Color12   Color12        P100          12
154   Color13   Color13         P00          13
155   Color13   Color13         P10          13
156   Color13   Color13         P20          13
157   Color13   Color13         P30          13
158   Color13   Color13         P40          13
159   Color13   Color13         P50          13
160   Color13   Color13         P60          13
161   Color13   Color13         P70          13
162   Color13   Color13         P80          13
163   Color13   Color13         P90          13
164   Color13   Color13        P100          13
165   Color14   Color14         P00          14
166   Color14   Color14         P10          14
167   Color14   Color14         P20          14
168   Color14   Color14         P30          14
169   Color14   Color14         P40          14
170   Color14   Color14         P50          14
171   Color14   Color14         P60          14
172   Color14   Color14         P70          14
173   Color14   Color14         P80          14
174   Color14   Color14         P90          14
175   Color14   Color14        P100          14
176   Color15   Color15         P00          15
177   Color15   Color15         P10          15
178   Color15   Color15         P20          15
179   Color15   Color15         P30          15
180   Color15   Color15         P40          15
181   Color15   Color15         P50          15
182   Color15   Color15         P60          15
183   Color15   Color15         P70          15
184   Color15   Color15         P80          15
185   Color15   Color15         P90          15
186   Color15   Color15        P100          15
187   Color16   Color16         P00          16
188   Color16   Color16         P10          16
189   Color16   Color16         P20          16
190   Color16   Color16         P30          16
191   Color16   Color16         P40          16
192   Color16   Color16         P50          16
193   Color16   Color16         P60          16
194   Color16   Color16         P70          16
195   Color16   Color16         P80          16
196   Color16   Color16         P90          16
197   Color16   Color16        P100          16
198   Color17   Color17         P00          17
199   Color17   Color17         P10          17
200   Color17   Color17         P20          17
201   Color17   Color17         P30          17
202   Color17   Color17         P40          17
203   Color17   Color17         P50          17
204   Color17   Color17         P60          17
205   Color17   Color17         P70          17
206   Color17   Color17         P80          17
207   Color17   Color17         P90          17
208   Color17   Color17        P100          17
209   Color18   Color18         P00          18
210   Color18   Color18         P10          18
211   Color18   Color18         P20          18
212   Color18   Color18         P30          18
213   Color18   Color18         P40          18
214   Color18   Color18         P50          18
215   Color18   Color18         P60          18
216   Color18   Color18         P70          18
217   Color18   Color18         P80          18
218   Color18   Color18         P90          18
219   Color18   Color18        P100          18
220   Color19   Color19         P00          19
221   Color19   Color19         P10          19
222   Color19   Color19         P20          19
223   Color19   Color19         P30          19
224   Color19   Color19         P40          19
225   Color19   Color19         P50          19
226   Color19   Color19         P60          19
227   Color19   Color19         P70          19
228   Color19   Color19         P80          19
229   Color19   Color19         P90          19
230   Color19   Color19        P100          19
231   Color20   Color20         P00          20
232   Color20   Color20         P10          20
233   Color20   Color20         P20          20
234   Color20   Color20         P30          20
235   Color20   Color20         P40          20
236   Color20   Color20         P50          20
237   Color20   Color20         P60          20
238   Color20   Color20         P70          20
239   Color20   Color20         P80          20
240   Color20   Color20         P90          20
241   Color20   Color20        P100          20
242   Color21   Color21         P00          21
243   Color21   Color21         P10          21
244   Color21   Color21         P20          21
245   Color21   Color21         P30          21
246   Color21   Color21         P40          21
247   Color21   Color21         P50          21
248   Color21   Color21         P60          21
249   Color21   Color21         P70          21
250   Color21   Color21         P80          21
251   Color21   Color21         P90          21
252   Color21   Color21        P100          21
253   Color22   Color22         P00          22
254   Color22   Color22         P10          22
255   Color22   Color22         P20          22
256   Color22   Color22         P30          22
257   Color22   Color22         P40          22
258   Color22   Color22         P50          22
259   Color22   Color22         P60          22
260   Color22   Color22         P70          22
261   Color22   Color22         P80          22
262   Color22   Color22         P90          22
263   Color22   Color22        P100          22
264   Color23   Color23         P00          23
265   Color23   Color23         P10          23
266   Color23   Color23         P20          23
267   Color23   Color23         P30          23
268   Color23   Color23         P40          23
269   Color23   Color23         P50          23
270   Color23   Color23         P60          23
271   Color23   Color23         P70          23
272   Color23   Color23         P80          23
273   Color23   Color23         P90          23
274   Color23   Color23        P100          23
275   Color24   Color24         P00          24
276   Color24   Color24         P10          24
277   Color24   Color24         P20          24
278   Color24   Color24         P30          24
279   Color24   Color24         P40          24
280   Color24   Color24         P50          24
281   Color24   Color24         P60          24
282   Color24   Color24         P70          24
283   Color24   Color24         P80          24
284   Color24   Color24         P90          24
285   Color24   Color24        P100          24

 

 

Apparently, the surface opacity is only affected by the surface color setting, but not by the surface opacity setting. For some color settings, the surface opacity assumes invalid values that do not start with “P”.

Has anyone experienced a similar issue? Did I make an error in my use of ZOS-API?

Best regards
Benjamin

full code:
 

Show content
import os
import winreg
from itertools import product
from shutil import get_terminal_size

import clr
import numpy as np
import pandas as pd

reg_key = winreg.OpenKey(winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER), r"Software\Zemax", 0, winreg.KEY_READ)
zemax_data = winreg.QueryValueEx(reg_key, 'ZemaxRoot')
winreg.CloseKey(reg_key)

clr.AddReference(os.path.join(os.sep, zemax_data[0], r'ZOS-API\Libraries\ZOSAPI_NetHelper.dll'))  # noqa
import ZOSAPI_NetHelper  # noqa

PATH_ZEMAX = ZOSAPI_NetHelper.ZOSAPI_Initializer.GetZemaxDirectory()
clr.AddReference(os.path.join(os.sep, PATH_ZEMAX, "ZOSAPI.dll"))  # noqa
clr.AddReference(os.path.join(os.sep, PATH_ZEMAX, "ZOSAPI_Interfaces.dll"))  # noqa
import ZOSAPI  # noqa

if __name__ == '__main__':
    app = ZOSAPI.ZOSAPI_Connection().CreateNewApplication()

    lde = app.PrimarySystem.LDE
    lde.InsertNewSurfaceAt(2)
    surface2 = lde.GetSurfaceAt(2)

    colors = ['Default', 'NoColor'] + [f"Color{_}" for _ in range(1, 25)]
    opacities = [f"P{_:02d}" for _ in range(0, 110, 10)]

    colors_get, opacities_get = [], []
    tuples = list(product(colors, opacities))
    for color_set, opacity_set in tuples:
        surface2.TypeData.SurfaceColor = getattr(ZOSAPI.Common.ZemaxColor, color_set)
        surface2.TypeData.SurfaceOpacity = getattr(ZOSAPI.Common.ZemaxOpacity, opacity_set)
        opacities_get.append(str(surface2.TypeData.SurfaceOpacity))
        colors_get.append(str(surface2.TypeData.SurfaceColor))

    df = pd.DataFrame({key: val for key, val in zip(["color_set", "color_get", "opacity_set", "opacity_get", ],
                                                    [np.array(tuples).T[0], colors_get, np.array(tuples).T[1],
                                                     opacities_get, ])})

    # df["color set = get"] = df.apply(lambda row: row["color_set"] == row["color_get"], axis=1)
    # df["opacity set = get"] = df.apply(lambda row: row["opacity_set"] == row["opacity_get"], axis=1)
    with pd.option_context('display.max_rows', None,
                           'display.max_columns', None,
                           'display.width', get_terminal_size()[0] * 3):
        print(df)
        # print()
        # print(df[df["opacity set = get"]])

 

 

Best answer by Ethan

Hello @Benjamin.N and @David.Nguyen ,

 

It turns out that this is a bug. You are able to set the value of the SurfaceOpacity as you’d expect. If you save the file as a ZMX from your Python code, you will see that the setting applied correctly. However, when you attempt to retrieve the value for SurfaceOpacity, it is actually returning the value for SurfaceColor. This clearly isn’t right. So, to summarize, you can set the value, but you cannot trust the value returned by SurfaceOpacity.

I will report this as a bug to our development team so that they can dig into why the values are confused.

Please let me know if you need further clarification on this issue.

 

Thanks,
Ethan

View original
Did this topic help you find an answer to your question?

2 replies

David.Nguyen
Luminary
Forum|alt.badge.img+2
  • Luminary
  • 1089 replies
  • January 23, 2024

I observe the same with R2.00 and Python 3.11.4.


Ethan
En-Lightened
Forum|alt.badge.img+2
  • En-Lightened
  • 107 replies
  • Answer
  • February 2, 2024

Hello @Benjamin.N and @David.Nguyen ,

 

It turns out that this is a bug. You are able to set the value of the SurfaceOpacity as you’d expect. If you save the file as a ZMX from your Python code, you will see that the setting applied correctly. However, when you attempt to retrieve the value for SurfaceOpacity, it is actually returning the value for SurfaceColor. This clearly isn’t right. So, to summarize, you can set the value, but you cannot trust the value returned by SurfaceOpacity.

I will report this as a bug to our development team so that they can dig into why the values are confused.

Please let me know if you need further clarification on this issue.

 

Thanks,
Ethan


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings