91 lines
3.5 KiB
Python
91 lines
3.5 KiB
Python
import bpy, sys, os, math
|
|
|
|
pid = '26385'
|
|
workdir = '/home/water/Downloads/'
|
|
|
|
filename = f'{workdir}{pid}/{pid}_9cm_x1.obj'
|
|
bpy.ops.import_scene.obj(filepath=filename)
|
|
# 坐标复位
|
|
obj = bpy.context.selected_objects[0]
|
|
obj.rotation_euler[0] = 0
|
|
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
|
|
bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_VOLUME', center='MEDIAN')
|
|
bpy.ops.object.align(align_mode='OPT_1', relative_to='OPT_1', align_axis={'Y', 'Z'})
|
|
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
|
|
# bpy.ops.export_scene.obj(filepath=f'{workdir}{pid}_align_yz.obj')
|
|
|
|
# 躺平到打印机排版需要的坐标与角度
|
|
obj.rotation_euler = (math.radians(90), math.radians(90), 0)
|
|
bpy.ops.object.transform_apply(rotation=True)
|
|
# bpy.ops.export_scene.obj(filepath=f'{workdir}{pid}_rotate_y90.obj')
|
|
|
|
heights = {}
|
|
min_height = 999999
|
|
min_i = 0
|
|
max_height = -999999
|
|
max_i = 0
|
|
|
|
bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_VOLUME', center='MEDIAN')
|
|
bpy.ops.object.align(align_mode='OPT_1', relative_to='OPT_3', align_axis={'X', 'Y', 'Z'})
|
|
|
|
# 步进精度2旋转X轴到180度,找到Y轴最低点和最高点,其中最低点为打印
|
|
step = 2
|
|
i = 0
|
|
while i <= 180:
|
|
obj.rotation_euler = (math.radians(step), 0, 0)
|
|
bpy.ops.object.transform_apply(rotation=True)
|
|
if obj.dimensions[1] < min_height:
|
|
min_height = obj.dimensions[1]
|
|
min_i = i
|
|
if obj.dimensions[1] > max_height:
|
|
max_height = obj.dimensions[1]
|
|
max_i = i
|
|
heights[i] = (obj.dimensions[0], obj.dimensions[1], obj.dimensions[2])
|
|
print(i, heights[i])
|
|
i += step
|
|
|
|
obj.rotation_euler = (0, 0, 0)
|
|
bpy.ops.object.transform_apply(rotation=True)
|
|
obj.rotation_euler = (math.radians(min_i), 0, 0)
|
|
bpy.ops.object.transform_apply(rotation=True)
|
|
#bpy.ops.export_scene.obj(filepath=f'{workdir}{pid}_miny.obj')
|
|
print(f'最小高度: {min_height} @ {heights[min_i]}min_i:{min_i}' , f'最大高度: {max_height} @ {heights[max_i]}max_i:{max_i}')
|
|
|
|
offset = 45.5
|
|
radian = math.radians(90)
|
|
bpy.ops.mesh.primitive_plane_add(size=200, enter_editmode=False, align='WORLD', location=(offset, 0, 0), rotation=(0, radian, 0), scale=(1, 1, 1))
|
|
|
|
# 布尔切割,保留交集切面
|
|
bpy.ops.object.modifier_add(type='BOOLEAN')
|
|
bpy.context.object.modifiers["Boolean"].object = bpy.data.objects[pid]
|
|
bpy.context.object.modifiers["Boolean"].operation = 'INTERSECT'
|
|
bpy.context.object.modifiers["Boolean"].solver = 'FAST'
|
|
bpy.ops.object.modifier_apply(modifier="Boolean")
|
|
|
|
# 拆分切割面为多个多边形,然后遍历多边形,找到最大的面积
|
|
bpy.ops.mesh.separate(type='LOOSE')
|
|
|
|
max_area = 0
|
|
max_obj = None
|
|
for obj in bpy.data.objects:
|
|
if obj.type == 'MESH' and obj.name.startswith('Plane'):
|
|
area = obj.data.polygons[0].area
|
|
if area > max_area:
|
|
max_area = area
|
|
max_obj = obj
|
|
|
|
# 选中最大面积的多边形,然后计算中心点
|
|
bpy.ops.object.select_all(action='DESELECT')
|
|
max_obj.select_set(True)
|
|
bpy.context.view_layer.objects.active = max_obj
|
|
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
|
|
|
|
bpy.ops.import_scene.obj(filepath=f'{workdir}{pid}/qrcode.obj')
|
|
qr_obj = bpy.data.objects['qrcode']
|
|
shore_obj = bpy.data.objects['Cube.001']
|
|
qr_obj.location = (max_obj.location[0] - qr_obj.dimensions[0] / 2 - shore_obj.dimensions[0], max_obj.location[1], max_obj.location[2])
|
|
shore_obj.location = (qr_obj.location[0]-0.01, max_obj.location[1], max_obj.location[2])
|
|
|
|
for obj in bpy.data.objects:
|
|
if obj.type == 'MESH' and obj.name.startswith('Plane'):
|
|
bpy.data.objects.remove(obj) |