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
0 Share
Download All Files

Thing Apps Enabled

Order This Printed View All Apps

Contents

Use This Project

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

14223Views 3458Downloads

Summary

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

2015-05-27: see http://www.thingiverse.com/thing:850853 for python3 port.

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

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.

All Apps

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

App Info Launch App

This App connects Thingiverse with Makeprintable, a cloud-based mesh repair service that analyzes, validates and repairs most common mesh errors that can occur when preparing a 3D design file for p...

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
May 26, 2015 - Modified May 26, 2015

i tried to edit.
but i got this error

WARNING: Object may not be a valid 2-manifold and may need repair! See http://en.wikibooks.org/wiki/OpenSCAD_User_Manual/STL_Import_and_Export

all i add is

difference(){
object1();
translate([0,10,5]){
cylinder(h=1,r=6.5);
}
}

anyone know how to edit? trying to make a hole in the object created

Tyr to use netfabb basic to repair the stl before converting it.

Sorry new to OpenSCAD... With the file converted to what looks like g-code, how can I make changes to the object?

module OpenSCAD_Model() {
polyhedron(
points=[
[0.0, 0.0, 1.0],
[0.0, 1.0, 1.0],
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
[0.0, 1.0, 1.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[1.0, 0.0, 1.0],
[1.0, 1.0, 1.0],
[0.0, 1.0, 1.0],
[0.0, 0.0, 1.0],
[1.0, 1.0, 1.0],
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 0.0],
[1.0, 0.0, 1.0],
[0.0, 1.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 1.0],
[1.0, 1.0, 1.0],
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 1.0, 1.0],
[1.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[1.0, 1.0, 1.0],
[1.0, 0.0, 1.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 1.0]
],
faces=[
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11],
[12, 13, 14],
[15, 16, 17],
[18, 19, 20],
[21, 22, 23],
[24, 25, 26],
[27, 28, 29],
[30, 31, 32],
[33, 34, 35]
]
);
}

OpenSCAD_Model();

Wel, you have to edit vertices/faces, which is not really easy!

The script worked fine. What's the difference b/t say, modifying an object imported via an .scad file and doing the same but as a .stl?

From a technical point of view, I don't really know. But it allows you to have only 1 file...

Jan 3, 2015 - Modified Jan 3, 2015

great tool, works great for me for almost all models i tried it with.
Today however i tried to use the tool on a blank iphone case from thingiverse (http://www.thingiverse.com/thing:126949) that i wanted to customize, and for this model only all the resulting faces of the polyhedron are facing the wrong direction (vertices are in the wrong order) and as a result i can't render the object with F6 (compiling works fine).

has anyone else had this problem and do you know of any way to fix this in the convertor itself. (so not just inverting all the faces in the .scad)

Blank iPhone 5 case

If all faces are reverted, try to add:

face[0], face[2] = face[2], face[0]

juste before the lines:

faces.append(str(face))

at the same indentation level. This will revert the order of vertices of each face.

thanks for the fast reply, worked like a charm to invert the faces. i still cant get it to render though but ill look into that later

Didn't work for me. I got:

C:\Python34>stl2scad.py SamsungConvoyFrontRepaired.stl
File "C:\Python34\stl2scad.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("<fff", inputFile.read(3*4))
struct.error: unpack requires a string argument of length 12

C:\Python27>

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:

https://www.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, http://enablingthefuture.orghttp://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)http://www.thingiverse.com/thi.... 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.pydebug2_stl2scad.py", line 150, in <module>
main()
File "http://debug2_stl2scad.pydebug2_stl2scad.py", line 140, in main
modules = parseBinary(inputFile)
File "http://debug2_stl2scad.pydebug2_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.pydebug2_stl2scad.py", line 148, in <module>
main()
File "http://debug2_stl2scad.pydebug2_stl2scad.py", line 138, in main
modules = parseBinary(inputFile)
File "http://debug2_stl2scad.pydebug2_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.pystl2scad.py
Traceback (most recent call last):
File "./http://stl2scad.pystl2scad.py", line 148, in <module>
main()
File "./http://stl2scad.pystl2scad.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.pystl2scad.py... This is a command-line tool, without GUI.

Rookie - in reply to fma

never mind I fixed

thks...!!!

Top