如何使用 Blender 脚本删除场景中较小的多个对象?

How to delete smaller multiple objects in my scene using Blender script?

我正在使用 Blender 2.8。我想将一个对象导入到搅拌机中,该对象由几个未连接的部分组成。所以我想将对象拆分并只导出最大的部分。

假设一个物体有 3 个部分,一大两小。我能够将这个对象变成三个对象,每个对象包含一个片段。我想删除两个较小的对象,只保留最大的一个。我在想也许以某种方式找到三个不同对象的表面积并且只保留最大的同时删除所有其他对象?我是 Blender 的新手。

bpy.ops.import_mesh.stl(filepath='path/of/file.stl')
bpy.ops.mesh.separate(type='LOOSE')
amount_of_pieces = len(context.selected_objects)

if amount_of_pieces > 1:
    highest_surface_area = 0

    #the rest is pseudocode
    for object in scene:
        if object.area > highest_surface_area:
            highest_surface_area = object.area
        else:
            bpy.ops.object.delete()

bpy.ops.export_mesh.stl(filepath='path/of/new/file.stl')

我能够编写适用于此的代码,但是它又长又乱。如果有人能给我一些清理方面的建议,我将不胜感激。

import bpy
import os
import bmesh


context = bpy.context

file = (r'path\to\file.stl')

bpy.ops.import_mesh.stl(filepath= file)
fileName = os.path.basename(file)[:-4].capitalize()
bpy.ops.mesh.separate(type='LOOSE')
bpy.ops.object.select_all(action='SELECT')
piece = len(context.selected_objects)
bpy.ops.object.select_all(action='DESELECT')

high = 0
if piece > 1:
    bpy.data.objects[fileName].select_set(True)
    obj = bpy.context.active_object
    bm = bmesh.new()
    bm.from_mesh(obj.data)
    area = sum(f.calc_area() for f in bm.faces)
    high = area
    bm.free()
    bpy.ops.object.select_all(action='DESELECT')

    for x in range (1, piece):
        name = fileName + '.00' + str(x)
        object = bpy.data.objects[name]
        context.view_layer.objects.active = object
        bpy.data.objects[name].select_set(True)
        obj = bpy.context.active_object
        bm = bmesh.new()
        bm.from_mesh(obj.data)
        newArea = sum(f.calc_area() for f in bm.faces)
        bm.free()
        if newArea > high:
            high = newArea
            bpy.ops.object.select_all(action='DESELECT')
        else:
            bpy.ops.object.delete()
        bpy.ops.object.select_all(action='DESELECT')

    if area != high:
        bpy.data.objects[fileName].select_set(True)
        bpy.ops.object.delete()


bpy.ops.export_mesh.stl(filepath= 'path/to/export/file.stl')
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False, confirm=False)

步骤是:-

  • 导入文件
  • 分成多个对象
  • 为了安全起见,获取网格对象列表
  • 列出每个物体的表面积
  • 从区域列表中获取最大值
  • 删除不是最大的对象
  • 出口最大
  • 清理

我们不需要使用 bmesh to get the surface area, the normal mesh data includes polygon.area

使用list comprehension,我们可以将大部分步骤都放在一行中。

import bpy

# import and separate
file = (r'path/of/file.stl')
bpy.ops.import_mesh.stl(filepath= file)
bpy.ops.mesh.separate(type='LOOSE')

# list of mesh objects
mesh_objs = [o for o in bpy.context.scene.objects
                if o.type == 'MESH']

# dict with surface area of each object
obj_areas = {o:sum([f.area for f in o.data.polygons])
                for o in mesh_objs}

# which is biggest
big_obj = max(obj_areas, key=obj_areas.get)

# select and delete not biggest
[o.select_set(o is not big_obj) for o in mesh_objs]
bpy.ops.object.delete(use_global=False, confirm=False)

#export
bpy.ops.export_mesh.stl(filepath= 'path/of/new/file.stl')

# cleanup
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False, confirm=False)