Skip to main content
Solved

import BatchRayTrace in ipython

  • September 6, 2023
  • 5 replies
  • 140 views

Forum|alt.badge.img+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?

 

Best answer by MichaelH

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')

 

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

5 replies

MichaelH
Ansys Staff
Forum|alt.badge.img+2
  • Ansys Staff
  • 352 replies
  • September 6, 2023

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.

 


MichaelH
Ansys Staff
Forum|alt.badge.img+2
  • Ansys Staff
  • 352 replies
  • Answer
  • September 6, 2023

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')

 


Forum|alt.badge.img+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


Mark.Nicholson
Luminary
Forum|alt.badge.img+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

MichaelH
Ansys Staff
Forum|alt.badge.img+2
  • Ansys Staff
  • 352 replies
  • September 8, 2023

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


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