Script components
A script component is drawn by code instead of by hand. You or your agent write a function in a script file. For the parameter values of each placement, the function returns the component’s channels, points, regions and placed components. Its parameters and computed values are set as for any other component.
Use a script component when its parameters should change the layout, not only the sizes. A component you draw can tie a child’s position or width to an expression, but it always has the same children. A script places as many children as the parameters ask for, by any rule you can write. Examples are a spiral with a given number of turns, an array whose spacing grows along it, or a layout built from a table of values. A script also helps with a shape that no built-in component covers and that would be tedious to draw by hand.
Most people have their agent write script components from a description of the shape and its parameters. You can write one by hand, but it is less convenient than the rest of the app. The script is written in a text editor, and part of what the component declares is typed as JSON.
The definition
A script component names a script file and a function in it. You draw nothing in its body, since the function makes everything in it. Everything else works as for any component. You place it, bind its parameters, wire its pins, rename it, duplicate it, make a local copy of it and move it to a library.
A few things a script component declares are typed as JSON in the Declarations box of its Body tab. These are its pins, pins whose number a parameter sets, the compartments it exposes for other components to be placed in, the components it may place, its slots, the values it reports and whether it fills the compartment it is placed in (Inspector). Your agent can set them too. See Using AI agents.
A new script component comes from Script component under the + of the Components
tab. Its function is script_component, in the script file named after the design, and the file
is created if it does not exist. The next new script component, ScriptComponent2, gets the
function script_component2. The inspector’s
Body tab shows the file and the function, Open in editor, which opens the file, and the
Declarations box.
The script file
The script file is a .star file in the same folder as the design file that holds the
component, and the component names it without a folder. By convention a design file has one
script file of the same name, so the components of lab-v2.exw name lab-v2.star. A library’s
script components follow the same rule: the script file sits beside the library file.
A design that has never been saved has no folder, so its script components cannot run. Each one is listed in Problems with the message save the design first.
The function
load("experiware", "Path", "channel", "point")
def straight(p):
return [
point("inlet", (0.0, 0.0), p.width),
point("outlet", (p.length, 0.0), p.width),
channel("channel", ("inlet", "outlet"), [(0.0, 0.0), (p.length, 0.0)], p.width),
]
Scripts are written in Starlark, a small dialect of Python. The function takes one argument, p,
which holds the placement’s parameters and computed values by name: p.width. A number
parameter arrives as a decimal number, a whole-number parameter as an integer, a 2D vector as an
(x, y) pair and a switch as True or False.
The function returns a list of children, or generated(children, reported = {...}) when the
component declares reported values. A pin’s source names one of the returned children by its
label. If the function does not return a pin’s source, the component is not drawn, and an error
names the pin. The children are made with the functions of the experiware module, which
load("experiware", ...) brings in:
point,channelandbore_requestfor points, channels and bores. A channel ends on a point’s label or on a placed component’s pin, such as"mixer.outlet".regionfor a chamber, withcompartmentandpatchfor its pockets and holes, andtapfor a wall tap on one of its walls. A region’s outline is a list of(x, y)and(x, y, bulge)vertices in counter-clockwise order.instancefor a placed component, with its parameter values in a dict, andfillfor the component you put in one of its slots.stationandPath, a walker of straights and arcs that the curved built-in components use too, for a channel’s stations.ring_distance,point_ring_distance,point_in_ringandring_bounds, which measure outlines with the app’s own geometry.generated.
The math functions sin, cos, tan, atan2, asin, acos, sqrt, hypot, floor,
ceil, round and pow, and PI, are available without a load. A script cannot load any
other module or file.
Placed components
A script component places only the components it declares it may place. The function names
one as "Mixer" for a component of this design, "lab-v2/Mixer" for one from a library and
"builtin@1/Serpentine" for a built-in: instance("m", "Mixer", (0.0, 0.0), args = {"width": 100.0}). A component outside the list, a parameter the component lacks and a
value of the wrong kind each stop the function at the instance call.
A component it declares counts as placed by the script component. You cannot delete it while the script component lists it, Move to library takes it along with the script component, and a component may not list one that places it.
A function must give the same children for the same parameter values, and Experiware runs it again only when the values change. A run is stopped if it takes more than 2,000,000 steps (calls and loop iterations), more than 256 MiB of memory or more than 30 seconds.
Slots and fills
A script component can place copies of a component chosen by whoever places it, as
LatticeFill places its item. For each slot it declares the fill the component in it sits in,
the parameters the function sets on every copy, the pins the function connects to, and the
component the slot holds by default. The function places a copy with
instance("p0", slot = "item", pos = (x, y)) and never sees which component it is.
A script component fills a slot of a component it places with slots:
instance("array", "builtin@1/LatticeFill", host = {"socket": "ch.floor"}, slots = {"item": fill("Diamond", args = {"size": 40.0})}). The component in a slot must be one it declares, and
it must fit the slot. A parameter the slot sets on every copy cannot be given a value there. See
Slots for filling a slot of a placed component in the inspector.
A script component that sits in a void can be declared to fill its host. Its function then
receives the outline of the compartment it is placed in as p.host_ring, in the component’s own
frame, and None when it is not placed in one. Moving the component in its compartment runs the
function again.
Script loading
Experiware reads the script files when the design opens, when you link or unlink a library, when you save the design to another folder, and when you choose File › Reload Libraries. After you edit a script file in an editor, save it and choose Reload Libraries to see the change. A script file a component names for the first time, such as after you add the component or type its name in the Body tab, is read then.
Script problems
Problems lists what is wrong with the scripts at the top of its list, one row per problem. A row
starts with the file and line, such as chip.star:12, then names the component and says what
failed. There are two kinds:
- The component cannot run. Its script file does not compile, is not in the design’s folder, or has no function of that name, or the design has not been saved. A file that does not compile is one row for the file. Anything that places the component cannot be drawn, and the error under the inspector’s header gives the same message with the file, line and column.
- The component fails at its default values. Experiware runs each script component once at its defaults, so a broken script shows before you place it. A placement whose own values the function accepts is still drawn.
A function that refuses a placement’s values, with fail("...") or an error, stops the design
from drawing as a built-in component’s refusal does, and the error under the inspector’s header
names the file, line and column where it stopped.