1
$\begingroup$

I would like to center the view3d on a selected object, just like numpad "." (dot) do. i saw we can use bpy.ops.view3d.view_axis(type='LEFT') for exemple but i can't find how to center the view on an object with python.

Does anybody have an idea ? Thanks !

$\endgroup$

2 Answers 2

3
$\begingroup$

Make sure you have Python Tooltips enabled in your Preferences:

python tooltips and developer extras options

That way, when you hover something to get its tooltip, it displays the python command it actually calls. So taking the example of Frame Selected which is what you are trying to do, we see this:

frame selected's tooltip

And thanks to Developer Extras, you can RMB RMBCopy Python Command so that it copies bpy.ops.view3d.view_selected(use_all_regions=False) in your clipboard and you can paste it into any sript you want.

$\endgroup$
1
  • $\begingroup$ thanks a lot !! $\endgroup$
    – Leandre
    Jul 7, 2022 at 15:40
1
$\begingroup$

Note that to use that Python command in a script, the current context needs to be temporarily overridden to provide a 3D view area and a window region, like this:

import bpy
from bpy import context

areas = [area for area in bpy.context.window.screen.areas if area.type == 'VIEW_3D']
if len(areas) > 0:
    context_override = context.copy()
    context_override['area'] = areas[0]
    context_override['region'] = [region for region in areas[0].regions if region.type == 'WINDOW'][0]
    with context.temp_override(**context_override):
        bpy.ops.view3d.view_selected()

See docs for more information about contexts.

$\endgroup$

You must log in to answer this question.

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