-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathname_clean_up.py
190 lines (148 loc) · 5.06 KB
/
name_clean_up.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
bl_info = {
"name": "Name Clean Up",
"author": "tianer2820",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "F3 Search Menu > cleanup",
"description": "Easily reformat object names, mesh names and image names",
"warning": "",
"doc_url": "",
"category": "User Interface",
}
import bpy
from bpy import data as D
from mathutils import *
from math import *
def format_name(name: str) -> str:
name = name.lower()
name = name.replace('-', ' ')
name = name.replace('_', ' ')
name = name.strip()
last_c = None
new_name = ''
for c in name:
if not last_c is None:
if c == last_c == ' ':
continue
new_name += c
last_c = c
return new_name
class LowerObjectNames(bpy.types.Operator):
"""Rename all selected objects to lower case"""
bl_idname = "cleanup.lower_object_names"
bl_label = "Lower object names"
@classmethod
def poll(cls, context: bpy.types.Context):
return len(context.view_layer.objects.selected) > 0
def execute(self, context):
self.all_lower(context)
return {'FINISHED'}
def all_lower(self, context: bpy.types.Context):
count = 0
objs = context.view_layer.objects.selected
for obj in objs:
obj: bpy.types.Object
name = obj.name
new_name = format_name(name)
if obj.name != new_name:
count += 1
obj.name = new_name
self.report({'INFO'}, 'renamed {} objects'.format(count))
class LowerImageNames(bpy.types.Operator):
"""Rename all images to lower case"""
bl_idname = "cleanup.lower_image_names"
bl_label = "Lower image names"
@classmethod
def poll(cls, context: bpy.types.Context):
return True
def execute(self, context):
self.lower_img_names(context)
return {'FINISHED'}
def lower_img_names(self, context: bpy.types.Context):
count = 0
images = context.blend_data.images
for img in images:
img: bpy.types.Image
name = img.name
new_name = format_name(name)
if img.name != new_name:
count += 1
img.name = new_name
self.report({'INFO'}, 'renamed {} materials'.format(count))
class SyncMeshNames(bpy.types.Operator):
"""Rename all selected objects to lower case"""
bl_idname = "cleanup.sync_mesh_names"
bl_label = "Sync mesh names"
@classmethod
def poll(cls, context: bpy.types.Context):
return len(context.view_layer.objects.selected) > 0
def execute(self, context):
self.sync_mesh_names(context)
return {'FINISHED'}
def sync_mesh_names(self, context: bpy.types.Context):
count = 0
objs = context.view_layer.objects.selected
for obj in objs:
obj: bpy.types.Object
name = obj.name
mesh = obj.data
if mesh.users == 1:
if mesh.name != name:
count += 1
mesh.name = name
self.report({'INFO'}, 'renamed {} meshs'.format(count))
class SyncMaterialNames(bpy.types.Operator):
"""Rename all selected objects to lower case"""
bl_idname = "cleanup.sync_material_names"
bl_label = "Sync material names"
@classmethod
def poll(cls, context: bpy.types.Context):
return len(context.view_layer.objects.selected) > 0
def execute(self, context):
self.sync_material_names(context)
return {'FINISHED'}
def sync_material_names(self, context: bpy.types.Context):
count = 0
objs = context.view_layer.objects.selected
for obj in objs:
obj: bpy.types.Object
name = obj.name
mat_slots = obj.material_slots
for slot in mat_slots:
slot: bpy.types.MaterialSlot
if slot.material is not None:
if slot.material.users == 1:
if slot.material.name != name:
count += 1
slot.material.name = name
self.report({'INFO'}, 'renamed {} materials'.format(count))
class DoAllFormattings(bpy.types.Operator):
"""Do all formattings"""
bl_idname = "cleanup.do_all_formattings"
bl_label = "Do all formattings"
@classmethod
def poll(cls, context: bpy.types.Context):
return len(context.view_layer.objects.selected) > 0
def execute(self, context):
self.do_all(context)
return {'FINISHED'}
def do_all(self, context: bpy.types.Context):
bpy.ops.cleanup.lower_object_names()
bpy.ops.cleanup.lower_image_names()
bpy.ops.cleanup.sync_mesh_names()
bpy.ops.cleanup.sync_material_names()
classes = [
LowerObjectNames,
SyncMeshNames,
SyncMaterialNames,
LowerImageNames,
DoAllFormattings,
]
def register():
for c in classes:
bpy.utils.register_class(c)
def unregister():
for c in reversed(classes):
bpy.utils.unregister_class(c)
if __name__ == "__main__":
register()