blender脚本,

Posted by shensunbo on September 25, 2024

选中大于10000个三角形的对象

1
2
3
4
5
6
7
8
9
10
11
import bpy

bpy.ops.object.select_all(action='DESELECT')

for obj in bpy.data.objects:
    if obj.type == 'MESH':
        tri_count = len(obj.data.polygons)
        
        if tri_count > 10000:
            obj.select_set(True)
            bpy.context.view_layer.objects.active = obj

批量将mesh名称修改为object名称

1
2
3
4
5
6
7
8
9
10
import bpy

for obj in bpy.data.objects:
    if obj.type == 'MESH':
        # 将物体名称同步到关联的网格数据块
        if obj.data.name != obj.name:
            obj.data.name = obj.name

        # 可选:同时修改物体名称的编号后缀(保持唯一性)
        # obj.name = obj.data.name

使用正则表达式批量重命名object和mesh

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
import bpy
import re

def advanced_rename_with_regex():
    """
    使用正则表达式进行更灵活的重命名
    """
    
    renamed_count = 0
    pattern = r'^12(.*?)\.001$'  # 匹配以8开头、以.001结尾的名称
    replacement = r'11\1'         # 替换为7开头,删除.001,保留中间部分
    
    for obj in bpy.context.selected_objects:
        original_name = obj.name
        
        # 使用正则表达式匹配和替换
        if re.match(pattern, original_name):
            new_name = re.sub(pattern, replacement, original_name)
            
            # 重命名对象
            obj.name = new_name
            
            # 重命名网格数据块(如果存在)
            if obj.type == 'MESH' and obj.data:
                mesh_original_name = obj.data.name
                if re.match(pattern, mesh_original_name):
                    mesh_new_name = re.sub(pattern, replacement, mesh_original_name)
                    obj.data.name = mesh_new_name
                    print(f"网格数据块: {mesh_original_name} -> {mesh_new_name}")
            
            print(f"对象: {original_name} -> {new_name}")
            renamed_count += 1
    
    print(f"使用正则表达式重命名完成!共处理 {renamed_count} 个对象")
    return renamed_count

# 运行高级版本
advanced_rename_with_regex()