10
$\begingroup$

I want to use a keyboard shortcut to bring up a text field for renaming the active object.

As far as I know, currently no such shortcut exists, and making one will require an addon. I seem to remember reading about just such an addon before, but now I can't remember where (it was a while ago). If I remember correctly, it had some other features too (batch renaming or something).

Anyway, if this functionality or an addon supplying it already exists, where can I find it?

$\endgroup$
1
  • 1
    $\begingroup$ Select the object, and hit F2 on your keyboard I think? $\endgroup$
    – Yohello 1
    Commented Apr 24, 2021 at 17:17

4 Answers 4

3
$\begingroup$

You don't need an addon for renaming objects. You can now use Blender's built in renaming tool by pressing F2 on the selected object. You can also push ⎈ CtrlF2 to bring up the batch rename which could be very handy.

$\endgroup$
18
$\begingroup$

I just found one ;)

With the following Add-on enabled, you can press CtrlR to enter a 'new name' for the active object as well as rename its data block (optional). In order to speed up the process you can hit Return followed by O to confirm, after typing the 'new name':

enter image description here Click to enlarge

viewport-rename.py

# ##### 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 #####
# <pep8 compliant>

import bpy
import re

bl_info = {
    "name": "Viewport Rename",
    "author": "p2or",
    "version": (0, 5),
    "blender" : (2, 80, 0),
    "location": "3D View > Ctrl+R",
    "warning": "", # used for warning icon and text in addons panel
    "wiki_url": "",
    "tracker_url": "",
    "category": "3D View"
}


class ViewportRenameOperator(bpy.types.Operator):
    """Rename selected object(s) in 3D View"""
    bl_idname = "view3d.viewport_rename"
    bl_label = "Viewport Rename"
    bl_options = {'REGISTER', 'UNDO'}
    bl_property = "new_name"

    new_name : bpy.props.StringProperty(name="New Name")
    data_flag : bpy.props.BoolProperty(name="Rename Data-Block", default=False)

    @classmethod
    def poll(cls, context):
        return bool(context.selected_objects)

    def execute(self, context):
        user_input = self.new_name
        reverse = False
        if user_input.endswith("#r"):
            reverse = True
            user_input = user_input[:-1]

        suff = re.findall("#+$", user_input)
        if user_input and suff:
            number = ('%0'+str(len(suff[0]))+'d', len(suff[0]))
            real_name = re.sub("#", '', user_input)           

            objs = context.selected_objects[::-1] if reverse else context.selected_objects
            names_before = [n.name for n in objs]
            for c, o in enumerate(objs, start=1):
                o.name = (real_name + (number[0] % c))
                if self.data_flag and o.data is not None:
                    o.data.name = (real_name + (number[0] % c))
            self.report({'INFO'}, "Renamed {}".format(", ".join(names_before)))
            return {'FINISHED'}

        elif user_input:
            old_name = context.active_object.name
            context.active_object.name = user_input
            if self.data_flag and context.active_object.data is not None:
                context.active_object.data.name = user_input
            self.report({'INFO'}, "{} renamed to {}".format(old_name, user_input))
            return {'FINISHED'}

        else:
            self.report({'INFO'}, "No input, operation cancelled")
            return {'CANCELLED'}

    def invoke(self, context, event):
        wm = context.window_manager
        dpi = context.preferences.system.pixel_size
        ui_size = context.preferences.system.ui_scale
        dialog_size = 450 * dpi * ui_size
        if context.active_object:
            self.new_name = context.active_object.name
        return wm.invoke_props_dialog(self, width=dialog_size)

    def draw(self, context):
        row = self.layout
        row.row()
        row.prop(self, "new_name")
        row.prop(self, "data_flag")
        row.row()


# ------------------------------------------------------------------------
#    register, unregister and hotkey
# ------------------------------------------------------------------------

addon_keymaps = []

def register():
    from bpy.utils import register_class

    addon_keymaps.clear()
    register_class(ViewportRenameOperator)

    # handle the keymap
    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    if kc:
        km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D')
        kmi = km.keymap_items.new(ViewportRenameOperator.bl_idname, type='R', value='PRESS', ctrl=True)
        addon_keymaps.append((km, kmi))

def unregister():
    from bpy.utils import unregister_class

    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)
    addon_keymaps.clear()

    unregister_class(ViewportRenameOperator)

if __name__ == "__main__":
    register()

The Add-on also allows to batch rename all objects in the current selection. The suffix is determined by the given number of hash characters at the end of the string. You can simply type Cube-### to get 'Cube-001', 'Cube-002' and 'Cube-003' as well as Cube-###r to get a reversed list:

enter image description here

Repository: https://github.com/p2or/blender-viewport-rename

$\endgroup$
2
  • 1
    $\begingroup$ Brilliant, thanks! Great find ;) I wonder if there's a way to get the text field to have focus by default..? $\endgroup$
    – gandalf3
    Commented Aug 14, 2016 at 20:54
  • 2
    $\begingroup$ Nice add-on, and a great way to demonstrate the use of draw, invoke, execute, etc. in a simple operator! $\endgroup$
    – Jake Dube
    Commented Jan 22, 2017 at 21:08
2
$\begingroup$

I made a quick rename addon myself for exactly this purpose. It doesn't have the capacity of batch renaming like "viewport-rename" do. But it propose renaming options according to current name (if available).

quick rename pop-up with proposition

Trigger it with alt-N, like ALTernative Name (ctrl-r is really good but already taken for bone roll in armature edit mode)

$\endgroup$
1
$\begingroup$

This may end up being a bit of an overkill solution to achieve it, but you could use the Name Panel Addon (available in Free and Commercial flavors) to rename your active object.

It's a great addon and I strongly recommend it's use, though it may over complicate in this case, because the addon is a lot more complex and is mainly concerned with batch naming and name copying, among other involved functionality.

It does not assign one hot key by default as far as I know, but you can go to the user preferences and manually assign it yourself.

Just browse to the File > User Preferences > Input > 3D View >3D View Global and add a new operator wm.batch_name with the desired key combination.

It should call out the operator popup with the (potentially overly complex) popup dialog

Edit

Not sure if this is what you want, but to set the name automatically to a specific predefined name you can add another operator wm.context_set_enum with Context Attributes set to `scene.BatchName.customName and Value set to whatever name you want, but I could not do it all (set the name and actual renaming) at once with a single key press. Perhaps with some additional scripting?

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .