Fix for invalid break statement outside loop.
[blender-addons-contrib.git] / object_batch_rename_datablocks.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 bl_info = {
20     "name": "Batch Rename Datablocks",
21     "author": "tstscr",
22     "version": (1, 0),
23     "blender": (2, 59, 0),
24     "location": "Search > (rename)",
25     "description": "Batch renaming of datablocks "
26         "(e.g. rename materials after objectnames)",
27     "warning": "",
28     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
29         "Scripts/Object/Batch_Rename_Datablocks",
30     "tracker_url": "https://developer.blender.org/T25242",
31     "category": "Object"}
32
33
34 import bpy
35 from bpy.props import *
36
37 def get_first_material_name(ob):
38     for m_slot in ob.material_slots:
39         if m_slot.material:
40             material_name = m_slot.material.name
41             return material_name
42
43 def get_name(self, ob):
44     if self.naming_base == 'Object':
45         return ob.name
46
47     if self.naming_base == 'Mesh':
48         if ob.data: return ob.data.name
49         else: return ob.name
50
51     if self.naming_base == 'Material':
52         material_name = get_first_material_name(ob)
53         if not material_name: return ob.name
54         else: return material_name
55
56     if self.naming_base == 'Custom':
57         return self.rename_custom
58
59
60 def rename_datablocks_main(self, context):
61     obs = context.selected_editable_objects
62     for ob in obs:
63         name = get_name(self, ob)
64
65         if self.rename_object:
66             if (self.rename_use_prefix
67             and self.prefix_object):
68                 ob.name = self.rename_prefix + name
69             else:
70                 ob.name = name
71
72         if self.rename_data:
73             if (ob.data
74             and ob.data.users == 1):
75                 if (self.rename_use_prefix
76                 and self.prefix_data):
77                     ob.data.name = self.rename_prefix + name
78                 else:
79                     ob.data.name = name
80
81         if self.rename_material:
82             if ob.material_slots:
83                 for m_slot in ob.material_slots:
84                     if m_slot.material:
85                         if m_slot.material.users == 1:
86                             if (self.rename_use_prefix
87                             and self.prefix_material):
88                                 m_slot.material.name = self.rename_prefix + name
89                             else:
90                                 m_slot.material.name = name
91
92 class OBJECT_OT_batch_rename_datablocks(bpy.types.Operator):
93     """Batch rename Datablocks"""
94     bl_idname = "object.batch_rename_datablocks"
95     bl_label = "Batch Rename Datablocks"
96     bl_options = {'REGISTER', 'UNDO'}
97
98     name_origins = [
99                     ('Object', 'Object', 'Object'),
100                     ('Mesh', 'Mesh', 'Mesh'),
101                     ('Material', 'Material', 'Material'),
102                     ('Custom', 'Custom', 'Custom')
103                     ]
104     naming_base = EnumProperty(name='Name after:',
105                                 items=name_origins)
106     rename_custom = StringProperty(name='Custom Name',
107                                 default='New Name',
108                                 description='Rename all with this String')
109     rename_object = BoolProperty(name='Rename Objects',
110                                 default=False,
111                                 description='Rename Objects')
112     rename_data = BoolProperty(name='Rename Data',
113                                 default=True,
114                                 description='Rename Object\'s Data')
115     rename_material = BoolProperty(name='Rename Materials',
116                                 default=True,
117                                 description='Rename Objects\' Materials')
118     rename_use_prefix = BoolProperty(name='Add Prefix',
119                                 default=False,
120                                 description='Prefix Objectnames with first Groups name')
121     rename_prefix = StringProperty(name='Prefix',
122                                 default='',
123                                 description='Prefix name with this string')
124     prefix_object = BoolProperty(name='Object',
125                                 default=True,
126                                 description='Prefix Object Names')
127     prefix_data = BoolProperty(name='Data',
128                                 default=True,
129                                 description='Prefix Data Names')
130     prefix_material = BoolProperty(name='Material',
131                                 default=True,
132                                 description='Prefix Material Names')
133
134     dialog_width = 260
135
136     def draw(self, context):
137         layout = self.layout
138         col = layout.column()
139         col.label(text='Rename after:')
140
141         row = layout.row()
142         row.prop(self.properties, 'naming_base', expand=True)
143
144         col = layout.column()
145         col.prop(self.properties, 'rename_custom')
146
147         col.separator()
148         col.label('Datablocks to rename:')
149         col.prop(self.properties, 'rename_object')
150         col.prop(self.properties, 'rename_data')
151         col.prop(self.properties, 'rename_material')
152
153         col.separator()
154         col.prop(self.properties, 'rename_use_prefix')
155         col.prop(self.properties, 'rename_prefix')
156
157         row = layout.row()
158         row.prop(self.properties, 'prefix_object')
159         row.prop(self.properties, 'prefix_data')
160         row.prop(self.properties, 'prefix_material')
161
162         col = layout.column()
163
164     @classmethod
165     def poll(cls, context):
166         return context.selected_objects != None
167
168     def execute(self, context):
169
170         rename_datablocks_main(self, context)
171
172         return {'FINISHED'}
173
174     def invoke(self, context, event):
175         wm = context.window_manager
176         wm.invoke_props_dialog(self, self.dialog_width)
177         return {'RUNNING_MODAL'}
178
179
180 def register():
181     bpy.utils.register_module(__name__)
182     pass
183 def unregister():
184     bpy.utils.unregister_module(__name__)
185     pass
186 if __name__ == '__main__':
187     register()