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”)
MODE.setmaterial(temp,“name”,“NewMaterial”)

but I get an error:


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

C:\Program Files\Lumerical\MODE\api\python\lumapi.py 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 method.name = str(name)
1096 try:

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

C:\Program Files\Lumerical\MODE\api\python\lumapi.py 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\lumapi.py 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\lumapi.py in translate(value)
557 return PutTranslator.translateList(PutTranslator.putListMembers(value))
558 else:
–> 559 raise LumApiError(“Unsupported data type”)
560
561 @staticmethod

LumApiError: ‘Unsupported data type’

Hi,

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”);’)
MODE.eval(‘setmaterial(temp,“name”,“matName”);’)

Hi ,
This solution dosen’t work:

MODE.eval(‘addmaterial(“temp = Sampled data”);’)
MODE.eval(‘setmaterial(temp,“name”,“matName”);’)


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

C:\Program Files\Lumerical\MODE\api\python\lumapi.py 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)
1155
1156 def getv(self, varname):

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

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!

Thanks,
Aniruddha.

Hello,

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”);’)
MODE.eval(‘setmaterial(temp,“name”,“matName”);’)

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.