Blender - 移动网格,使最小Z点位于Z = 0平面上

时间:2015-01-20 00:16:41

标签: python translation scale blender mesh

我正在尝试在混合器中移动网格,以使最低的z点为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)

2 个答案:

答案 0 :(得分:6)

您应该知道的第一件事是顶点位置在对象空间中 - 与距离对象原点的距离一样。您需要使用对象matrix_world将其转换为世界空间。

如果您只希望最低z位置等于零,则可以忽略x和y值。

import bpy
import mathutils

mesh_obj = bpy.context.active_object

minz = 999999.0

for vertex in mesh_obj.data.vertices:
    # object vertices are in object space, translate to world space
    v_world = mesh_obj.matrix_world * mathutils.Vector((vertex.co[0],vertex.co[1],vertex.co[2]))

    if v_world[2] < minz:
        minz = v_world[2]

mesh_obj.location.z = mesh_obj.location.z - minz

答案 1 :(得分:2)

另一种解决方案:

def object_lowest_point(obj):
    matrix_w = obj.matrix_world
    vectors = [matrix_w * vertex.co for vertex in obj.data.vertices]
    return min(vectors, key=lambda item: item.z)