How to reduce size of the stl file generated through python script?

(Siddharth Sharma) #1

i am using this script to generate a stl file.
i want to reduce the size/quality of the stl file generated through python script. how will i do that?

(techrama) #2

for example

import numpy
from stl import mesh

# Using an existing stl file:
your_mesh = mesh.Mesh.from_file('some_file.stl')

# Or creating a new mesh (make sure not to overwrite the `mesh` import by
# naming it `mesh`):
VERTICE_COUNT = 100
data = numpy.zeros(VERTICE_COUNT, dtype=mesh.Mesh.dtype)
your_mesh = mesh.Mesh(data, remove_empty_areas=False)

# The mesh normals (calculated automatically)
your_mesh.normals
# The mesh vectors
your_mesh.v0, your_mesh.v1, your_mesh.v2
# Accessing individual points (concatenation of v0, v1 and v2 in triplets)
assert (your_mesh.points[0][0:3] == your_mesh.v0[0]).all()
assert (your_mesh.points[0][3:6] == your_mesh.v1[0]).all()
assert (your_mesh.points[0][6:9] == your_mesh.v2[0]).all()
assert (your_mesh.points[1][0:3] == your_mesh.v0[1]).all()

your_mesh.save('new_stl_file.stl')
(Andras Lasso) #3

@techrama Would you like to tell us what the code snippet above is supposed to do?

@Siddharth_Sharma You can decrease size & quality of the generated mesh by adding this line before CreateClosedSurfaceRepresentation method call:

segmentationNode.GetSegmentation().SetConversionParameter("Decimation factor", "0.9") 

The parameter value is between 0.0-1.0 and it specifies the fraction points that you would like to remove from the mesh.

(techrama) #4

Do you still want me to explain or you got it?

(James Butler) #5

Source of @techrama 's code can be found at https://numpy-stl.readthedocs.io/en/latest/usage.html#quickstart, if others want to look more into it. It is listed as a “Quickstart” example set.