Skip to content

Script reference ​

Needs MNodes 1.1.5 or newer

The Script node is new in MNodes 1.1.5. What is marked 1.1.6 on this page is new in MNodes 1.1.6 and does not exist in 1.1.5.

This page lists everything the code of a Script node can use, so you can look it up. How the node works, its settings and longer examples are on the Script node's page, and each part here links to where it is explained.

Inside MNodes, the Reference button on the node shows the ones you use most over the code, each with a button that writes it where the cursor is.

Functions you write ​

Write these in your code and the Script calls them. Leave out the ones you do not need.

FunctionWhen it runsWhat it is given
onStart()When the code starts, and again after every edit or Restart.Nothing.
onInput(n, value, time)For every message on In n (In 1 is 1). With CV Inputs on, also each time a CV on In n rises through 0.5.value: a number, a text, true or false, or undefined for an Event. time (1.1.6): when a CV rose, on the clock now() reads. See CV in.
onTick(dt)Tick times a second, when Tick is on in the Inspector. With the picture (1.1.6): once each time the screen refreshes.dt: the seconds since the last tick.
onKey(name, down) 1.1.6For every key of the computer keyboard pressed and let go.name: "Left", "Space", "A" and so on. down: true when pressed, false when let go.
onButton(name, down, pad) 1.1.6For every button of a game controller pressed and let go.name: one of the button names. down: true or false. pad: 1 to 4.

The node ​

CallWhat it does
out(n, value)Sends value out of Out n: a number, a text, true or false as they are, an array or an object as JSON text.
out(n)With no value, sends an Event (a trigger) out of Out n.
print(...)Writes a line in the console under the code. Objects are written as JSON.
time()The seconds since the code started.
stateAn object kept with the project and across edits: state.count = 3 is still there when the project opens again.

Keys and game controllers ​

1.1.6. How to use them in a game: Keys, for a game.

CallWhat it gives
keyDown(name)true while that key of the computer keyboard is down.
gamepad(n)What game controller n (1 to 4) is doing now. gamepad() is pad 1.

Key names are the ones the Keyboard node gives: "Left", "Right", "Up", "Down", "Space", "Enter", "Escape", the letters "A" to "Z" and the digits "0" to "9". The Script takes the keys only if its code has onKey or keyDown, and never while you type in a text box.

What gamepad(n) gives back:

FieldWhat it is
connectedtrue when a controller is there.
A, B, X, YThe four buttons on the right: true while held.
LB, RBThe shoulder buttons.
Back, StartThe two small buttons in the middle.
LS, RSThe sticks pressed in.
Up, Down, Left, RightThe direction pad.
lx, ly, rx, ryThe left and right sticks, -1 to 1, up and right positive. A stick at rest reads 0.
lt, rtThe triggers, 0 to 1.

On Windows any controller that speaks XInput works: Xbox pads and most others. On a Mac, the ones the system knows: Xbox, PlayStation and MFi pads. The controllers are read only while a Script uses them.

CV and MIDI out ​

1.1.6. Set CV Outputs and MIDI Outputs in the Inspector first. How it works, with an example: CV and MIDI out.

CallWhat it does
cv(n, value, { at, glide })Moves CV n to value and holds it there. glide: slides there over that many seconds.
note(n, pitch, velocity, length, { at, channel })Plays a note out of MIDI n. Pitch 60 is middle C, velocity 1 to 127 (100 if not given), length in seconds (0.25 if not given).
noteOn(n, pitch, velocity, { at, channel })Starts a note that sounds until its noteOff.
noteOff(n, pitch, { at, channel })Ends it.
cc(n, number, value, { at, channel })A controller: number and value, 0 to 127.
bend(n, value, { at, channel })Pitch bend, -1 to 1.
allNotesOff(n)Every note of MIDI n off. With no n, of every MIDI output.
now()The clock at is on, in seconds.
transport()The song: playing, bpm, beat, beatInBar, beatsPerBar and time (now, on the same clock).
atBeat(beat)The time a beat falls at, to use as at.

The options can be left out. at is a time on the clock now() reads: the change lands on that exact sample. Without it, it plays at the start of the next block. channel is 1 to 16 (1 if not given). Each call gives back true when it was sent, or false when it was not, such as an output the node does not have; the console says why, once.

The app window ​

1.1.6. For the menu of a game made in MNodes. They work only in the standalone app, never in a DAW, and not in the code's first moment and a half. See also the App node.

CallWhat it does
app.quit()Leaves MNodes, asking to save first.
app.fullScreen(on)true fills the screen, false gives it back, nothing turns it the other way round.
app.isFullScreen()true while the window fills the screen.
app.standalone()true in the standalone app, false in a DAW.

app.quit() and app.fullScreen() give back true when they work, or a text that says why not: "standalone only" or "too soon".

Saved games ​

1.1.8. A game's progress, kept on the computer of whoever plays it and apart from the project: playing never changes the project, and a player who opens the game again, from the library or in a new session, carries on where they left off. How to use it: Saving a game.

CallWhat it does
save.game(name)Names the game. Call it once, at the top: its saves are kept under that name.
save.get(key, otherwise)What was kept under key, or otherwise when nothing was, as the first time the game is played.
save.set(key, value)Keeps a value under key: a number, a text, true or false, or an array or an object of them. Gives back false when it cannot keep it, and the console says why.
save.remove(key)Forgets one.
save.list()The keys kept, as a list.
save.clear()Forgets everything in this save.
save.slot(name)Another save of the same game from then on, such as "Slot 2" for a second player. The first is "Slot 1".
save.slots()The saves the game has, as a list of their names.
save.perInstance(true)In a DAW, each copy of MNodes keeps saves of its own, and finds them again when the session opens.

save.game(), save.slot() and save.perInstance() with nothing in the brackets say which is in use, and they can go in one line: save.game("Marble Run").slot("Slot 2").

Drawing on a Scene ​

Wire Out n into a Scene's Objects input, then draw on it from the code. How it works: Drawing on a Scene.

CallWhat it does
canvas(n)The Scene that Out n (1 to 8) is wired into. scene(n) is the same.
c.size(width, height, fit)The units things are laid out in, fitted to the Scene. fit: "contain" (the default), "stretch" or "cover".
c.background(colour)A plain background. It hides the Scene's own picture and its zones.
c.showCode(on)Brings the Scene's own picture and zones back under the drawing.
c.clear()Takes away everything the Script drew.
c.find(id)The object with that id, or null.
c.on(event, fn), c.off(event, fn)The mouse on the Scene, heard after its objects and groups. See Events.
c.theme({...})The look of the controls made after it.
c.add(Kind, {...})A control of your own. See Your own control.

What you can draw ​

Each maker takes the object's properties and gives the object back. Inside a group, g.rect({...}) and the others put the new object in the group.

MakerWhat it drawsIts own properties
c.rect({...})A rectanglex, y (top left), w, h, radius
c.circle({...})A circle or an ellipsex, y (middle), r, or rx and ry
c.arc({...})Part of a ringx, y, r, width, start, end (degrees, 0 at the top, clockwise), cap
c.line({...})A straight linex1, y1, x2, y2, width, cap ("round", "butt" or "square")
c.polyline({...})Lines through pointspoints, closed, width, cap
c.triangle({...})A trianglex1, y1, x2, y2, x3, y3
c.polygon({...})A filled shape through pointspoints, counted from its x and y
c.path({...})Any outline, as SVG writes itd, or points and closed
c.star({...})A starx, y, r, sides, inner
c.ngon({...})A regular polygonx, y, r, sides (6)
c.text({...})Textx, y, text, size, align, baseline, lineHeight, letterSpacing, maxTextWidth (longer text shrinks to fit)
c.image({...})A picture from a filex, y, w, h, src, fit ("contain" or "cover"), radius
c.wave({...})The sound on the Scene's In A or In Bx, y, w, h, input ("a" or "b"), shows ("wave" or "spectrum"), style ("line", "fill", "bars" or "mirror"), bars, gain, log
c.group({...})A group of objectsx, y, w, h, clip, reveal, revealFrom

points is [[x, y], ...] or [x, y, x, y, ...]. What each property means in detail: the makers' table.

Properties every object takes ​

PropertyWhat it does
fillIts colour, a gradient or a pattern.
stroke, strokeWidthIts outline, and how thick.
glow, glowColorA soft light round it.
shadowA shadow under it: a colour, or { color, x, y, blur }. false takes it away.
blurSoftens its edges.
trailWhere it has been fades behind it over so many seconds.
opacity0 (not seen) to 1.
rotation, scale, scaleX, scaleY, originX, originYTurned (degrees, clockwise) and sized round its middle, or round the point originX and originY say (0 to 1 across it).
visiblefalse hides it and what it holds.
blend"add" adds its light to what is under it.
cursorThe pointer over it: "pointer", "grab", "crosshair", "text", "move", "ns-resize", "ew-resize" or "none".
tooltipA text shown when the pointer rests on it.
interactivefalse lets the mouse through. true takes clicks with no handler.
hitPadHow far round it, in units, the mouse still finds it.
idA name to find it with c.find(id).
parentThe group to put it in, or its id.
onHandlers, as { click: fn, drag: fn }.
bindTies a property to the sound, the time or a control. See Following without code.

Colours are written "#4fd1ff", "#4fd1ff80" (the last two digits its alpha), "rgb(79, 209, 255)", "rgba(79, 209, 255, 0.5)" or a colour's name. A gradient is { from, to, angle }, with radial: true or conic: true. A pattern is { from, to, pattern, size, weight, angle }, the patterns "stripes", "dots", "checker", "grid" and "noise". The details: Properties every object takes.

A place or a size can also be a share, written as a text: "50%", "100% - 20", "50%w", "50%h", "50%min", "50%max", "calc(50%w + 10)".

Changing what is drawn ​

CallWhat it does
o.set({...})Changes it at once.
o.get(name)The last value given to that property.
o.animate({...}, { duration, ease, delay, repeat, yoyo }, then)Takes it there over time. A number in place of the options is the duration. Defaults: 0.3 seconds, "outCubic", no delay, no repeat. then() runs when it is over.
o.stop()Stops its changes over time where they are.
o.front(), o.back()Over, or under, the objects next to it.
o.on(event, fn), o.off(event, fn)Listens to the mouse on it, or stops. off with no fn stops them all.
o.remove()Takes it away, with everything in it.
g.clear()Empties a group.
g.redraw(fn)Empties a group and lets fn(g) fill it again.

Every object also has o.id, o.kind ("rect", "group" and so on), o.parent and o.children.

The easings: linear, inQuad, outQuad, inOutQuad, inCubic, outCubic, inOutCubic, inSine, outSine, inOutSine, inExpo, outExpo, inOutExpo, inBack, outBack, inOutBack, outElastic and outBounce.

Events ​

o.on(event, fn) calls fn(e). The mouse events: enter, leave, move, press, release, click, doubleclick, dragstart, drag, dragend, wheel and rightclick. Also animationend, when a change over time is over.

Field of eWhat it is
e.typeThe event's name.
e.x, e.yWhere, in the Scene's units.
e.world 1.1.8Over a 3D view or a solid in it: the point of its world under the pointer, on the flat plane where z is 0, as { x, y, z }.
e.u, e.vWhere on the object, 0 to 1 across it.
e.dx, e.dyHow far a drag moved since the last one.
e.totalX, e.totalYHow far since the press.
e.deltaHow far the wheel turned.
e.shift, e.ctrl, e.altWhether those keys were held.
e.targetThe object.
e.canvasThe Scene it happened on.
e.stop()Stops it going on to the groups the object is in and to c.on.

Following without code ​

bind: { property: [source, from, to, options] } moves a property on every frame, while the code does nothing. How it works: Following the sound with no code.

SourceWhat it follows (0 to 1)
levelA, levelBHow loud the Scene's In A or In B is.
bass, mid, trebleIn A's lows, mids and highs.
P1 to P60The Scene's P controls.
timeThe Scene's clock, 0 to 1 over and over, as many times a second as the fourth number says.
a zone's nameThat zone's value. "Pad.y" is the Y of an XY pad.
OptionWhat it does
decimals, prefix, suffixFor text: how the number is written.
rateHow many times a second time goes round.
smoothGlides after the source over that many seconds, instead of jumping.

o.set({ bind: null }) lets every bind of the object go.

3D ​

c.view3d({...}) puts a 3D world on the Scene, and the makers below put things in it. In the world, y is up, x across and z towards you. How it works: In three dimensions.

Each maker matches a Scene node, linked in the table, and most of that node's settings are a property here with the same meaning. To see the exact names, build it with the node, right-click the node and use Copy as Script.

MakerWhat it makesIts own properties
c.view3d({...})A 3D view, with its camera (3D View)x, y, w, h, radius, yaw, pitch, distance, tx, ty, tz, fov, ortho, orbit, fill, ambient, environment, showEnvironment, intensity, envYaw, exposure, bloom, bloomThreshold, bloomSize, focus, focusDistance, focusRange, tiltShift, ao, aoRadius, saturation, contrast, warmth, vignette, edges, edgeWidth, edgeColor, temporal, fog, fogColor, fogStart, fogHeight, fogFade, hideLabels
v.box(), v.sphere(), v.cylinder(), v.cone(), v.torus(), v.plane()A solid (3D Shape)w, h, d (1 each, a plane 4 by 4), r (0.5), tube (0.18), segments, mirror (a plane)
v.mesh()A model from a file (3D Model)src (an OBJ or glTF file), w, h, d, animation, animationTime
v.light()A light (3D Light)type ("directional", "point", "spot", "panel" or "environment"), intensity (1), fill, r (10), w, h (a panel), tx, ty, tz, cone, soft, shadows, blur (1.5)
v.group()A group that moves and turns together (3D Group)label
v.clones()Copies of what it holds (3D Clones)count (8), layout, spacing (1.2), columns, r (2), turns, turnEach, riseEach, growEach, jitter, seed, spectrum, input
v.particles()Sparks, fire, snow (3D Particles)rate (40), life (2), speed (2), spread (25), gravity, drag, size (0.12), sizeEnd, fill, colorEnd, glow, blend, shape, count (400)
v.join()What it holds as one solid, merged or melted together (3D Join)mode ("merge", "blend" or "intersect"), smooth (0.25), detail (64)
v.subdivide()What it holds with its faces split, rounded off (3D Subdivide)levels (2), round (true)
v.displace()What it holds with its surface moved out and in (3D Displace)map ("noise", "waves", "bricks", "tiles", "dimples", "brushed" or a picture's whole path), amount (0.15), levels (2), mid (0.5), mapRepeat (1)
v.fog()Fog in the world (3D Fog)fog (0.07), fogColor, fogStart, fogHeight, fogFade, opacity
v.sound() 1.1.6Where a sound is (3D Sound)name
v.listener() 1.1.6Where the sound is heard from (3D Listener)
v.zone() 1.1.6A place that sounds like a room (3D Reverb Zone)w (10), h (6), d (10), r, roomMix (0.35), roomSize (0.6), roomDecay (0.6), roomDamp (0.3), roomFade (2), roomPreDelay (20 ms)

The numbers in brackets are what a property is when you do not give it.

What every solid, group and light takes:

PropertyOnWhat it does
x, y, zeverythingWhere it is.
rotX, rotY, rotZsolids, groupsTurned round each axis, in degrees. rotation is rotZ.
scale, scaleX, scaleY, scaleZsolids, groupsIts size: 2 twice as big.
fill, opacitysolidsIts colour, and how much shows through.
metal, roughsolids0 plastic to 1 metal; 0 polished to 1 matt (0.5).
glow, glowColorsolidsThe light it gives of its own.
flatsolidsEach face shaded as one.
texture, normalMapsolidsA picture on it, and bumps that catch the light: a file's whole path, "image1" or "image2", or a pattern's name.
tile, tileU, tileV, offsetU, offsetV, bumpsolidsHow its pictures repeat and slide, and how strong the bumps are.

Inside a join, a group with cut: true is cut out of the join instead of added to it. A join, subdivide or displace with lookInside: true wears the look of the first solid it holds.

Anything flat made in a 3D world (a text, a circle, a control) is a label, drawn where its group is seen. A particles object has p.burst(n), which makes n at once (20 if not given).

1.1.8. v.pointAt(x, y) turns a point of the Scene, such as a mouse event's e.x and e.y (or the event itself, v.pointAt(e)), into the point of the view's world under it: where the camera's line of sight through that point meets the flat plane where z is 0. It gives { x, y, z }, or null when that line never meets the plane. { y: 0 } uses the ground instead, { x: 2 } or { z: -1 } another plane, and { facing: true } the plane through what the camera looks at, turned to face it. It follows the camera as it is drawn, however it is turned, tilted or zoomed, with or without Orthographic, so a solid can be dragged under the mouse: const p = v.pointAt(e); if (p) bell.set({ x: p.x, y: p.y });

Physics ​

1.1.6. A solid or a 3D group with body falls, collides and bounces in its view's world. How it works: Physics.

PropertyOnWhat it does
bodya solid, a 3D group"dynamic" (falls and is pushed), "static" (stays put) or "kinematic" (goes where its x, y and z say).
massa bodyIn kilograms. 0, the default: from its size.
bounce, frictiona body0 a thud to 1 a superball; 0 ice to 1 rubber (0.5).
damping, gravitya bodyHow much the air slows it (0.05); how much gravity pulls it (1).
sensora bodyStops nothing, only feels what comes in and goes out.
uprighta bodyNever turns over.
fasta bodyDoes not pass through thin walls when thrown fast.
collidera body"box", "ball", "capsule" or "cylinder" in place of its own shape.
namea bodyFor joints made by nodes to find it.
CallWhat it does
v.physics({ gravity, speed, paused, tempo })The view's world: how hard everything is pulled down (9.81), how fast its time goes (1), stopped or not. tempo: the BPM it is laid out at; while the transport plays, its time follows the transport's tempo (laid out at 90, it goes a third faster at 120), so a fall that takes a beat takes a beat at any tempo. 0, the default: off.
v.joint({...})Joins body a to body b, or to the world: joint is "hinge", "spring", "rope", "ball", "fixed" or "slider".
o.push(x, y, z)Adds to its speed. push({ x, y, z, spinX, spinY, spinZ }) spins it too.
o.velocity(x, y, z)Sets its speed.
o.body()Where it is and how fast it goes now: { x, y, z, vx, vy, vz, awake }.
o.restart()Every dynamic body of its view back where it was put.
v.ray(from, to)The first body a straight line meets: { item, id, x, y, z, normal, distance }, or null.

A joint's properties: a, b, x, y, z, axis ("x", "y" or "z"), min, max, speed, strength, friction, stiffness (2), damping (0.1), length. All of them explained.

EventWhenFields
hitIt begins to touch something.e.other, e.otherId, e.strength (0 to 1), e.speed, e.point, e.normal, e.at
bodyenter, bodyexitA body comes into or leaves a sensor.e.other, e.otherId, e.at
bodymoveEvery frame it moves.e.point, e.speed
jointmoveA joint moves.e.value (a hinge's angle, a slider's place, a spring's length), e.speed

1.1.8. e.at is when it happened, to a sixtieth of a second, on the clock now() reads, however late the picture was drawn. Give it to a note to play it in time: note(1, 60, 100, 0.3, { at: e.at + 0.05 }). A time that has already gone plays at once, so the 0.05 (50 ms) keeps every hit the same short way behind its moment, an even beat.

Controls ​

Knobs, sliders and the rest, ready to place in one line each. How they work: Ready-made controls.

MakerWhat it isIts own options
c.knob({...})A knobsize (80)
c.slider({...})A sliderw, h (160 by 28, or 28 by 160 standing up), vertical
c.button({...})A buttonw, h (100 by 36), mode ("trigger", the default, or "momentary"), onPress, glissando (a group's name: slide from one momentary button to the next)
c.toggle({...})An on and off switchw, h (64 by 32)
c.xy({...})A pad with a point to drag; its value is [x, y]w, h (160 by 160), outX, outY
c.meter({...})A level to watchw, h (160 by 16, or 16 by 120 standing up), vertical
c.steps({...})A row of cells to turn on and off; its value is the listcount (8), w, h (34 by 34 each), gap (8)
c.select({...})A row of choices, one lit; its value is its number from 0options, w, h (70 by 30 each)

Every control also takes:

OptionWhat it does
x, yWhere it is: its top left, or a knob's middle.
nameShown under it, and what its value is kept under with the project.
labelShown instead of the name. "" shows none.
value, defaultWhere it starts, and what a double-click brings back.
min, max, step, logIts range (0 to 1 unless you say), its steps, and a range for frequencies.
unit, decimals, formatHow its value is written. format is a function that gives the text.
colorIts own accent colour.
tooltip, idA text when the pointer rests on it, and a name to find it.
outIts value goes out of Out n whenever it changes, and once when the code starts. send: false leaves out that first one.
inputA message on In n sets it, and goes on out.
onChangeA function called with each new value.
paramtrue makes it one of the Scene's zones: an output of the Scene, and a parameter your DAW can automate. Not for a meter or steps.
On the controlWhat it does
k.valueIts value, to read or to set: k.value = 0.3.
k.on("change", fn)Calls fn with each new value.
k.groupThe group it is drawn in, to move or animate it.
k.remove()Takes it away.
steps.light(i)Lights cell i as the one playing.

c.theme({ accent, body, edge, track, text, label, font }) changes the look of the controls made after it. font is the size of their text (12).

Your own control ​

A class built on canvas.Component, made with c.add(Kind, {...}), takes every option above:

PartWhat it is
build()You write it: makes the drawing in this.group, once.
draw()You write it: shows this.value.
this.set(v)Gives it a new value, kept, sent and drawn as any control's.
this.group, this.valueIts group, and its value now.

An example: Ready-made controls.

Limits ​

WhatHow much
Inputs, Outputs1 to 8 each.
CV Outputs, MIDI Outputs0 to 8, and 0 to 4.
A callA quarter of a second. One that runs longer is stopped.
StartingThe code's top level and onStart have two seconds each.
canvas(n)n from 1 to 8.
gamepad(n)Pads 1 to 4.
Trails8 on a Scene.
Lights32 in a 3D view.
A save256 KB of what it keeps.

The code runs in a sandbox: it reaches no network, and no files other than its own saved games.

MNodes Documentation