The best results I've seen for this is using matplotib and saving a scatter plot. You are able to adjust the resolution or save as a vector once it is in that format:
import matplotlib.pylab as plt
slicex = meshx.section(plane_origin=meshx.centroid, plane_normal=[0,0,30])
slice_2D, to_3D = slicex.to_planar()
fig, ax = plt.subplots(figsize=(16,8))
ax.set_aspect('equal')
_ = ax.scatter(slice_2D.vertices[:,0], slice_2D.vertices[:,1], color='lightgray')
ax.axis('off')
plt.savefig('meshx_slice.png')
More details on the file format and options are here. This also works well for saving a full 2D mesh or a planar view of a 3D mesh, using the Trimesh.vertices as points.
ALTERNATIVE
If you're wanting to replicate what slice_2D.show()
does, you can just borrow from its code (which uses matplotlib):
import matplotlib.pyplot as plt
# keep plot axis scaled the same
plt.axes().set_aspect('equal', 'datalim')
# hardcode a format for each entity type
eformat = {'Line0': {'color': 'g', 'linewidth': 1},
'Line1': {'color': 'y', 'linewidth': 1},
'Arc0': {'color': 'r', 'linewidth': 1},
'Arc1': {'color': 'b', 'linewidth': 1},
'Bezier0': {'color': 'k', 'linewidth': 1},
'Bezier1': {'color': 'k', 'linewidth': 1},
'BSpline0': {'color': 'm', 'linewidth': 1},
'BSpline1': {'color': 'm', 'linewidth': 1}}
for entity in slice_2D.entities:
# if the entity has it's own plot method use it
if hasattr(entity, 'plot'):
entity.plot(slice_2D.vertices)
continue
# otherwise plot the discrete curve
discrete = entity.discrete(slice_2D.vertices)
# a unique key for entities
e_key = entity.__class__.__name__ + str(int(entity.closed))
fmt = eformat[e_key].copy()
if hasattr(entity, 'color'):
# if entity has specified color use it
fmt['color'] = entity.color
plt.plot(*discrete.T, **fmt)
plt.savefig('meshx_slice.png')