Solved

import BatchRayTrace in ipython

  • 6 September 2023
  • 5 replies
  • 101 views

Badge +1

Hello!

I’m attempting to use the `zosapi` package with @MichaelH ‘s RayTrace.dll for fast batch ray tracing in Python. It works just fine when I call Python scripts from some terminal, but am having some difficulty using it in Jupyter/IPython notebooks.

Below shows where the kernel crashes, without giving me a stack trace to try and diagnose why the kernel is crashing. Has anyone encountered this problem before?

 

icon

Best answer by MichaelH 7 September 2023, 00:33

View original

5 replies

Userlevel 6
Badge +2

Hi Jaren,

I just ran the following code with Python 3.8.10 & pythonnet 3.0.1 and I got no errors:

import numpy as np, matplotlib.pyplot as plt, zosapi, clr, os

dll = r'c:\temp\RayTrace.dll'
print(clr.AddReference(dll))

import BatchRayTrace

print('finished')

The print output is:

The two things that I can think of without more information is:

  1. Make sure the RayTrace.dll that you downloaded from the Knowledge Base is “unblocked”
    • Right click the RayTrace.dll and select Properties
    • In the Attributes section, you should see “Read-only” and “Hidden” checkboxes.  If you see a Security section with an “Unblock” checkbox, click this box and hit OK
  2. Try running your script directly in Python from the command line and not through the (Jupyter) notebook
    • Sometimes IDEs have nuances in their site-packages modules downloaded from PIP.  By ensuring that RayTrace.dll can load in pure Python, you can narrow down if it’s an issue with your Python environment or the file itself.

 

Userlevel 6
Badge +2

Quick followup:

If you’re using Jupyter, for some reason, you need to establish a connection to OpticStudio before importing the BatchRayTrace dll.  So the following code works in a Jupyter notebook:

import numpy as np, matplotlib.pyplot as plt, zosapi, clr, os

zos = zosapi.App()

dll = r'c:\temp\RayTrace.dll'
print(clr.AddReference(dll))

import BatchRayTrace

print('finished')

 

Badge +1

Hi Michael,

Thanks for your help! The establishment of a connection to OpticStudio in jupyter worked like a charm, thank you for the fix. 

 

-Jaren

Userlevel 7
Badge +3

Hey Michael,

Tut tut tut...you’ll be banished from the Kingdom of Python if your import has to be done at a specific location 😂

  • Mark
Userlevel 6
Badge +2

Ha Mark, I blame Jupyter’s python kernel.  In pure python it’s implemented correctly and you can use import wherever you want.

Also, it appears that it’s an issue with the RayTrace.dll, not the zosapi PyPi module; it’s more of a C# logic and not Python logic.  So you need to take it up with whomever wrote & compiled the RayTrace.dll. I heard it was someone with the initials MH, but I just don’t know who that could be.  That MH will probably also be banished from the Kingdom of C# 😲.

Reply