Lua Enters the House – Part Two

My experiences with OpenScad have been a good introduction to programmatic solid modeling.  OpenScad is a small enough language that I could comprehend and make good usage of it fairly quickly.  It took me about 6 months to run into the limitations of the language.  I would say hitting those limits coincided with my desire/ability to actually describe more complex forms.

I definitely want a more substantial language.  When trying to choose a language, I have considered a few design goals.

I want a language that can be utilized in multiple environments.  By ‘environments’ I mean embedded controllers as well as desktop machines.  I think an interpreted language will be better than a standard ‘binary’ deployment.  This makes for a more flexible programming environment as programs can be easily deployed by just sending a text string.  I want an easy connection to standard ‘C’ libraries.

I had considered JavaScript, as that’s widely used, and available in web browsers.  I had considered Python because it is very popular, and has tons of libraries available.  In the end, I chose Lua.  Lua is better suited for the embedded controllers I intend to use it on.  It is a fairly robust language, and it has the smallest runtime environment of those choices I have considered.  Lua also claims to be the fastest scripting language.  I don’t know about that claim, but considering it has been out in the open market for several years, leads me to believe they have learned quite a lot, and had plenty of time to make it solid and optimized.

So what have I gained?  One of the routines I have in my library is a some metaball calculations.  Basically, the function needs to sum up the influence of different ‘balls’ in a list.

function SumInfluence(x,y,z, ballList)
local sum = 0;

for i,ball in ipairs(ballList) do
sum = sum + MBInfluence(x,y,z, ball)
return sum;

This was not possible in OpenScad.  I did not have true function bodies, so I could not have a loop.  That alone is enough to make this language interesting.  What might be even more interesting is the ‘iterators’ that you find in Lua.  Iterators allow you to write code like this:

for v in IterateMetaballs({200,200,200}, balls) do

An iterator allows you to simply write that for loop, and perform whatever function you want within the loop.  In this case, the iterator (IterateMetaballs) will keep returning values (the edge of the isosurface) until there are no more values to be returned within the specified volume we are exploring ({200,200,200}).

As functions are first class citizens in Lua, this iteration could be simplified even further, but it doesn’t need to be in this particular case.

Another thing I find interesting about Lua is the way in which it supports object oriented programming concepts.  It supports OO programming in a ‘meta’ way.  Unlike C# (my current ‘native’ language), Object Oriented concepts are not front and center as a MUST.  Lua supports tables as a basic data structure.  Tables can be used to store functions, and that can be the basis for a fairly simple object oriented system.  You can create more interesting constructs such as monads and the like.  At the same time, none of that is in your face up front, so the core language remains fairly small and fast.

For my own usage, I will stick to the simplest programming constructs.  Although more advanced functions and methods are possible, I find them to be unreadable.  I want to keep my code as readable as possible.

One thing that I would like to explore is creating a simple 3D modeling environment such as presented by OpenScad.  That is, a simple split window interface where text is in one window, and a simple rendering window is in the other.  This is fairly easy to do in Lua.

2 Comments on “Lua Enters the House – Part Two”

  1. I admire what you already created within the limitations of OpenScad. But a more widely spread scripting language would sure be better. I am happy to read that you choosed Lua. Another argument for Lua is that you can easily create a sandbox for Lua scripts, so thingiverse could allow uploading Lua files that use a standard 3D-Lua-Library – I bet that this would be more difficult with another scripting language. To facilitate the transition from OpenScad language to Lua, the 3D-Library could produce OpenScad code as one output format in addition to STL. Buth another thing could help even more important: you now seem to upload your libraries in Thingiverse, which is great for sharing objects, but poor for sharing source code. How about sharing your code in a public source code repository, such as github? I think this will increase the chance for contributions a lot. OpenScad and MCAD are also hosted at github. In git you can also add test cases and script and hooks on commits, for instance to rebuild 3D-objects and nice images of it with the latest version of you library.

    • You are right about Github. I created an account a while back, with the intention of doing exactly what you say. At some point I’ll spend the time necessary to actually put my code there.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s