2
$\begingroup$

numpad . will frame the selected object in the viewport, which is fine, but if it's a teeny-tiny control bone it also zooms my view way in, so I have to do numpad .thenctrlscrol scroll scroll scroll scroll… to get back to a useable view every time. Is there a way of centering the viewport on a selected object (/bone/thing) without the view zooming in or out?

$\endgroup$

2 Answers 2

4
$\begingroup$

First answer:

Not exactly what you want but something close in two steps:

  • shift S 2 (cursor to selected). You can also place the 3D cursor where you want with shift RMB
  • View > Align View > Center View to Cursor (if you right clic on "Center View to Cursor" you can assign a shortcut for example shift V)

Better answer than my previous:

This script CenterViewSelected.py allows to center the 3D view on the selected element(s):

  • by Menu > View > Align View > Center to Selected
  • by Shortcut: shift Numpad . (no other action needed but you can change the shortcut by right clic in the menu).

enter image description here

#########################################################################
# Centers the view to selected item(s)                                  #
# License: GPL v3                                                       #
#########################################################################

############# Add-on description (used by Blender) ######################
bl_info = {
    "name": "Center View to Selected",
    "description": 'Centers the 3D View to Selected',
    "author": "hatterer raoul",
    "version": (0, 1),
    "blender": (3, 00, 0),
    "location": "View > Align View > Center View to Selected",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "3D View"}

#########################################################################
import bpy

class CentereViewSelected(bpy.types.Operator):
    """Center the 3D viewport on the selected item(s)"""
    bl_idname = "view3d.view_center_selected"
    bl_label = "Center View to Selected"
   
    def execute(self, context):
        # store the current cursor location
        cursor_location = context.scene.cursor.location.copy()
        # center the cursor on the active item
        bpy.ops.view3d.snap_cursor_to_selected()
        # center the view on the cursor
        bpy.ops.view3d.view_center_cursor()
        # reset the cursor location
        context.scene.cursor.location = cursor_location
        return {'FINISHED'}


def menu_func(self,context):
    self.layout.operator(CentereViewSelected.bl_idname)

addon_keymaps = []

def register():
    bpy.utils.register_class(CentereViewSelected)
    bpy.types.VIEW3D_MT_view_align.prepend(menu_func)

    # Add the hotkey
    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(CentereViewSelected.bl_idname, type='NUMPAD_PERIOD', value='PRESS', shift=True)
        addon_keymaps.append((km, kmi))

def unregister():
    bpy.utils.unregister_class(CentereViewSelected)
    bpy.types.VIEW3D_MT_view_align.remove(menu_func)

    # Remove the hotkey
    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)
    addon_keymaps.clear()    
    
if __name__ == "__main__":
    register()

$\endgroup$
5
  • $\begingroup$ nice workaround! $\endgroup$
    – Chris
    Commented Feb 11, 2022 at 11:59
  • $\begingroup$ Nice. I'm still learning python and the Blender API, thanks for the help. $\endgroup$
    – stib
    Commented Feb 12, 2022 at 23:38
  • $\begingroup$ As a side question, do you know a reference for how to choose a valid bl_idname? $\endgroup$
    – stib
    Commented Feb 12, 2022 at 23:43
  • $\begingroup$ One issue with this approach is that it still moves the point of view of the viewport. Is it possible to only rotate the point of view to look at the selection? $\endgroup$
    – stib
    Commented Feb 12, 2022 at 23:49
  • $\begingroup$ There is no rotation neither with Frame Selected (Numpad .) $\endgroup$ Commented Feb 13, 2022 at 2:19
1
$\begingroup$

This is one of those really basic functions that Blender seems to inexplicably lack.

So I wrote a script that does what Raoul Hatterer suggested. The script is here (free / open source). Edit: use Raoul's script, it will do the hotkey thing without jumping through any hoops. If you install it you can add it to a hotkey by typing in 'view.center_no_zoom' in the hotkey menu (I can't get it to appear in the right-click add hotkey menu, because I don't know how bl_idname works).

I've added it to Numpad . which overrides the default behaviour (which is also mapped to ctrlNumpad . so I can still use the default behaviour too.

enter image description here

$\endgroup$
1
  • $\begingroup$ Well done. I also wrote a slightly improved version $\endgroup$ Commented Feb 12, 2022 at 23:04

You must log in to answer this question.

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