Function parser for OpenSCAD and parametric curve grapher

by arpruss, published

Function parser for OpenSCAD and parametric curve grapher by arpruss May 4, 2017
0 Share
Download All Files

Thing Apps Enabled

Open in Customizer Order This Printed View All Apps


Design Tools


A part of these Groups

View All

Liked By

View All

Give a Shout Out

If you print this Thing and display it in public proudly give attribution by printing and displaying this tag.

Print Thing Tag

Thing Statistics

1041Views 217Downloads Found in Math


One of the OpenSCAD limitations is the inability to pass functions to functions or modules.
This library lets you define mathematical functions in a string, using all of the OpenSCAD mathematical operators (and a bit more). This string is then compiled to an optimized representation which can be evaluated fairly quickly. For instance:

use <eval.scad>;
f = "pow(x,3)*y-x*cos(y)+y";
fc = compileFunction(f);
echo(eval(fc, [["x", 12], ["y", -5]]));

will evaluate the function at x=12 and y=-5.

Parsing the string is not fast, but if you evaluate the compiled function for different variable values, the evaluated function should be fairly fast (in my test, about 10,000 eval()s per second).

There are three public functions provided:

  • compileFunction(f): takes a function string f and returns an optimized compiled representation; the representation is described here: http://www.thingiverse.com/thing:2289738
    There may be some bugs in the optimization. If you run into problems, add the optional argument optimize=false.
  • eval(fc, variables): takes a compiled function fc and evaluates it for the specified parameter values; variables is a vector of [variableName, value] pairs; for instance, [["x",1],["y",2]]
  • evaluateFunction(expression, variables): use this to evaluate an uncompiled function once; this is a convenience function equivalent to eval(compileFunction(expression,optimize=false),variables); if you are going to evaluate the function more than a couple of times, please compile it once first, and then call eval() on the compiled version

The string function representations should be able to use all of the following standard OpenSCAD language elements:

+ - * / %  
pow sqrt cos sin tan acos asin atan atan2 abs ceil exp floor ln log round sign
cross norm max min concat
?: != == >= <= < > ! && || 

Vectors are supported, using [x,y,z] style vector forming and a[i] indexing.

Numbers can be specified in the usual way, and the true and false constants are available.

Additionally, the ^ infix operator is provided as a shorthand for pow, and COS, SIN, TAN, ACOS, ASIN, ATAN and ATAN2 are provided which are radian-based trigonometric functions.

There are likely multiple bugs I have yet to catch, and there may be some subtle deviations from OpenSCAD order of operations.

The demo file is a 3D parametric curve grapher. You should be able to use the Customizer (unless it times out) to change the formula.


On my i5 based Windows 10 laptop, per 10,000 runs with x^3*y-x*y^3:

  • compileFunction() with optimization: 25 seconds
  • compileFunction() without optimization: 21 seconds
  • eval() applied to precompiled function: 0.8 seconds
  • evaluateFunction() 22 seconds

Note: As I update eval.scad, I will upload new versions here. However, I will not be regularly updating the version inside the demo file. There is a repository here:

Update notes:

  • May 6, 2017: No longer distinguish the "fast" variables x,y,z,t from the others. Remove dollar sign operator for variable values in compiled form now that I've discovered a fast check for whether something is a string.
  • May 16, 2017: Fix evaluator bug in some functions like exp(x) and floor(x).
  • May 17, 2017: compileFunction() and evaluateFunction() should now work if fed in an already-compiled function; in that case, compileFunction() returns its input and evaluateFunction() is the same as eval(); this means that you can seamlessly support compiled and uncompiled function arguments in the same module; I also removed a warning when optimizing a function that uses norm()
  • May 18, 2017: PI supported

More from Math

view more

All Apps

Customizer is the easiest way to take great 3D printable designs and make them your own. Make your OpenSCAD designs infinitely more valuable by empowering the community to customize them.

App Info Launch App
Customizer Thing App

3D Print your file with 3D Hubs, the world’s largest online marketplace for 3D printing services.

App Info Launch App

Auto-magically prepare your 3D models for 3D printing. A cloud based 3D models Preparing and Healing solution for 3D Printing, MakePrintable provides features for model repairing, wall thickness...

App Info Launch App

Kiri:Moto is an integrated cloud-based slicer and tool-path generator for 3D Printing, CAM / CNC and Laser cutting. *** 3D printing mode provides model slicing and GCode output using built-in...

App Info Launch App
KiriMoto Thing App

With 3D Slash, you can edit 3d models like a stonecutter. A unique interface: as fun as a building game! The perfect tool for non-designers and children to create in 3D.

App Info Launch App

Print through a distributed network of 3D printing enthusiasts from across the US, at a fraction of the cost of the competitors. We want to change the world for the better through technology, an...

App Info Launch App

Quickly Scale, Mirror or Cut your 3D Models

App Info Launch App

3D Print a wide range of designs with Treatstock. Easy to use tools to get the perfect result. The global 3D printing network that connects you with high-quality and fast working print services nea...

App Info Launch App