Lumerical Python API: Issue with getVar()

api
python

#1

I’m having an issue with the lumapi.getVar functionality. I’m trying to extract some variables from the project’s workspace but I’m receiving the following error

'Failed to get variable'
  C:\...\lumapi.py:144

Which is an exception raised when:

ec = iapi.appGetVar(handle, varname.encode(), byref(value))
if ec < 0:
    raise LumApiError("Failed to get variable")

Can anyone please point me on to why I’m having this issue when I’m extracting a simple variable from MODE? Also, what does the ec variable in the API stand for here?

I have also noticed an issue with Lumerical applications crashing when Python API getVar calls non-existent variable. Can there be an except raised in the API instead of crashing the application?

Cheers,
Mustafa


#2

Below is a simple script that demonstrates the issue:

#%% find lumapi 
import sys, os

paath = 'C:/Program Files/Lumerical/INTERCONNECT/api/python'

if os.path.exists(os.path.join(paath,'lumapi.py')):
    print('Found lumapi path at' + ': ' +paath)
else:
    print('lumapi path does not exist, edit paath variable')
    
os.chdir(paath)
import lumapi
#%% run FDTD, initiate variable, and read it back

fdtd = lumapi.open('fdtd')

# plug in variable into workspace
inputValue = '1' 
lumapi.evalScript(fdtd, "a = '%s';" % inputValue)

# read back the variable
outputValue = lumapi.getVar(fdtd, "a")
print('FDTD: input value is: ' + inputValue + '       read value is: ' + outputValue)

#%% run MODE, initiate variable, and read it back

mode = lumapi.open('mode')

# plug in variable into workspace
inputValue = '1' 
lumapi.evalScript(mode, "a = '%s';" % inputValue)

# read back the variable
outputValue = lumapi.getVar(mode, "a")
print('MODE: input value is: ' + inputValue + '       read value is: ' + outputValue)

#3

Hi @mustafa.sh

Thanks for the script. I ran it with INTERCONNECT, FDTD, and MODE python path and they all worked just fine with no error:

It is possible that you are using different versions of the products. Can you please check and make sure that they all belong to the same release?
https://www.lumerical.com/downloads/customer.html

You can also try using MODE python path to see if you get the same error again.

I need to discuss it with our developers if you were interested to know further. Please let me know.

I tried with a modified code to retrieve a non existent “b” value:

mode = lumapi.open(‘mode’)

# plug in variable into workspace
inputValue = '1' 
lumapi.evalScript(mode, "a = '%s';" % inputValue)

# read back the variable
outputValue = lumapi.getVar(mode, "b")
print('MODE: input value is: ' + inputValue + '       read value is: ' + outputValue)

This returned a LumApiError of ‘Failed to get variable’, with no crash. Is this what you were referring to?

Please keep me updated with your findings.
Thanks


#4

Thanks @bkhanaliloo, you are correct, I ran the script on a different machine and seems to work just fine. I have the latest versions installed, but I’ll reinstall and debug further.

Cheers,
Mustafa


#5

Hi @mustafa.sh

Thanks for updating us. Please let me know if you still had any other issues.