So I've written a script which does this:
Before:
The code is:
#fname - filename of imported .stl
#thic - required thickness of output model
#txt - ascii or binary stl
#cyc - cycles of smoothing
#cut - times triangle divided
#fac - smoothing factor
#per - smoothing reps
#bbX - bounding box X
#bbY - bounding box Y
#bbZ - bounding box Z
#trX, trY, trZ - count of elements in array
def s_surface(fname, thic, txt, cyc, cut, fac, rep, bbX, bbY, bbZ, trX, trY, trZ ):
bb = [[(bbX/2, 0, 0), (1,0,0), True, False],
[(-1*bbX/2, 0, 0), (1,0,0), False, True],
[(0, bbY/2, 0), (0,1,0), True, False],
[(0,-1*bbY/2, 0), (0,1,0), False, True],
[(0, 0, bbZ/2), (0,0,1), True, False],
[(0, 0, -1*bbZ/2), (0,0,1), False, True]
]
tr = [trX, trY, trZ]
bpy.ops.import_mesh.stl(filepath=fname)
ob_new = bpy.context.selected_objects[0]
bpy.context.scene.objects.active = ob_new
bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN')
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.normals_make_consistent(inside=False)
bpy.ops.mesh.remove_doubles(threshold=0.02)
for i in range(cyc):
bpy.ops.mesh.subdivide(number_cuts=cut)
bpy.ops.mesh.vertices_smooth(factor=fac, repeat=rep, xaxis=True, yaxis=True, zaxis=True)
bpy.ops.mesh.normals_make_consistent(inside=False)
for i in range(len(bb)):
bpy.ops.mesh.bisect(plane_co=bb[i][0],plane_no=bb[i][1], clear_outer=bb[i][2],clear_inner=bb[i][3])
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.normals_make_consistent(inside=False)
bpy.ops.object.mode_set(mode='OBJECT')
for i in range(3):
bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN')
bpy.ops.object.modifier_add(type='ARRAY')
bpy.context.object.modifiers["Array"].count = tr[i]
bpy.context.object.modifiers["Array"].relative_offset_displace[0] = 0
bpy.context.object.modifiers["Array"].relative_offset_displace[i] = 1
bpy.context.object.modifiers["Array"].use_merge_vertices = True
bpy.context.object.modifiers["Array"].merge_threshold = 0.01
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Array")
bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN')
bpy.ops.object.modifier_add(type='SOLIDIFY')
bpy.context.object.modifiers["Solidify"].thickness = thic
bpy.context.object.modifiers["Solidify"].use_quality_normals = True
bpy.context.object.modifiers["Solidify"].use_even_offset = True
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Solidify")
nfname = ""
nfname = fname[0:-4] + ".stl"
bpy.ops.export_mesh.stl(filepath=nfname, ascii = txt)
Surfaces I am trying to smooth are supposed to be triply periodic minimal surfaces, so it means that after smoothing is applied mean curvature is meant to be 0 in all vertexes of a mesh. How can I calculate mean curvature to check if smoothing is done right?
bpy.ops
. Most coders write blender scripts without bpy.ops, since it is slower, context-sensitive and more difficult to read. This answer provides some generalized explanation. Every bpy.ops triggers a scene update. $\endgroup$ – Leander Aug 2 '19 at 7:29