import bmesh
import bpy
def stretchAxis(mesh, idx, tgt_min, tgt_max):
""" Squish/stretch the vertex coordinates of a mesh so that their .co[idx] ranges from tgt_min to tgt_max. """
bm = bmesh.new()
bm.from_mesh(mesh)
bm.verts.ensure_lookup_table()
min = bm.verts[0].co[idx]
max = min
for v in bm.verts:
val = v.co[idx]
if (val<min):
min = val
if (val > max):
max = val
# remap co[idx] from min..max to tgt_min..tgt_max
for v in bm.verts:
old = v.co[idx]
v.co[idx] = (old-min)*(tgt_max-tgt_min) / (max-min) +tgt_min
bm.to_mesh(mesh)
bm.free()
#
# squish or stretch all the selected meshes to a specific thickness along a
# specific axis.
#
idx = 2 # the z coordinate
for obj in bpy.context.scene.objects:
if (obj.select):
stretchAxis(obj.data, idx, 0, 1)
obj.location[idx] = 1
obj.scale[idx] = 1
|
Blender python API quick-start
Syntax highlighting by Pygments.