我想在Blender中移动一个网格,使其最低点的z值为0。这样最低点就会在Z = 0平面上。由于我首先按其最大轴缩放模型,所以这更具挑战性。我正在尝试使这对任何单个网格模型都适用。
这是我当前的尝试:
mesh_obj = bpy.context.scene.objects[0]
# I first find the min and max of the mesh. The largest and smallest points on the Z-axis
max_floats = [mesh_obj.data.vertices[0].co[0], mesh_obj.data.vertices[0].co[1], mesh_obj.data.vertices[0].co[2]]
min_floats = [mesh_obj.data.vertices[0].co[0], mesh_obj.data.vertices[0].co[1], mesh_obj.data.vertices[0].co[2]]
for vertex in mesh_obj.data.vertices:
if vertex.co[0] > max_floats[0]:
max_floats[0] = vertex.co[0]
if vertex.co[0] < min_floats[0]:
min_floats[0] = vertex.co[0]
if vertex.co[1] > max_floats[1]:
max_floats[1] = vertex.co[1]
if vertex.co[1] < min_floats[1]:
min_floats[1] = vertex.co[1]
if vertex.co[2] > max_floats[2]:
max_floats[2] = vertex.co[2]
if vertex.co[2] < min_floats[2]:
min_floats[2] = vertex.co[2]
max_float = max(max_floats)
min_float = min(min_floats)
# I then get the the point with the biggest magnitude
if max_float < math.fabs(min_float):
max_float = math.fabs(min_float)
recip = 1.0 / max_float
# And use that point to scale the model
# This needs to be taken into account when moving the model by its min z_point
# since that point is now smaller
mesh_obj.scale.x = recip
mesh_obj.scale.y = recip
mesh_obj.scale.z = recip
# naive attempt at moving the model so the lowest z-point = 0
mesh_obj.location.z = -(min_floats[2] * recip / 2.0)