2024-11-3-早期用到的一个blender去除重叠物体的python代码

检测场景中的重叠对象并随机删除每对重叠对象中的一个
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  

import bpy
import mathutils
import random

# 获取所有对象
objects = list(bpy.context.scene.objects)

# 清除当前选择
bpy.ops.object.select_all(action='DESELECT')

# 存储已经删除的对象,以避免重复删除
deleted_objects = set()

# 检测重叠对象并记录重叠对
overlap_pairs = []

for i, obj in enumerate(objects):
if obj.name in deleted_objects:
continue

bbox_corners = [obj.matrix_world @ mathutils.Vector(corner) for corner in obj.bound_box]

for j, other_obj in enumerate(objects):
if i >= j or other_obj.name in deleted_objects:
continue

other_bbox_corners = [other_obj.matrix_world @ mathutils.Vector(corner) for corner in other_obj.bound_box]

if (min(c[0] for c in bbox_corners) <= max(c[0] for c in other_bbox_corners) and
max(c[0] for c in bbox_corners) >= min(c[0] for c in other_bbox_corners) and
min(c[1] for c in bbox_corners) <= max(c[1] for c in other_bbox_corners) and
max(c[1] for c in bbox_corners) >= min(c[1] for c in other_bbox_corners) and
min(c[2] for c in bbox_corners) <= max(c[2] for c in other_bbox_corners) and
max(c[2] for c in bbox_corners) >= min(c[2] for c in other_bbox_corners)):

overlap_pairs.append((obj.name, other_obj.name))

# 随机删除每对重叠对象中的一个
for obj_name, other_obj_name in overlap_pairs:
to_delete_name = random.choice([obj_name, other_obj_name])
deleted_objects.add(to_delete_name)
to_delete = bpy.data.objects.get(to_delete_name)
if to_delete:
bpy.data.objects.remove(to_delete, do_unlink=True)

仅仅对选中的所有对象检测重叠并随机删除每对重叠对象中的一个
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  

import bpy
import mathutils
import random

# 获取选中的对象
selected_objects = [obj for obj in bpy.context.selected_objects]

# 存储已经删除的对象,以避免重复删除
deleted_objects = set()

# 检测重叠对象并记录重叠对
overlap_pairs = []

for i, obj in enumerate(selected_objects):
if obj.name in deleted_objects:
continue

bbox_corners = [obj.matrix_world @ mathutils.Vector(corner) for corner in obj.bound_box]

for j, other_obj in enumerate(selected_objects):
if i >= j or other_obj.name in deleted_objects:
continue

other_bbox_corners = [other_obj.matrix_world @ mathutils.Vector(corner) for corner in other_obj.bound_box]

if (min(c[0] for c in bbox_corners) <= max(c[0] for c in other_bbox_corners) and
max(c[0] for c in bbox_corners) >= min(c[0] for c in other_bbox_corners) and
min(c[1] for c in bbox_corners) <= max(c[1] for c in other_bbox_corners) and
max(c[1] for c in bbox_corners) >= min(c[1] for c in other_bbox_corners) and
min(c[2] for c in bbox_corners) <= max(c[2] for c in other_bbox_corners) and
max(c[2] for c in bbox_corners) >= min(c[2] for c in other_bbox_corners)):

overlap_pairs.append((obj.name, other_obj.name))

# 随机删除每对重叠对象中的一个
for obj_name, other_obj_name in overlap_pairs:
to_delete_name = random.choice([obj_name, other_obj_name])
deleted_objects.add(to_delete_name)
to_delete = bpy.data.objects.get(to_delete_name)
if to_delete:
bpy.data.objects.remove(to_delete, do_unlink=True)

2024-10-8-SendToUE第2个医院 2024-10-19-第1个景观实战练习

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×