Loading
Hey! This thing is still a Work in Progress. Files, instructions, and other stuff might change!

STL to OpenSCAD python converter

by fma, published

STL to OpenSCAD python converter by fma Mar 22, 2013

Description

Here is a standalone version of the online STL to OpenSCAD converter, by Riham.

2014-04-06: added USE_FACES flag to generate polyhedron using "faces=" param, instead of "triangles=" (default enabled).

Recent Comments

view all
Thanks very much. That loaded into Openscad just fine. Of course *now* I'm getting the dreaded "No top level geometry to render" but I think that's an Openscad version issue. I'll have to try stl2scad on my Sparc machine (as soon as I can figure out why it's crashing randomly).
Well that's what I'm running - 2.7.8, only under XP instead of Linux. I just tried it again - all it prints for a traceback is those seven lines.

So if it worked for you, any chance you could send me the result so I can at least proceed with my project? I'd be very grateful.

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

Instructions

  • save the python script
  • make it executable
  • convert STL using: ./stl2scad.py

Look at https://www.logre.eu/wiki/Convertisseur_STL_vers_SCAD for future updates.

Didn't work for me. I got:

C:Python34>stl2scad.py SamsungConvoyFrontRepaired.stl
File "C:Python34stl2scad.py", line 56
print "Processing object %s..." % solidName
^
SyntaxError: Missing parentheses in call to 'print'

C:Python34>

This is with XP SP3.
The script is written for python2.x; it needs some (minor) modifications to run with python3.x. Replacing print statements by calls to print() function is the first thing to do.
Nov 23, 2014 - Modified Nov 23, 2014
Michele31415 - in reply to fma
Thanks for the quick reply. Since this was the only thing I've ever used Python for, it was easier to just install v. 2.7.8. But it still doesn't work:

C:Python27>python
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> stl2scad.py SamsungConvoyFrontRepaired.stl
File "{stdin}", line 1
stl2scad.py SamsungConvoyFrontRepaired.stl
^
SyntaxError: invalid syntax
>>>

(In the original fixed-pitch font of the cmd window, the caret is actually pointing to the "d" just before ".stl", and the line that begins "File:" has angle brackets. For some reason, this comment system deletes everything inside angle brackets, so I had to edit them into curly brackets to get it to post at all.)
If you want to run th python script, you need to use the following syntax:
C:Python27>python set2scad.py SamsungConvoyFrontRepaired.stl
Oh duh! Well shows you how little I know about Python. Anyway, now it's at least doing something different:

C:Python27>python stl2scad.py SamsungConvoyFrontRepaired.stl
binary file
found 654 faces
Traceback (most recent call last):
File "stl2scad.py", line 153, in
main()
File "stl2scad.py", line 143, in main
modules = parseBinary(inputFile)
File "stl2scad.py", line 100, in parseBinary
vertex = struct.unpack("
Last part of the traceback is missing... Could you upload you file on a file transfert service, so I can have a look?
That was all the output it generated. I've uploaded the file here:

filesanywhere.com/fs/v.aspx?v=8b6e67865c6770b2a1aa

Thanks.
Strange, is works fine for me. I'm using python 2.7.8 under linux... And you should get a complete traceback; this is not normal.
Well that's what I'm running - 2.7.8, only under XP instead of Linux. I just tried it again - all it prints for a traceback is those seven lines.

So if it worked for you, any chance you could send me the result so I can at least proceed with my project? I'd be very grateful.
Thanks very much. That loaded into Openscad just fine. Of course *now* I'm getting the dreaded "No top level geometry to render" but I think that's an Openscad version issue. I'll have to try stl2scad on my Sparc machine (as soon as I can figure out why it's crashing randomly).
This tool is awesome! I just used it to generate OpenSCAD versions of a pile of STL files (for the e-NABLE project, enablingthefuture.org) and it worked perfectly!

FYI, I did get one warning, "DEPRECATED: polyhedron(triangles=[]) will be removed in future releases. Use polyhedron(faces=[]) instead." but it works fine in OpenSCAD 2014.03.
fma - in reply to laird
Thanks! I'll fix the warning...
laird - in reply to fma
Perhaps related to this warning:

The scad your tool produced worked _great_ in OpenSCAD on my desktop, but it doesn't render in Customizer (http://www.thingiverse.com/thing:285009). I can't see any error messages or warnings. But what I can see is that the geometry that's converted from STL doesn't render in Customizer, but the geometry that's "native OpenSCAD" renders.
Assembly of e-NABLE components WORK IN PROGRESS
by laird
fma - in reply to laird
Maybe an openscad version issue... Does it work if you replace triangles by faces?
fma - in reply to fma
I uploaded a new release, with a flag to generate either "faces=" or "triangles=" param. Enabled by default, for OpenSCAD versions > 2014.03. Set to False if you use an older version.
I think I've found a bug which prevents this script from working for me on Win7.

I am using "Binary.stl" attached to Riham's "RTL to OpenSCAD Converter" as a test case.

The script fails at line 98 with the error "struct.error: unpack requires a string argument of length 12".

So, the script is getting an unexpected number of bytes from file.read, in the middle of parsing a vertex. After some debug, it seems this is caused by interpreting 0x1A bytes in the file as EOF and returning early from file.read().

To fix this, I made the following change to the script:

#inputFile = file(inputFileName)

inputFile = open(inputFileName, 'rb')

In the python documentation on "file()" it says, "When opening a file, it’s preferable to use open() instead of invoking this constructor directly."

Python documentation describes open() mode this way: "The default is to use text mode, which may convert 'n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.)"

In my testing, open() mode "rb" worked for both ASCII and Binary STL files.
Mmm, you're right! I'll fix that ASAP. Thanks for reporting :)
fma - in reply to fma
I rechecked, and I don't have any problem... Are you sure you didn't
modify the binary file while downloading it
(md5=10e4aa48f516619d748333e3b9b206d5)? What python version are you
using ? What plateform?
I think I've found a bug which prevents this script from working for me on Win7.

I am using "Binary.stl" attached to Riham's "RTL to OpenSCAD Converter" as a test case.

I get the following output from the script:
Traceback (most recent call last):
File "http://debug2_stl2scad.py", line 150, in <module>
main()
File "http://debug2_stl2scad.py", line 140, in main
modules = parseBinary(inputFile)
File "http://debug2_stl2scad.py", line 98, in parseBinary
vertex = struct.unpack("<fff", "file()"="" "rb"="" "the="" "when="" #inputfile="file(inputFileName)" 'n'="" 'b'="" 'rb')="" (appending="" 0x1a="" a="" after="" an="" and="" append="" arguments="" as="" ascii="" back="" binary="" both="" by="" bytes="" caused="" change="" characters="" constructor="" convert="" debug,="" default="" describes="" differently,="" directly."="" documentation="" documentation.)"="" don’t="" early="" eof="" even="" file="" file,="" file.read().="" file.read,="" files="" files.="" fix="" following="" for="" from="" getting="" i="" improve="" in="" inputfile="open(inputFileName," instead="" interpreting="" invoking="" is="" it="" it’s="" keyword="" mad="" may="" middle="" mode="" mode,="" my="" no="" number="" of="" on="" open="" open()="" opening="" parsing="" platform-specific="" portability.="" preferable="" python="" reading.="" representation="" returning="" s="inputFile.read(3*4))" says,="" script="" script:="" seems="" serves="" should="" so,="" some="" stl="" systems="" takes="" testing,="" text="" that="" the="" this="" this,="" thus,="" to="" treat="" typeerror:="" unexpected="" unpack()="" use="" useful="" value="" vertex.="" way:="" when="" where="" which="" will="" worked="" writing="" you=""></fff",></module>
I think I've found a bug, which prevented this script from working for me on Win7. As a test case, I used "Binary.stl" attached to Riham's "STL to OpenSCAD converter".

The script would terminate early with this output:

binary file
found 1512 triangles
Traceback (most recent call last):
File "http://debug2_stl2scad.py", line 148, in <module>
main()
File "http://debug2_stl2scad.py", line 138, in main
modules = parseBinary(inputFile)
File "http://debug2_stl2scad.py", line 98, in parseBinary
vertex = struct.unpack("<fff", "file()"="" "rb"="" "the="" "when="" #inputfile="file(inputFileName)" 'n'="" 'b'="" 'rb')="" (appending="" 0x1a="" 12="" a="" about="" an="" and="" append="" argument="" as="" ascii="" back="" because="" believe="" binary="" both="" bytes="" change="" characters="" constructor="" convert="" default="" descibe="" differently,="" directly."="" docs="" documentation.)"="" don’t="" eof="" even="" file="" file,="" file.read()="" files="" files.="" for="" function="" i="" improve="" in="" incorrectly="" inputfile="open(inputFileName," inputfile.read(3*4))="" instead="" interpreting="" invoking="" is="" it="" it’s="" length="" made="" may="" middle="" mode="" mode,="" mode:="" my="" number="" of="" on="" open="" open()="" open(),="" opening="" platform-specific="" portability.="" preferable="" prematurely.="" python="" read.="" reading.="" representation="" requires="" returning="" says,="" script.="" serves="" should="" so,="" stl="" string="" struct.error:="" systems="" terminating="" testing,="" text="" that="" the="" this="" thus,="" to="" treat="" unexpected="" unpack="" use="" useful="" value="" vertex="" was="" when="" where="" which="" will="" works="" writing="" you=""></fff",></module>
Many thanks to both of you!

This is an absolute life-saver for reverse engineering sketchup files.
fma - in reply to ajd4096
:o)
It's always pleasant when things like this arrive JUST as you find you need them! Thank you for this fma
fma - in reply to M_G
All credit goes to Riham! I was also looking for such tool...

Now, we can hack all designs ;)
how make it executable?
thanks you fma
You first need to install a python interpreter. Once done, under Windows, you only need to double-click on the file. Under linux, use 'chmod +x <script>', then './<script>' to launch it.</script>
Rookie - in reply to fma
I get this

./http://stl2scad.py
Traceback (most recent call last):
File "./http://stl2scad.py", line 148, in <module>
main()
File "./http://stl2scad.py", line 129, in main
inputFileName = sys.argv[1]
IndexError: list index out of range</module>
fma - in reply to Rookie
You need to give the name of the file to convert after ./http://stl2scad.py... This is a command-line tool, without GUI.
Rookie - in reply to fma
never mind I fixed

thks...!!!
Top