Script Commands as Methods - Python API – Ansys Optics
Script Commands as Methods - Python API – Ansys Optics
In this article
Script Commands as Methods - Python
Built-In Scripting Commands Related
articles
API
Importing Custom Script
Commands Python API
Non-Constructor Script overview
Commands
FDTD MODE DGTD CHARGE HEAT FEEM INTERCONNECT Lumerical
Unsupported Methods scripting
Local DocumentationAutomation API language -
Alphabetical
TOP ⬆
list
At the most basic level, the Lumerical Python API can be used to directly invoke
Lumerical script commands and interact with the product as the Lumerical Scripting Passing Data
- Python API
Language would.
Session
This article will describe the basic use case for using scripting commands as Management
methods, and common best practices. - Python API
Lumerical
scripting
Built-In Scripting Commands language -
By category
Overview
Recently
Almost all script commands in the Lumerical Scripting Language can be used as viewed
methods on your session object in Python. The lumapi methods and the Lumerical articles
script commands share the same name and can be called directly on the session
Session
once it's been created.
Management
- Python API
For more information on the Lumerical Scripting Language, please see:
Python API
Lumerical Scripting Learning Track on Ansys Innovation Courses (AIC) overview
For more information on how to import lumapi, see the Knowledge Base article on
Installation and Getting Started for Lumerical Python API.
https://optics.ansys.com/hc/en-us/articles/360041579954-Script-Commands-as-Methods-Python-API 第1/8⻚
Script Commands as Methods - Python API – Ansys Optics 2025/5/5 18:20
Example
import os,sys
import numpy as np
sys.path.append("C:\\Program Files\\Lumerical\\v251\\api\\python\\") # locat
import lumapi
import matplotlib.pyplot as plt
# Set up source
fdtd.addgaussian()
fdtd.set("injection axis","z")
fdtd.set("direction","forward")
fdtd.set("x",0)
fdtd.set("x span",16e-6)
fdtd.set("y",0)
fdtd.set("y span",16e-6)
fdtd.set("z",0.2e-6)
https://optics.ansys.com/hc/en-us/articles/360041579954-Script-Commands-as-Methods-Python-API 第2/8⻚
Script Commands as Methods - Python API – Ansys Optics 2025/5/5 18:20
# Set up monitor
fdtd.addpower()
fdtd.set("monitor type","2D Z-normal")
fdtd.set("x",0)
fdtd.set("x span",16e-6)
fdtd.set("y",0)
fdtd.set("y span",16e-6)
fdtd.set("z",0.3e-6)
# Run simulation
fdtd.save("fdtd_tutorial.fsp")
fdtd.run()
Many Lumerical script commands are used to add simulation objects such simulation
regions or geometric regions. These commands typically start with “add”, for
example, addrect or addfdtd.
In the Lumerical Python API, simulation objects can be created in many different
ways. At a fundamental level, objects can be created and have their properties set
like how it is done in a Lumerical script using set and setnamed.
The examples below show various methods on object construction. For more
information regarding adding and manipulating simulation objects, including best
practices, see the Knowledge Base article on Working with Simulation Objects in
Python API . Property names where a space is present (e.g. “x span”) is replaced by
an underscore when using keyword arguments (e.g. “x_span”).
Example
These examples create a 3D FDTD region centered at the origin and with a span of
1µm.
fdtd = lumapi.FDTD()
https://optics.ansys.com/hc/en-us/articles/360041579954-Script-Commands-as-Methods-Python-API 第3/8⻚
Script Commands as Methods - Python API – Ansys Optics 2025/5/5 18:20
fdtd.addfdtd()
fdtd.set(“x”,0)
fdtd.set(“y”,0)
fdtd.set(“z”,0)
fdtd.set(“x span”,1e-6)
fdtd.set(“y span”,1e-6)
fdtd.set(“z span”,1e-6)
The following code creates the FDTD region uses an ordered dictionary to set its
properties.
The following code creates the FDTD region using keyword arguments
fdtd = lumapi.FDTD()
fdtd.addfdtd(x=0,y=0,z=0,x_span=1e-6, y_span=1e-6, z_span=1e-6) #Note that t
Example
MyFunctions.lsf
function helloWorld(){
return "helloworld";
}
function customAdd(a,b){
return a+b;
}
https://optics.ansys.com/hc/en-us/articles/360041579954-Script-Commands-as-Methods-Python-API 第4/8⻚
Script Commands as Methods - Python API – Ansys Optics 2025/5/5 18:20
MyFunctions2.lsf
function customMultiply(a,b){
return a*b;
}
The following script imports functions from both custom script files upon session
creation.
Returns
helloworld
3.0
20.0
For example the following code will result in an error, even though the set script
command takes property and value as input arguments.
Example
fdtd.addfdtd()
fdtd.set(property = "x span", value = 1e-6)
fdtd.addfdtd()
fdtd.set("x span", 1e-6)
This applies also to methods defined in other scripting files that are loaded by first
evaluating the script file.
Example
function constructFDTDandRect(x_input,y_input,z_input){
https://optics.ansys.com/hc/en-us/articles/360041579954-Script-Commands-as-Methods-Python-API 第5/8⻚
Script Commands as Methods - Python API – Ansys Optics 2025/5/5 18:20
#This function creates an FDTD and a rectangle region with center coordi
addfdtd;
set("x",x_input);
set("y",y_input);
set("z",z_input);
addrect;
set("x",x_input);
set("y",y_input);
set("z",z_input);
}
The following Python driver script will cause an error, even though the function
defined in the script file have arguments named x_input, y_input, and z_input.
fdtd = lumapi.FDTD()
custom_code = open("MyConstructor.lsf", "r").read()#This assumes the current
fdtd.eval(custom_code)
fdtd.constructFDTDandRect(x_input =0,y_input = 0, z_input = 0)
In contrast, the following driver script will execute without error, and add both the
FDTD region as well as the rectangle.
fdtd = lumapi.FDTD()
custom_code = open("MyConstructor.lsf", "r").read()#This assumes the current
fdtd.eval(custom_code)
fdtd.constructFDTDandRect(0,0,0)
Unsupported Methods
While most script commands are available, there are a few categories of commands
that are not available for use in the Python API. For example, certain reserved
keywords (such as “c” for the speed of light) are unavailable in Python. If you
requires access to these variables, it is best to either define them in Python, or to
use the eval() method. However, if the eval() method is used, you should be mindful
that the variables in the Python and Lumerical scripting environments are not
automatically shared.
Operators
Script operators that are used in the Lumerical Scripting Language cannot be
overloaded and used “as-is” using the same syntax in Python, therefore, they are not
available, and you should use alternatives in Python. The unavailable operators
include:
https://optics.ansys.com/hc/en-us/articles/360041579954-Script-Commands-as-Methods-Python-API 第6/8⻚
Script Commands as Methods - Python API – Ansys Optics 2025/5/5 18:20
When algebraic or logical operators are needed, you should use the native Python
operators to manipulate variables, even though some syntax differences would exist
between Python and Lumerical Scripting Language. For example, one way to
implement the exponent operator ^ in Lumerical scripts in Python is through the
** operator.
When the ? (print, display) operator is required, your should use the Python print
function to display and querynamed method, queryanalysisprop method, and etc.
to access simulation object properties in Lumerical.
Local Documentation
For information on the lumapi methods from within the environment we support
Python docstrings for Lumerical session objects. This is the simplest way to
determine the available script commands, and syntax. This contains information that
is similar to the Alphabetical List of Script Commands . You can view the docstring
by using the Python built-in function "help" or most ways rich interactive Python
shells display docstrings (e.g. IPython, Jupyter Notebook):
help(fdtd.addfdtd)
Help on method addfdtd in module lumapi:
addfdtd(self, *args) method of lumapi.FDTD instance
Adds an FDTD solver region to the simulation environment. The extent of
the solver region determines the simulated volume/area in FDTD
Solutions.
+-----------------------------------+-----------------------------------
| Syntax | Description
+-----------------------------------+-----------------------------------
| o.addfdtd() | Adds an FDTD solver region to the
| | simulation environment.
| |
| | This function does not return any
| | data.
+-----------------------------------+-----------------------------------
See Also
set(), run()
https://kb.lumerical.com/en/ref_scripts_addfdtd.html
Note: We still support previous version of the Python API, yet we strongly
recommend to use the newer Python API.
See Also
Python API Overview, Working with Simulation Objects – Python API, Script
Commands as Methods – Python API, Installation and Getting Started – Python
API
https://optics.ansys.com/hc/en-us/articles/360041579954-Script-Commands-as-Methods-Python-API 第7/8⻚
Script Commands as Methods - Python API – Ansys Optics 2025/5/5 18:20
https://optics.ansys.com/hc/en-us/articles/360041579954-Script-Commands-as-Methods-Python-API 第8/8⻚