Updated batch object renamer.
[blender.git] / release / scripts / object_batch_name_edit.py
1 #!BPY
2
3 """
4 Name: 'Batch Object Name Edit'
5 Blender: 240
6 Group: 'Object'
7 Tooltip: 'Apply the chosen rule to rename all selected objects at once.'
8 """
9
10 __author__ = "Campbell Barton"
11 __url__ = ("blender", "elysiun")
12 __version__ = "1.0"
13
14 __bpydoc__ = """\
15 "Batch Object Name Edit" allows you to change multiple names of Blender
16 objects at once.  It provides options to define if you want to: replace text
17 in the current names, truncate their beginnings or endings or prepend / append
18 strings to them.
19
20 Usage:
21
22 Select the objects to be renamed and run this script from the Object->Scripts
23 menu of the 3d View.
24 """
25
26
27 # $Id$
28 #
29 # --------------------------------------------------------------------------
30 # Batch Name Edit by Campbell Barton (AKA Ideasman)
31 # --------------------------------------------------------------------------
32 # ***** BEGIN GPL LICENSE BLOCK *****
33 #
34 # This program is free software; you can redistribute it and/or
35 # modify it under the terms of the GNU General Public License
36 # as published by the Free Software Foundation; either version 2
37 # of the License, or (at your option) any later version.
38 #
39 # This program is distributed in the hope that it will be useful,
40 # but WITHOUT ANY WARRANTY; without even the implied warranty of
41 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42 # GNU General Public License for more details.
43 #
44 # You should have received a copy of the GNU General Public License
45 # along with this program; if not, write to the Free Software Foundation,
46 # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
47 #
48 # ***** END GPL LICENCE BLOCK *****
49 # --------------------------------------------------------------------------
50
51 from Blender import *
52
53 global renameCount
54 renameCount = 0
55
56 def main():
57         global renameCount
58         # Rename the datablocks that are used by the object.
59         def renameLinkedDataFromObject():
60                 
61                 # Result 1, we want to rename data
62                 for ob in Object.GetSelected():
63                         if ob.name == ob.getData(1):
64                                 return # Alredy the same name, dont bother.
65                                 
66                         try:
67                                 if ob.getType() == 'Mesh': 
68                                         data = ob.getData(mesh=1) # use mesh so we dont have to update the nmesh.
69                                 else:
70                                         data = ob.getData()
71                                 data.name = ob.name
72                         except:
73                                 # Maybe trying to renasme an empty, dont worry about this.
74                                 pass
75                 
76         
77         
78         def new():
79                 global renameCount
80                 NEW_NAME_STRING = Draw.Create('')
81                 RENAME_LINKED = Draw.Create(0)
82                 pup_block = [\
83                 ('New Name: ', NEW_NAME_STRING, 19, 19, 'New Name'),\
84                 ('Rename ObData', RENAME_LINKED, 'Renames objects data to match the obname'),\
85                 ]
86                 
87                 if not Draw.PupBlock('Replace in name...', pup_block):
88                         return 0
89                 
90                 NEW_NAME_STRING= NEW_NAME_STRING.val
91                 
92                 Window.WaitCursor(1)
93                 for ob in Object.GetSelected():
94                         if ob.name != NEW_NAME_STRING:
95                                 ob.name = NEW_NAME_STRING
96                                 renameCount+=1
97                 
98                 return RENAME_LINKED.val
99                 
100         def replace():
101                 global renameCount
102                 REPLACE_STRING = Draw.Create('')
103                 WITH_STRING = Draw.Create('')
104                 RENAME_LINKED = Draw.Create(0)
105                 
106                 pup_block = [\
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'),\
110                 ]
111                 
112                 if not Draw.PupBlock('Replace in name...', pup_block) or\
113                 ((not REPLACE_STRING.val) and (not WITH_STRING)):
114                         return 0
115                 
116                 REPLACE_STRING = REPLACE_STRING.val
117                 WITH_STRING = WITH_STRING.val
118                 
119                 Window.WaitCursor(1)
120                 for ob in Object.GetSelected():
121                         newname = ob.name.replace(REPLACE_STRING, WITH_STRING)
122                         if ob.name != newname:
123                                 ob.name = newname
124                                 renameCount+=1
125                 return RENAME_LINKED.val
126                         
127         
128         def prefix():
129                 global renameCount
130                 PREFIX_STRING = Draw.Create('')
131                 RENAME_LINKED = Draw.Create(0)
132                 
133                 pup_block = [\
134                 ('Prefix: ', PREFIX_STRING, 19, 19, 'Name prefix'),\
135                 ('Rename ObData', RENAME_LINKED, 'Renames objects data to match the obname'),\
136                 ]
137                 
138                 if not Draw.PupBlock('Prefix...', pup_block) or\
139                 not PREFIX_STRING.val:
140                         return 0
141                 
142                 PREFIX_STRING = PREFIX_STRING.val
143                 
144                 Window.WaitCursor(1)
145                 for ob in Object.GetSelected():
146                         ob.name = PREFIX_STRING + ob.name
147                         renameCount+=1 # we knows these are different.
148                 return RENAME_LINKED.val
149                 
150         def suffix():
151                 global renameCount
152                 SUFFIX_STRING = Draw.Create('')
153                 RENAME_LINKED = Draw.Create(0)
154                 
155                 pup_block = [\
156                 ('Suffix: ', SUFFIX_STRING, 19, 19, 'Name suffix'),\
157                 ('Rename ObData', RENAME_LINKED, 'Renames objects data to match the obname'),\
158                 ]
159                 
160                 if not Draw.PupBlock('Suffix...', pup_block) or\
161                 not SUFFIX_STRING.val:
162                         return 0
163                 
164                 SUFFIX_STRING = SUFFIX_STRING.val
165                 
166                 Window.WaitCursor(1)
167                 for ob in Object.GetSelected():
168                         ob.name =  ob.name + SUFFIX_STRING
169                         renameCount+=1 # we knows these are different.
170                 return RENAME_LINKED.val
171         
172         def truncate_start():
173                 global renameCount
174                 TRUNCATE_START = Draw.Create(0)
175                 RENAME_LINKED = Draw.Create(0)
176                 
177                 pup_block = [\
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'),\
180                 ]
181                 
182                 if not Draw.PupBlock('Truncate Start...', pup_block) or\
183                 not TRUNCATE_START.val:
184                         return 0
185                         
186                 Window.WaitCursor(1)
187                 TRUNCATE_START = TRUNCATE_START.val
188                 for ob in Object.GetSelected():
189                         newname = ob.name[TRUNCATE_START: ]
190                         ob.name = newname
191                         renameCount+=1
192                 
193                 return RENAME_LINKED.val
194
195         
196         def truncate_end():
197                 global renameCount
198                 TRUNCATE_END = Draw.Create(0)
199                 RENAME_LINKED = Draw.Create(0)
200                 
201                 pup_block = [\
202                 ('Truncate End: ', TRUNCATE_END, 0, 19, 'Truncate chars from the end of the name'),\
203                 ('Rename ObData', RENAME_LINKED, 'Renames objects data to match the obname'),\
204                 ]
205                 
206                 if not Draw.PupBlock('Truncate End...', pup_block) or\
207                 not TRUNCATE_END.val:
208                         return 0
209                         
210                 Window.WaitCursor(1)
211                 TRUNCATE_END = TRUNCATE_END.val
212                 for ob in Object.GetSelected():
213                         newname = ob.name[: -TRUNCATE_END]
214                         ob.name = newname
215                         renameCount+=1
216                 
217                 return RENAME_LINKED.val
218
219         def renameObjectFromLinkedData():
220                 global renameCount
221                 Window.WaitCursor(1)
222                 
223                 for ob in Object.GetSelected():
224                         newname = ob.getData(1)
225                         if newname != None and ob.name != newname:
226                                 ob.name = newname
227                                 renameCount+=1
228                 return 0
229                 
230         
231         name = "Selected Object Names%t|New Name|Replace Text|Add Prefix|Add Suffix|Truncate Start|Truncate End|Rename Objects to Data Names"
232         result = Draw.PupMenu(name)
233         
234         renLinked = 0 # Rename linked data to the object name?
235         
236         if result == -1:
237                 return
238         elif result == 1: renLinked= new()
239         elif result == 2: renLinked= replace()
240         elif result == 3: renLinked= prefix()
241         elif result == 4: renLinked= suffix()
242         elif result == 5: renLinked= truncate_start()
243         elif result == 6: renLinked= truncate_end()
244         elif result == 7: renameObjectFromLinkedData()
245         
246         if renLinked:
247                 renameLinkedDataFromObject()
248         
249         Window.WaitCursor(0)
250         
251         Draw.PupMenu('renamed: %d objects.' % renameCount)
252
253 main()