Solved

Python NSC Examples failing with AttributeError

  • 5 October 2022
  • 6 replies
  • 394 views

Userlevel 1

I've tried running the example file PythonStandalone_02_NSC_ray_trace.py and realized that it fails when trying to read out ObjectData:

numXPixels = obj.ObjectData.NumberXPixels;

Result: 

AttributeError: 'IObject' object has no attribute 'NumberXPixels'.

The code runs flawlessly, except when reading ObjectData. I have spent a full day trying to debug this error without success.

I have also tried the methods outlined in 

PPG example not working | Zemax Community

and

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

without success. 

Does anyone know how to continue debugging?

Note: using Zemax OpticStudio 22.2.1

icon

Best answer by svilches 6 October 2022, 13:11

View original

6 replies

Userlevel 6
Badge +2

Can you confirm the fly's eye homogenizer is unmodified and Object 4 is actually a Detector Rectangle?  Line 185 in the example is hard-coded to point to Object 4 and this object in the unmodified file is a Detector Rectangle.  You will typically get this error if you're trying to read a parameter (NumberXPixels) on an object which doesn't have that parameter.

Userlevel 1

Hi @MichaelH, thank you for pushing me to double check. Unfortunately, the .zmx is unmodified, and object 4 is a Detector Rectangle, as expected:

Furthermore, I have found a similar issue with all the non-sequential examples that try to modify a property.

Maybe some useful information: obj.ObjectData returns an IObject, and not an IObjectDetectorRectanglewhich is what I'd expect (although I'm new to the API, so I might be easily wrong). Of course, an IObject, being a general object, does not have the NumberXPixels attribute:

I hope this helps!

Userlevel 7
Badge +2

Hi svilches,

 

The example works for me in 22.2.1.

This is the result I got in Spyder

You are saying:

Unfortunately, the .zmx is unmodified

 

However, the code uses the *.ZOS file (this is the new file extension):

    #! [e02s01_py]
# Open file
testFile = os.path.join(os.sep, sampleDir, r'Non-sequential\Miscellaneous\Digital_projector_flys_eye_homogenizer.zos')

Could you check this file as well? I’m attaching my Digital_projector_flys_eye_homogenizer.zos file for which the code works.

Lastly, try rebooting the computer, it does wonder sometimes for me at least.

Take care,

 

David

Userlevel 1

I have found the issue: The sample code does not work with the latest pythonnet (3.0.0.post1)! Re-installing it with an older version (pythonnet 2.5.2) solved it.

There is still an issue with this example, but I’m not able to install the older pythonnet version. There is no pre-build wheel for pythonnet 2.5.2 for my python version (3.12) and building from source fails.

Does anybody know whether there is any effort made to fix the problems with pythonnet 3.x.x?

Userlevel 7
Badge +2

@Tilmann.Piffl,

 

It’s mostly fixed if you read the following:

However, the examples have not been modified to take this into account yet as far as I know.

Take care,

 

David

Reply