0
$\begingroup$

I attempt to center the view on the currently selected object using Python. Normally, you would select the object and then press Numpad . in the viewport.

According to the keymap, the command behind it is view3d.view_selected or more accurate bpy.ops.view3d.view_selected:

keymap

The documentation says basically nothing about how to use it.

$\endgroup$
0

1 Answer 1

1
$\begingroup$

You need to be in the correct context area which is VIEW_3D. Read more about context here.

import bpy

area_type = 'VIEW_3D'
areas  = [area for area in bpy.context.window.screen.areas if area.type == area_type]

with bpy.context.temp_override(
    window=bpy.context.window,
    area=areas[0],
    region=[region for region in areas[0].regions if region.type == 'WINDOW'][0],
    screen=bpy.context.window.screen
):
    bpy.ops.view3d.view_selected()
$\endgroup$

You must log in to answer this question.

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