-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduce_polygons.py
49 lines (44 loc) · 1.82 KB
/
reduce_polygons.py
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
48
49
import bpy
from PIL import Image
decimate_proportion = 0.5 # 精简比例
# 规则:
# 1.面数在各个区间范围内的 精简比例各是多少
# 2.png 图片转化为 jpg 压缩比例是多少
# 3.导出为 bin + gltf + textures 外部文件引用(相对路径)
# 精简 mesh
all_obj = bpy.data.objects
for i in all_obj:
if i.type == 'MESH':
if len(i.data.polygons) > 1000:
decimate = i.modifiers.new(name='DECIMATE', type='DECIMATE') # 新建精简修改器
decimate.ratio = 0.001 # 比率
count = 0
all_obj = bpy.context.selected_objects[:]
for i in all_obj:
if i.type == 'MESH':
a = len(i.data.polygons) # 面数
print(a)
count += a
b = i.modifiers['DECIMATE'].face_count
print(a, b)
# if b > a:
# print(a, b, i.name)
all_image = bpy.data.images
for i in all_image:
if i.file_format == 'PNG':
i.file_format = 'JPEG'
filepath = bpy.path.abspath('//textures/{}.jpg'.format('test'))
i.save_render(filepath)
image = Image.open(filepath)
image.save(filepath, quality=95) # 压缩 不是缩小尺寸
ex_path = r'C:\Users\thn\Desktop\精简测试\精简.gltf'
# 应用修改器导出glb
# export_apply 应用修改器
# export_format GLTF_SEPARATE gltf 分离式
# export_texture_dir 外部资源路径
# export_image_format PNG 图片转化为 JPEG
bpy.ops.export_scene.gltf(filepath=ex_path, export_format='GLTF_SEPARATE', export_colors=True,
export_texture_dir='textures',
export_draco_mesh_compression_enable=True, export_materials='EXPORT',
export_cameras=True, use_selection=False, use_visible=False, export_apply=True,
export_lights=True, export_selected=False, export_image_format='JPEG')