Tutorials PartCraft help Walkthroughs
Write and run scripts (Script workspace)
The Script workspace step by step: write a CraftScript or Lua script, run it with F5, read the console, keep results in the variables for the next script, show meshes, fields and plots, stop a run, and save scripts with the part.
Written for PartCraft 0.3. The same help is inside the app: press F1.
Preview. Scripting is new in PartCraft 0.3 and is a preview: the language, the library and the way scripts are saved may still change, and scripts written now may need small edits later. Tell us what breaks.
The Script workspace runs your own calculations next to the part: small sums, whole analyses, or the scripts in the analysis library. Scripts are written in CraftScript (a matrix language much like MATLAB, see The CraftScript language) or in Lua (see Scripts in Lua).
Choose Script in the Workspace box at the left of the toolbar. The part is not changed. The right panel becomes the script panel (wider than usual; drag its edge to change it, or move it to a second screen with its ⋯ menu, see Arrange the panels).
Run your first script
- Type in the editor, for example:
`` x = linspace(0, 2*pi, 50); y = sin(x); plot(x, y, 'title', 'A sine') disp(max(y)) ``
- Press F5F5 (or Run). The console under the editor shows what the script printed, and "Done in … s". The plot appears in the sub-displays along the bottom of the 3D view.
- A line without a semicolon prints its value, as in MATLAB:
a = 3printsa = 3;a = 3;prints nothing. - To run only part of a script, select the lines and press ⌘ ReturnCtrl+Enter (Selection). With nothing selected it runs the line the cursor is on.
- The line at the bottom of the console (after
>>) runs one statement as you press ReturnEnter. ↑↑ and ↓↓ bring back earlier lines.
When something goes wrong
- An error names the problem and its line: the line is marked red in the editor, and Line n in the console takes you there.
- A loop that never ends: press Stop (toolbar or panel). The script stops at once; the variables keep the values they had before that run.
- Every run also has a step budget (2 billion steps by default; change it under Run options in the panel). A run that passes it stops with a message.
Variables carry from script to script
Every top-level variable a script makes lands in the Variables tab (name, type, size, smallest and largest value) and stays there for the next script, in either language. That is how one script meshes, the next solves, and a third plots.
- Click a variable to show it: meshes, fields and models in the 3D view, arrays as a table (plus a line plot for a vector or a colour map for a matrix) in the sub-displays.
- Save as JSON, Save as CSV (a table of numbers) and Save as HDF5 download it; Delete removes it; Clear all empties the workspace.
- Tick Save the variables with the part to keep them in the part file (up to 32 MB; bigger ones stay for this visit only). When you open such a part, the panel offers to load them.
Meshes, fields and plots
show(m)draws a mesh;show(f)colours a field on its mesh with a legend. The Shown list at the top left of the view hides (eye) or removes each item, and hides or shows the part itself. Under the legend, type a range (From, to) or press Auto; a vector or stress field also offers which component to show.show(f, 'deform', u, 'scale', 50)draws the mesh moved by a displacement fieldu, enlarged 50 times (withoutscale, about 8 % of the model's size).- Click a shown mesh: the nearest node, the element and the field values there appear in the sub-displays (Picked).
plot,imagescandtableput a line plot, a colour map of a 2-D array or a table in the sub-displays. A plot with the same'id'(or title) as an earlier one replaces it, so a loop can update one convergence plot;'append', trueadds a curve to it. Clear empties the sub-displays; the arrow folds them away.- FF or Fit fits the view to what is shown.
Files
- Open in the Script group (or drop files on the view or the panel):
.craftand.luafiles open as new scripts (they do not run); data files (STL, OBJ, CSV, JSON, NPY, HDF5) load into the variables under their file name. - Save in the Script group downloads the current script as a file.
- New in the Script group (or + by the tabs) starts another script. Double-click a tab to rename it; its × removes it (you can undo that).
Scripts are saved in the part
Every script in the tabs is kept in the part, so Save in the File group saves them with it and changes to them can be undone. Opening a part never runs its scripts: the console says how many it carries, and they run only when you press Run. A part from someone else can hold any script: read it before you run it. Scripts run in the background on this computer with no network access and cannot touch the page.