-
Notifications
You must be signed in to change notification settings - Fork 40
/
object_batch_rename_datablocks.py
217 lines (178 loc) · 6.44 KB
/
object_batch_rename_datablocks.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
bl_info = {
"name": "Batch Rename Datablocks",
"author": "tstscr",
"version": (1, 1),
"blender": (2, 80, 0),
"location": "Search > (rename)",
"description": "Batch renaming of datablocks "
"(e.g. rename materials after objectnames)",
"warning": "",
"wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
"Scripts/Object/Batch_Rename_Datablocks",
"tracker_url": "https://developer.blender.org/maniphest/task/edit/form/2/",
"category": "Object"}
import bpy
from bpy.props import (
EnumProperty,
StringProperty,
BoolProperty,
)
def get_first_material_name(ob):
for m_slot in ob.material_slots:
if m_slot.material:
material_name = m_slot.material.name
return material_name
return None
def get_name(self, ob):
if self.naming_base == 'Object':
return ob.name
if self.naming_base == 'Mesh':
if ob.data:
return ob.data.name
else:
return ob.name
if self.naming_base == 'Material':
material_name = get_first_material_name(ob)
if not material_name:
return ob.name
else:
return material_name
if self.naming_base == 'Custom':
return self.rename_custom
def rename_datablocks_main(self, context):
obs = context.selected_editable_objects
for ob in obs:
name = get_name(self, ob)
if self.rename_object:
if (self.rename_use_prefix and self.prefix_object):
ob.name = self.rename_prefix + name
else:
ob.name = name
if self.rename_data:
if (ob.data
and ob.data.users == 1):
if (self.rename_use_prefix and self.prefix_data):
ob.data.name = self.rename_prefix + name
else:
ob.data.name = name
if self.rename_material:
if ob.material_slots:
for m_slot in ob.material_slots:
if m_slot.material:
if m_slot.material.users == 1:
if (self.rename_use_prefix and self.prefix_material):
m_slot.material.name = self.rename_prefix + name
else:
m_slot.material.name = name
class OBJECT_OT_batch_rename_datablocks(bpy.types.Operator):
"""Batch rename Datablocks"""
bl_idname = "object.batch_rename_datablocks"
bl_label = "Batch Rename Datablocks"
bl_options = {'REGISTER', 'UNDO'}
name_origins = [
('Object', 'Object', 'Use object name to rename other datablocks'),
('Mesh', 'Mesh', 'Use mesh name to rename other datablocks'),
('Material', 'Material', 'Use material name to rename other datablocks'),
('Custom', 'Custom', 'Use new custom name to rename other datablocks')
]
naming_base: EnumProperty(
name='Name after:',
items=name_origins)
rename_custom: StringProperty(
name='Custom Name',
default='New Name',
description='Custom new name')
rename_object: BoolProperty(
name='Rename Objects',
default=False,
description='Rename Objects')
rename_data: BoolProperty(
name='Rename Data',
default=True,
description='Rename Object\'s Data')
rename_material: BoolProperty(
name='Rename Materials',
default=True,
description='Rename Objects\' Materials')
rename_use_prefix: BoolProperty(
name='Add Prefix',
default=False,
description='Prefix Objectnames with first Groups name')
rename_prefix: StringProperty(
name='Prefix',
default='',
description='Prefix name with this string')
prefix_object: BoolProperty(
name='Object',
default=True,
description='Prefix Object Names')
prefix_data: BoolProperty(
name='Data',
default=True,
description='Prefix Data Names')
prefix_material: BoolProperty(
name='Material',
default=True,
description='Prefix Material Names')
dialog_width = 260
def draw(self, context):
layout = self.layout
col = layout.column()
col.label(text='Rename after:')
row = layout.row()
row.prop(self.properties, 'naming_base', expand=True)
col = layout.column()
col.prop(self.properties, 'rename_custom')
col.separator()
col.label(text='Datablocks to rename:')
col.prop(self.properties, 'rename_object')
col.prop(self.properties, 'rename_data')
col.prop(self.properties, 'rename_material')
col.separator()
col.prop(self.properties, 'rename_use_prefix')
col.prop(self.properties, 'rename_prefix')
row = layout.row()
row.prop(self.properties, 'prefix_object')
row.prop(self.properties, 'prefix_data')
row.prop(self.properties, 'prefix_material')
col = layout.column()
@classmethod
def poll(cls, context):
return context.selected_objects != None
def execute(self, context):
rename_datablocks_main(self, context)
return {'FINISHED'}
def invoke(self, context, event):
wm = context.window_manager
wm.invoke_props_dialog(self, width=self.dialog_width)
return {'RUNNING_MODAL'}
classes = [
OBJECT_OT_batch_rename_datablocks,
]
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
if __name__ == '__main__':
register()