How to set a name of new material in python script

Dear Lumerical team,
I am trying to set a name for a new material in python script:

temp = MODE.addmaterial(“Sampled data”)

but I get an error:

LumApiError Traceback (most recent call last)
2 temp = MODE.addmaterial(“Sampled data”)
----> 3 MODE.setmaterial(temp,“name”,“NewMaterial”)

C:\Program Files\Lumerical\MODE\api\python\ in (self, *args)
1092 appCallWithConstructor(self, x, args, **kwargs))(name)
1093 else:
-> 1094 method = (lambda x: lambda self, *args: appCall(self, x, args))(name)
1095 = str(name)
1096 try:

C:\Program Files\Lumerical\MODE\api\python\ in appCall(self, name, *args)
737 vin = vname + ‘i’
738 vout = vname + ‘o’
–> 739 putList(self.handle, vin, list(args[0]))
741 code = ‘%s = cell(3);\n’ % vout

C:\Program Files\Lumerical\MODE\api\python\ in putList(handle, varname, values)
326 llist = 0
327 try:
–> 328 llist = PutTranslator.putListMembers(values)
329 except LumApiError as e:
330 raise

C:\Program Files\Lumerical\MODE\api\python\ in putListMembers(value)
597 index = 0
598 for v in value:
–> 599 llist[index] = PutTranslator.translate(v)
600 index += 1
601 return llist

C:\Program Files\Lumerical\MODE\api\python\ in translate(value)
557 return PutTranslator.translateList(PutTranslator.putListMembers(value))
558 else:
–> 559 raise LumApiError(“Unsupported data type”)
561 @staticmethod

LumApiError: ‘Unsupported data type’


This seems to be an API bug where:

temp does not properly reference the temporary name of the material in the library. I reported it as a bug.

Meanwhile, you might need to execute the equivalent of Lumerical script via the API:

MODE.eval(‘addmaterial(“temp = Sampled data”);’)

Hi ,
This solution dosen’t work:

MODE.eval(‘addmaterial(“temp = Sampled data”);’)

LumApiError Traceback (most recent call last)
----> 1 MODE.eval(‘addmaterial(“temp = Sampled data”);’)
2 MODE.eval(‘setmaterial(temp,“name”,“matName”);’)

C:\Program Files\Lumerical\MODE\api\python\ in eval(self, code)
1152 def eval(self, code):
1153 “”“eval will evaluate the given script code as a string”""
-> 1154 evalScript(self.handle, code)
1156 def getv(self, varname):

C:\Program Files\Lumerical\MODE\api\python\ in evalScript(handle, code)
235 ec = iapi.appEvalScript(handle, code.encode())
236 if ec < 0:
–> 237 raise LumApiError(“Failed to evaluate code”)

LumApiError: ‘Failed to evaluate code’

Hello @mbenes/ @lyap ,

Even I am looking for some solution on this. How can I add and set material properties using a python script?

Need help!



The MODE.eval(‘command;’) statement simply executes the Lumerical scripting command via API. It is the equivalent of executing the command manually in your Lumerical tools’ command line. Hence, you can simply test the script first in your Lumerical tool and then carry it over to your Python script. I find this to be easiest way to troubleshoot your eval code.

Here is the full python script to add the material:

import lumapi
MODE = lumapi.MODE()
MODE.eval(‘temp = addmaterial(“Sampled data”);’)

Note that the forum likes to change the apostrophe to a different character. If you keep getting an error, make sure you are using the correct apostrophe accepted by your scripting editor/Python.