Convert STL to obj

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Convert STL to obj

Post by wmayer »

I meant why it seemed faster to convert with Mesh.export() compared to Mesh.Mesh().write(), not STL compared to obj. That is really much faster writing STL.
Mesh.export() cannot be faster than Mesh.Mesh().write() because internally it has a much bigger overhead and it uses the same functions to write the STL files. Now I wonder if you used it correctly: what is the file size of the created STL file? If it's only 84 bytes then you can be sure that it contains no mesh data.
Wow, that was fast, nice!
For further testing purposes I first saved the data into a buffer in memory and then wrote the whole content in one go to the file. The measured time doesn't decrease any more, it's even slightly slower.
clel
Posts: 39
Joined: Mon Jun 13, 2016 10:30 am

Re: Convert STL to obj

Post by clel »

wmayer wrote: Wed Feb 26, 2020 1:35 pm
I meant why it seemed faster to convert with Mesh.export() compared to Mesh.Mesh().write(), not STL compared to obj. That is really much faster writing STL.
Mesh.export() cannot be faster than Mesh.Mesh().write() because internally it has a much bigger overhead and it uses the same functions to write the STL files. Now I wonder if you used it correctly: what is the file size of the created STL file? If it's only 84 bytes then you can be sure that it contains no mesh data.
Sorry again misunderstanding. I meant Mesh.Mesh().write() is really faster writing STL than obj. But my point was that when comparing Mesh.export() and Mesh.Mesh().write() when writing obj files it seemed to me that Mesh.export() was faster. But that was probably only caused because I used a much smaller test file for that.
Wow, that was fast, nice!
For further testing purposes I first saved the data into a buffer in memory and then wrote the whole content in one go to the file. The measured time doesn't decrease any more, it's even slightly slower.
Alright.


Test results with ~24 MB test file:
FreeCADCmd:

Code: Select all

>>> import Mesh
>>> import time
>>>
>>> start = time.time()
>>>
>>> mesh = Mesh.Mesh()
>>> mesh.read("Y:\\00000188_6c4172e8a17140f89d3b1c0a_step_005.stl") # read STL
>>> mesh.write("Y:\\00000188_6c4172e8a17140f89d3b1c0a_step_005.obj") # write OBJ
saving......
>>>
>>> end = time.time()
>>> print(end - start)
483.34871077537537
FreeCAD GUI in Python console:

Code: Select all

>>> print(end - start)
455.2598876953125
Looks like GUI was actually faster, but that can be a coincidence.

Y:\ is a network drive, so that probably makes the problem of disk i/o much worse.

Testing again writing to a local drive (SSD, so that is probably also beneficial):

CMD:

Code: Select all

>>> print(end - start)
12.861110210418701
GUI:

Code: Select all

>>> print(end - start)
12.569581985473633
So for now I will change the batch script to use the local drive for output.
Rock007
Posts: 5
Joined: Wed Jun 02, 2021 8:26 am

Re: Convert STL to obj

Post by Rock007 »

wmayer wrote: Fri Feb 21, 2020 4:06 pm Obviously this isn't the complete script but according to the function calls "Mesh" in your script is apparently the Mesh module and not an instance of the Mesh type.
So, yes your script is somewhat inefficient because it creates a document, creates a mesh feature and copies the loaded mesh object. This all is superfluous for only converting to a different file format.

Instead try this (but not tested)

Code: Select all

import Mesh
for entry in os.scandir(inputDir):
    importPath = entry.path
    if ".stl" in importPath:
        exportPath = importPath.rsplit('.', 1)[0] + '.obj'
        exportPath = exportPath.replace(inputFolder, outputFolder)
        mesh = Mesh.Mesh() # creates an instance of the Mesh object
        mesh.read(importPath) # read STL
        mesh.write(exportPath) # write OBJ
Hello, I need a similar capability for the conversion of bulk of STEP files from a directory to OBJ files.
Can any one share the complete code to run it without GUI or FreeCAD for Python 3.6.5.
Thanks.
Post Reply