3
Name: 'Batch Object Name Edit'
6
Tooltip: 'Apply the chosen rule to rename all selected objects at once.'
8
__author__ = "Campbell Barton"
9
__url__ = ("blender", "blenderartists.org")
13
"Batch Object Name Edit" allows you to change multiple names of Blender
14
objects at once. It provides options to define if you want to: replace text
15
in the current names, truncate their beginnings or endings or prepend / append
19
Select the objects to be renamed and run this script from the Object->Scripts
22
# $Id: object_batch_name_edit.py 14530 2008-04-23 14:04:05Z campbellbarton $
24
# --------------------------------------------------------------------------
25
# Batch Name Edit by Campbell Barton (AKA Ideasman)
26
# --------------------------------------------------------------------------
27
# ***** BEGIN GPL LICENSE BLOCK *****
29
# This program is free software; you can redistribute it and/or
30
# modify it under the terms of the GNU General Public License
31
# as published by the Free Software Foundation; either version 2
32
# of the License, or (at your option) any later version.
34
# This program is distributed in the hope that it will be useful,
35
# but WITHOUT ANY WARRANTY; without even the implied warranty of
36
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37
# GNU General Public License for more details.
39
# You should have received a copy of the GNU General Public License
40
# along with this program; if not, write to the Free Software Foundation,
41
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
43
# ***** END GPL LICENCE BLOCK *****
44
# --------------------------------------------------------------------------
50
obsel = [ob for ob in bpy.data.scenes.active.objects.context if not ob.lib]
52
def setDataNameWrapper(ob, newname):
53
if ob.getData(name_only=1) == newname:
56
data= ob.getData(mesh=1)
58
if data and not data.lib:
65
# Rename the datablocks that are used by the object.
66
def renameLinkedDataFromObject():
68
# Result 1, we want to rename data
70
if ob.name == ob.getData(name_only=1):
71
return # Alredy the same name, dont bother.
73
data = ob.getData(mesh=1) # use mesh so we dont have to update the nmesh.
74
if data and not data.lib:
80
NEW_NAME_STRING = Draw.Create('')
81
RENAME_LINKED = Draw.Create(0)
83
('New Name: ', NEW_NAME_STRING, 19, 19, 'New Name'),\
84
('Rename ObData', RENAME_LINKED, 'Renames objects data to match the obname'),\
87
if not Draw.PupBlock('Replace in name...', pup_block):
90
NEW_NAME_STRING= NEW_NAME_STRING.val
94
if ob.name != NEW_NAME_STRING:
95
ob.name = NEW_NAME_STRING
98
return RENAME_LINKED.val
102
REPLACE_STRING = Draw.Create('')
103
WITH_STRING = Draw.Create('')
104
RENAME_LINKED = Draw.Create(0)
107
('Replace: ', REPLACE_STRING, 19, 19, 'Text to find'),\
108
('With:', WITH_STRING, 19, 19, 'Text to replace with'),\
109
('Rename ObData', RENAME_LINKED, 'Renames objects data to match the obname'),\
112
if not Draw.PupBlock('Replace in name...', pup_block) or\
113
((not REPLACE_STRING.val) and (not WITH_STRING)):
116
REPLACE_STRING = REPLACE_STRING.val
117
WITH_STRING = WITH_STRING.val
121
newname = ob.name.replace(REPLACE_STRING, WITH_STRING)
122
if ob.name != newname:
125
return RENAME_LINKED.val
130
PREFIX_STRING = Draw.Create('')
131
RENAME_LINKED = Draw.Create(0)
134
('Prefix: ', PREFIX_STRING, 19, 19, 'Name prefix'),\
135
('Rename ObData', RENAME_LINKED, 'Renames objects data to match the obname'),\
138
if not Draw.PupBlock('Prefix...', pup_block) or\
139
not PREFIX_STRING.val:
142
PREFIX_STRING = PREFIX_STRING.val
146
ob.name = PREFIX_STRING + ob.name
147
renameCount+=1 # we knows these are different.
148
return RENAME_LINKED.val
152
SUFFIX_STRING = Draw.Create('')
153
RENAME_LINKED = Draw.Create(0)
156
('Suffix: ', SUFFIX_STRING, 19, 19, 'Name suffix'),\
157
('Rename ObData', RENAME_LINKED, 'Renames objects data to match the obname'),\
160
if not Draw.PupBlock('Suffix...', pup_block) or\
161
not SUFFIX_STRING.val:
164
SUFFIX_STRING = SUFFIX_STRING.val
168
ob.name = ob.name + SUFFIX_STRING
169
renameCount+=1 # we knows these are different.
170
return RENAME_LINKED.val
172
def truncate_start():
174
TRUNCATE_START = Draw.Create(0)
175
RENAME_LINKED = Draw.Create(0)
178
('Truncate Start: ', TRUNCATE_START, 0, 19, 'Truncate chars from the start of the name'),\
179
('Rename ObData', RENAME_LINKED, 'Renames objects data to match the obname'),\
182
if not Draw.PupBlock('Truncate Start...', pup_block) or\
183
not TRUNCATE_START.val:
187
TRUNCATE_START = TRUNCATE_START.val
189
newname = ob.name[TRUNCATE_START: ]
193
return RENAME_LINKED.val
197
TRUNCATE_END = Draw.Create(0)
198
RENAME_LINKED = Draw.Create(0)
201
('Truncate End: ', TRUNCATE_END, 0, 19, 'Truncate chars from the end of the name'),\
202
('Rename ObData', RENAME_LINKED, 'Renames objects data to match the obname'),\
205
if not Draw.PupBlock('Truncate End...', pup_block) or\
206
not TRUNCATE_END.val:
210
TRUNCATE_END = TRUNCATE_END.val
212
newname = ob.name[: -TRUNCATE_END]
216
return RENAME_LINKED.val
218
def renameObjectFromLinkedData():
223
newname = ob.getData(name_only=1)
224
if newname != None and ob.name != newname:
229
def renameObjectFromDupGroup():
237
if newname != ob.name:
242
def renameLinkedDataFromObject():
247
if setDataNameWrapper(ob, ob.name):
251
name = "Selected Object Names%t|New Name|Replace Text|Add Prefix|Add Suffix|Truncate Start|Truncate End|Rename Objects to Data Names|Rename Objects to DupGroup Names|Rename Data to Object Names"
252
result = Draw.PupMenu(name)
253
renLinked = 0 # Rename linked data to the object name?
256
elif result == 1: renLinked= new()
257
elif result == 2: renLinked= replace()
258
elif result == 3: renLinked= prefix()
259
elif result == 4: renLinked= suffix()
260
elif result == 5: renLinked= truncate_start()
261
elif result == 6: renLinked= truncate_end()
262
elif result == 7: renameObjectFromLinkedData()
263
elif result == 8: renameObjectFromDupGroup()
264
elif result == 9: renameLinkedDataFromObject()
267
renameLinkedDataFromObject()
271
Draw.PupMenu('renamed: %d objects.' % renameCount)
273
if __name__=='__main__':