Skip to content

Python scripts ​

Moonomat runs Python scripts on your bundle, right in the browser. Use them for the support work around a design: analysis, data conversion, batch file work, 2D plans and checking measurements. The Design agent uses the same feature.

Python is not part of Moon. A published bundle never runs Python, and a .moon file cannot call it. Design geometry is always written in Moon; Python prepares data that Moon reads (profiles, point lists, dimension tables) and checks what Moon produced.

Running a script ​

Run a .py file from the bundle, or a line of code with -c, in the terminal:

python scripts/convert.py
python scripts/plan.py 1200 800
python -c "import numpy; print(numpy.__version__)"
  • The bundle is the current folder. Use relative paths: open("data/parts.csv"), glob.glob("data/*.json"), os.listdir(".").
  • Arguments after the file name arrive in sys.argv.
  • Output appears in the terminal as the script prints it; errors show the usual Python traceback.
  • Ctrl+C stops a running script. Runs stop after 120 seconds unless you pass --timeout (up to 600).
  • The first run in a browser tab loads Python, which takes a few seconds. Later runs start right away.

Changes and undo ​

A script's file changes (files created, overwritten, deleted or renamed) are applied together when it finishes, and the terminal lists them:

3 files changed
A parts/leg.json (52 B)
M params.json
D scratch/old.csv

One undo reverts the whole run. If the script fails, is stopped or times out, nothing changes, so running it again is always safe.

If a file the script read or changed was edited while it ran (in the editor, say), the changes are not applied: run the script again to work on the current version.

Packages ​

Common packages load automatically when a script imports them:

PurposePackages
Numbers and tablesnumpy, pandas, scipy, sympy, polars, duckdb, pyarrow
2D geometry and plansshapely, pyclipper, contourpy, matplotlib, svgwrite, drawsvg, svgpathtools
Meshestrimesh, numpy-stl (import stl), pygltflib
Input formatsopenpyxl (.xlsx), xlrd (.xls), ezdxf (DXF), pypdf, lxml, beautifulsoup4, pyyaml, fonttools
ImagesPillow, scikit-image, opencv-python
Othernetworkx, scikit-learn, regex, geopandas, pyproj

Other pure-Python packages from PyPI can be installed at the top of the script:

python
import micropip
await micropip.install("xmltodict")
import xmltodict

Packages with compiled code need a Pyodide build; those without one (for example ifcopenshell or rtree) can't be installed.

Limits ​

  • No network. Scripts can't fetch URLs. Bring data from the web into the bundle first: attach or import the file.
  • Only the bundle. Scripts see the files of the open bundle and nothing else of the app: not your other bundles, not your account.
  • No threads or subprocesses. Memory is limited to a few GB, and plain Python loops are slower than on a desktop, so use numpy for heavy numbers.
  • No display: save figures to a file (plt.savefig("scratch/plot.png")) and open them in the app.

Where scripts go ​

  • scripts/ for scripts whose results the bundle keeps (a conversion, a plan generator). They travel with the bundle when you transfer or publish it, as the record of how the derived files were made.
  • scratch/ for throwaway scripts and temporary files. It is never packed or published.

The Design agent ​

The Design agent has a python tool with the same environment. It uses Python for analysis, conversion, batch work, plans and verification, not for design geometry.

The agent's scripts follow the agent's file rules: they may create and overwrite files, but delete or rename only files the agent created in the conversation (or anything under scratch/), and they never write to export/. When a step needs more, for example renaming one of your files, the agent saves the script and asks you to run it in the terminal. Typing the command is your approval, and the result is still one undo step.