OpenSCAD metaprograming in C

2011-07-04: OpenSCAD is a pretty cool little program and I’ve been using it for awhile now. One thing that has bugged me about it though is that the language is kind of lame. For example, there are variables — sort of. You can set them, but only the last one in the program has any effect. So they don’t really act like real variables. The looping structures are sort of tacked on to deal with this fact (I think). There is no recursion. This last one is painful if you want to think about making fractal models.

It occurred to me that because the OpenSCAD language is so simple and so C-like (at least superficially), it is trivial to write a little C library to emit OpenSCAD code. Then, you can program in C, a “real” language with real variables, types, functions, recursion, etc. and it will still look (mostly) like OpenSCAD code.

So that’s what I did. I made a little library and called it opencscad. There are probably other, better attempts to do the same thing, or similar things. I know someone’s tried a similar thing with python, for example.

Anyway, here are a couple of pics that I made with the help of this tiny library:

Fort Fractal:

Fort fractal was produced by this code.

Recursive Rook:

Recursive rook was produced by this code.

~ by scaryreasoner on July 4, 2011.

2 Responses to “OpenSCAD metaprograming in C”

  1. Please can you send me the code for the second object?

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

%d bloggers like this: