~diresu/blender/blender-command-port

« back to all changes in this revision

Viewing changes to release/scripts/object_batch_name_edit.py

  • Committer: theeth
  • Date: 2008-10-14 16:52:04 UTC
  • Revision ID: vcs-imports@canonical.com-20081014165204-r32w2gm6s0osvdhn
copy back trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!BPY
 
2
"""
 
3
Name: 'Batch Object Name Edit'
 
4
Blender: 240
 
5
Group: 'Object'
 
6
Tooltip: 'Apply the chosen rule to rename all selected objects at once.'
 
7
"""
 
8
__author__ = "Campbell Barton"
 
9
__url__ = ("blender", "blenderartists.org")
 
10
__version__ = "1.0"
 
11
 
 
12
__bpydoc__ = """\
 
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
 
16
strings to them.
 
17
 
 
18
Usage:
 
19
Select the objects to be renamed and run this script from the Object->Scripts
 
20
menu of the 3d View.
 
21
"""
 
22
# $Id: object_batch_name_edit.py 14530 2008-04-23 14:04:05Z campbellbarton $
 
23
#
 
24
# --------------------------------------------------------------------------
 
25
# Batch Name Edit by Campbell Barton (AKA Ideasman)
 
26
# --------------------------------------------------------------------------
 
27
# ***** BEGIN GPL LICENSE BLOCK *****
 
28
#
 
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.
 
33
#
 
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.
 
38
#
 
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.
 
42
#
 
43
# ***** END GPL LICENCE BLOCK *****
 
44
# --------------------------------------------------------------------------
 
45
from Blender import *
 
46
import bpy
 
47
 
 
48
global renameCount
 
49
renameCount = 0
 
50
obsel = [ob for ob in bpy.data.scenes.active.objects.context if not ob.lib]
 
51
 
 
52
def setDataNameWrapper(ob, newname):
 
53
        if ob.getData(name_only=1) == newname:
 
54
                return False
 
55
        
 
56
        data= ob.getData(mesh=1)
 
57
        
 
58
        if data and not data.lib:
 
59
                data.name= newname
 
60
                return True
 
61
        return False
 
62
 
 
63
def main():
 
64
        global renameCount
 
65
        # Rename the datablocks that are used by the object.
 
66
        def renameLinkedDataFromObject():
 
67
                
 
68
                # Result 1, we want to rename data
 
69
                for ob in obsel:
 
70
                        if ob.name == ob.getData(name_only=1):
 
71
                                return # Alredy the same name, dont bother.
 
72
                        
 
73
                        data = ob.getData(mesh=1) # use mesh so we dont have to update the nmesh.
 
74
                        if data and not data.lib:
 
75
                                data.name = ob.name
 
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 obsel:
 
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 obsel:
 
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 obsel:
 
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 obsel:
 
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 obsel:
 
189
                        newname = ob.name[TRUNCATE_START: ]
 
190
                        ob.name = newname
 
191
                        renameCount+=1
 
192
                
 
193
                return RENAME_LINKED.val
 
194
 
 
195
        def truncate_end():
 
196
                global renameCount
 
197
                TRUNCATE_END = Draw.Create(0)
 
198
                RENAME_LINKED = Draw.Create(0)
 
199
                
 
200
                pup_block = [\
 
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'),\
 
203
                ]
 
204
                
 
205
                if not Draw.PupBlock('Truncate End...', pup_block) or\
 
206
                not TRUNCATE_END.val:
 
207
                        return 0
 
208
                        
 
209
                Window.WaitCursor(1)
 
210
                TRUNCATE_END = TRUNCATE_END.val
 
211
                for ob in obsel:
 
212
                        newname = ob.name[: -TRUNCATE_END]
 
213
                        ob.name = newname
 
214
                        renameCount+=1
 
215
                
 
216
                return RENAME_LINKED.val
 
217
 
 
218
        def renameObjectFromLinkedData():
 
219
                global renameCount
 
220
                Window.WaitCursor(1)
 
221
                
 
222
                for ob in obsel:
 
223
                        newname = ob.getData(name_only=1)
 
224
                        if newname != None and ob.name != newname:
 
225
                                ob.name = newname
 
226
                                renameCount+=1
 
227
                return 0
 
228
        
 
229
        def renameObjectFromDupGroup():
 
230
                global renameCount
 
231
                Window.WaitCursor(1)
 
232
                
 
233
                for ob in obsel:
 
234
                        group= ob.DupGroup
 
235
                        if group != None:
 
236
                                newname= group.name
 
237
                                if newname != ob.name:
 
238
                                        ob.name = newname
 
239
                                        renameCount+=1
 
240
                return 0
 
241
                
 
242
        def renameLinkedDataFromObject():
 
243
                global renameCount
 
244
                Window.WaitCursor(1)
 
245
                
 
246
                for ob in obsel:
 
247
                        if setDataNameWrapper(ob, ob.name):
 
248
                                renameCount+=1
 
249
                return 0
 
250
        
 
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?
 
254
        if result == -1:
 
255
                return
 
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()
 
265
        
 
266
        if renLinked:
 
267
                renameLinkedDataFromObject()
 
268
        
 
269
        Window.WaitCursor(0)
 
270
        
 
271
        Draw.PupMenu('renamed: %d objects.' % renameCount)
 
272
 
 
273
if __name__=='__main__':
 
274
        main()