Using camera_to_view_selected() operator with context.temp_override()

Hi,
i can’t seem to get the camera_to_view_selected() operator working with a context override and could need some help…

Imagine having the following simple scene:


A Suzanne, a Cube and a Camera.

I want to align the camera view to the cube without selecting the Cube. For that, i wanted to use a context.temp_override() for the “selected_objects” and then call the bpy.ops.view3d.camera_to_view_selected() operator like so:

import bpy

selected_objects = [bpy.data.objects['Cube']]

with bpy.context.temp_override(selected_objects=selected_objects):
    bpy.ops.view3d.camera_to_view_selected() # No reaction...
    # bpy.ops.transform.translate(value=(0.0, 0.0, 1.0)) # --> This works fine

Doing the same thing with the translate operator works fine, but with this one it just doesn’t work… If i have the Suzanne selected, the camera view will be aligned to that Suzanne and not the Cube…

Does anyone have some idea, what i’m missing here?

Thanks in advance!

may i ask why you want to do it that way?

“Normally” you would just move the camera, if you want to render after that. So another way would just be to do it the “normal” way and use the

bpy.app.handlers.frame_change_post.append(handler)

(or -pre) event and just select the objects, call view_to_selected and then just render.

Hi @Blender_Fun1 ,
thanks for joining in :slight_smile:

I’m currently rewriting a script at work, which often made use of operators and therefore deselecting and selecting objects all the time. It’s awful to work with, hence why i want to avoid that.

I did managed to get the same functionality with the .camera_fit_coords() function - letting me avoid operators all together. But in terms of speed, i just couldn’t get it on par with the operator… For context: I’m doing this on CAD-geometry with a couple million vertices.

Because of that, i thought i would just swallow the pill and use the operator → but with a context.override() to avoid having to deselect and select objects. But that’s where i hit a road block :sweat_smile:

I’ve investigated view3d.camera_to_view_selected code and it seems it’s accessing selected objects explicitly, not through context.selected_objects. So you won’t be able to use temp_override for this, the only option is to deselect all objects, select others and run the operators and then apply selection again. Or you can temporarily deselect jump couple objects you don’t need.

I’ve added info I’ve found to https://andrej730.github.io/bpy.ops.context/#view3d.camera_to_view_selected

Hi @Andrej ,
thank you very much for your answer!

Seems like i’m out of luck then :sweat_smile:

maybe that’s worth to file a bug report?

update: i filed a bug report

Can you please link it here?

Though report is likely to be closed as it’s not a bug - there is no requirement that operators should support context override.

[https://projects.blender.org/blender/blender/issues/140264](https://Bug report)