Skip to content
Snippets Groups Projects
object_batch_rename_datablocks.py 6.44 KiB
Newer Older
  • Learn to ignore specific revisions
  • Florian Meyer's avatar
    Florian Meyer committed
    # ##### 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 = {
    
    Florian Meyer's avatar
    Florian Meyer committed
        "name": "Batch Rename Datablocks",
        "author": "tstscr",
    
    florianfelix's avatar
    florianfelix committed
        "version": (1, 1),
        "blender": (2, 80, 0),
    
        "location": "Search > (rename)",
    
        "description": "Batch renaming of datablocks "
    
    florianfelix's avatar
    florianfelix committed
        "(e.g. rename materials after objectnames)",
    
    Florian Meyer's avatar
    Florian Meyer committed
        "warning": "",
    
        "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
    
    florianfelix's avatar
    florianfelix committed
        "Scripts/Object/Batch_Rename_Datablocks",
    
        "tracker_url": "https://developer.blender.org/maniphest/task/edit/form/2/",
    
    Florian Meyer's avatar
    Florian Meyer committed
        "category": "Object"}
    
    Florian Meyer's avatar
    Florian Meyer committed
    
    
    Florian Meyer's avatar
    Florian Meyer committed
    import bpy
    
    florianfelix's avatar
    florianfelix committed
    from bpy.props import (
        EnumProperty,
        StringProperty,
        BoolProperty,
    )
    
    
    Florian Meyer's avatar
    Florian Meyer committed
    
    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
    
    florianfelix's avatar
    florianfelix committed
        return None
    
    
    Florian Meyer's avatar
    Florian Meyer committed
    
    def get_name(self, ob):
        if self.naming_base == 'Object':
            return ob.name
    
    Florian Meyer's avatar
    Florian Meyer committed
        if self.naming_base == 'Mesh':
    
    florianfelix's avatar
    florianfelix committed
            if ob.data:
                return ob.data.name
            else:
                return ob.name
    
    Florian Meyer's avatar
    Florian Meyer committed
        if self.naming_base == 'Material':
            material_name = get_first_material_name(ob)
    
    florianfelix's avatar
    florianfelix committed
            if not material_name:
                return ob.name
            else:
                return material_name
    
    Florian Meyer's avatar
    Florian Meyer committed
    
        if self.naming_base == 'Custom':
            return self.rename_custom
    
    Florian Meyer's avatar
    Florian Meyer committed
    def rename_datablocks_main(self, context):
        obs = context.selected_editable_objects
    
    Florian Meyer's avatar
    Florian Meyer committed
        for ob in obs:
            name = get_name(self, ob)
    
    Florian Meyer's avatar
    Florian Meyer committed
            if self.rename_object:
    
    florianfelix's avatar
    florianfelix committed
                if (self.rename_use_prefix and self.prefix_object):
    
    Florian Meyer's avatar
    Florian Meyer committed
                    ob.name = self.rename_prefix + name
                else:
                    ob.name = name
    
    Florian Meyer's avatar
    Florian Meyer committed
            if self.rename_data:
                if (ob.data
    
    florianfelix's avatar
    florianfelix committed
                        and ob.data.users == 1):
                    if (self.rename_use_prefix and self.prefix_data):
    
    Florian Meyer's avatar
    Florian Meyer committed
                        ob.data.name = self.rename_prefix + name
                    else:
                        ob.data.name = name
    
    Florian Meyer's avatar
    Florian Meyer committed
            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:
    
    florianfelix's avatar
    florianfelix committed
                                if (self.rename_use_prefix and self.prefix_material):
    
    Florian Meyer's avatar
    Florian Meyer committed
                                    m_slot.material.name = self.rename_prefix + name
                                else:
                                    m_slot.material.name = name
    
    
    florianfelix's avatar
    florianfelix committed
    
    
    Florian Meyer's avatar
    Florian Meyer committed
    class OBJECT_OT_batch_rename_datablocks(bpy.types.Operator):
    
        """Batch rename Datablocks"""
    
    Florian Meyer's avatar
    Florian Meyer committed
        bl_idname = "object.batch_rename_datablocks"
        bl_label = "Batch Rename Datablocks"
        bl_options = {'REGISTER', 'UNDO'}
    
    Florian Meyer's avatar
    Florian Meyer committed
        name_origins = [
    
    florianfelix's avatar
    florianfelix committed
            ('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')
    
    Florian Meyer's avatar
    Florian Meyer committed
    
    
    Florian Meyer's avatar
    Florian Meyer committed
        dialog_width = 260
    
    Florian Meyer's avatar
    Florian Meyer committed
    
        def draw(self, context):
            layout = self.layout
            col = layout.column()
            col.label(text='Rename after:')
    
    Florian Meyer's avatar
    Florian Meyer committed
            row = layout.row()
            row.prop(self.properties, 'naming_base', expand=True)
    
    Florian Meyer's avatar
    Florian Meyer committed
            col = layout.column()
            col.prop(self.properties, 'rename_custom')
    
            col.separator()
    
    florianfelix's avatar
    florianfelix committed
            col.label(text='Datablocks to rename:')
    
    Florian Meyer's avatar
    Florian Meyer committed
            col.prop(self.properties, 'rename_object')
            col.prop(self.properties, 'rename_data')
            col.prop(self.properties, 'rename_material')
    
    Florian Meyer's avatar
    Florian Meyer committed
            col.separator()
            col.prop(self.properties, 'rename_use_prefix')
            col.prop(self.properties, 'rename_prefix')
    
    Florian Meyer's avatar
    Florian Meyer committed
            row = layout.row()
            row.prop(self.properties, 'prefix_object')
            row.prop(self.properties, 'prefix_data')
            row.prop(self.properties, 'prefix_material')
    
    Florian Meyer's avatar
    Florian Meyer committed
            col = layout.column()
    
    Florian Meyer's avatar
    Florian Meyer committed
        @classmethod
        def poll(cls, context):
            return context.selected_objects != None
    
        def execute(self, context):
    
    
    Florian Meyer's avatar
    Florian Meyer committed
            rename_datablocks_main(self, context)
    
    Florian Meyer's avatar
    Florian Meyer committed
            return {'FINISHED'}
    
        def invoke(self, context, event):
            wm = context.window_manager
    
    florianfelix's avatar
    florianfelix committed
            wm.invoke_props_dialog(self, width=self.dialog_width)
    
    Florian Meyer's avatar
    Florian Meyer committed
            return {'RUNNING_MODAL'}
    
    florianfelix's avatar
    florianfelix committed
    classes = [
        OBJECT_OT_batch_rename_datablocks,
    ]
    
    
    
    Florian Meyer's avatar
    Florian Meyer committed
    def register():
    
    florianfelix's avatar
    florianfelix committed
        from bpy.utils import register_class
        for cls in classes:
            register_class(cls)
    
    
    
    Florian Meyer's avatar
    Florian Meyer committed
    def unregister():
    
    florianfelix's avatar
    florianfelix committed
        from bpy.utils import unregister_class
        for cls in reversed(classes):
            unregister_class(cls)
    
    
    
    Florian Meyer's avatar
    Florian Meyer committed
    if __name__ == '__main__':
    
    Florian Meyer's avatar
    Florian Meyer committed
        register()