[英]Blender - Move mesh so that smallest Z point is on the Z = 0 plane
我試圖在攪拌機中移動網格,以便最低的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)
您應該知道的第一件事是頂點位置在對象空間中 - 與距離對象原點的距離一樣。 您需要使用對象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
另一種方案:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.